[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

SQl Query

Hello,
I have a table billing which has patientid,billamnt,invoicegrp,facilityid.
There is another table patientmedicaredays which has facilityd,patientid,startdt,enddt.
The billing table has one row for a patient.
The patientmedicaredays has multiple rows for a single patient with different values for the startdt and enddt.
Iam trying to get data in such a way to display one row per patient from the billing table and startdt and enddt as
startdt1 to enddt1, startdt2 to enddt2...depending on the coverage days break.The last column coveragedays iam looking to get all the dates concatenated.Some times the enddt is null too.

The query is below:

SELECT b.FacID,b.patid, B.BillAmt, coveragedays
FROM FwReports.dbo.Billing B
left join Pat..PatientMediCareCycles pc on b.facid = pc.facid and b.patid = pc.patid


Iam trying to display the data in crystal xi.

Please help
0
Star79
Asked:
Star79
  • 3
  • 2
  • 2
  • +1
1 Solution
 
David ToddSenior DBACommented:
Hi,

My standard answer to concatenation queries:
http://www.sqlservercentral.com/scripts/Miscellaneous/31894/

In that article I show a sample function that returns a string of comma separated values such that you can write the query above in a meaningful way.

HTH
   David
0
 
Star79Author Commented:
But the fuction tries to add the dates, but Iam looking to conacatenate the 2 dates with a ','
0
 
David ToddSenior DBACommented:
Hi,

You'll need to convert the dates to strings - say varchar( 10 ).

And if you try numbers it will add them.

What I'm showing is a generalised version of sum which can instead of adding, concatenate strings. Dates are a complex sort of number and so were being added for you.

HTH
  David
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
Star79Author Commented:
I rewrote your function as

create function concatFieldNames (@startdt varchar(20),@enddt varchar(20))
returns varchar(8000)
as
begin
declare @fields varchar(8000)
set @fields = null
set @fields = coalesce( @startdt + ', ', '' ) + coalesce(@enddt,0)
return @fields
end --function
go


and the select is as follows:

select dbo.concatFieldNames(CONVERT(DATETIME, CONVERT(CHAR(8), pc.startdt, 112)) from patientmcarecycles


it gives an error as below:
Conversion failed when converting the varchar value 'Jul 31 2014 12:00AM, ' to data type int.
0
 
mlmccCommented:
Shouldn't your select have 2 parameters since the function does.  Also since the function is looking for varchars, why convert it back to a date?

Try it this way.
select dbo.concatFieldNames(CONVERT(VARCHAR(20), pc.startdt, 112),CONVERT(VARCHAR(20), pc.enddt, 112)) from patientmcarecycles


You can also do the concatenation using Crystal formulas and display it in the group footer.

mlmcc
0
 
PortletPaulCommented:
Using a function isn't necessary, a combination of CROSS APPLY plus STUFF() and FOR XML PATH will concatenate a many-to-one relationship of data into a comma separated string - but with date of course you do have to convert them to string as well.
SELECT
      b.FacID
    , b.patid
    , B.BillAmt
    , coveragedays
    , ca1.DatePairs
FROM FwReports.dbo.Billing B
      CROSS APPLY (
                  SELECT
                        STUFF((
                              SELECT
                                    ', '
                                    + CONVERT(varchar, [pc.startdt], 112)
                                    + ' - '
                                    + CONVERT(varchar, [pc.enddt], 112)
                              FROM Pat..PatientMediCareCycles pc
                              WHERE pc.facid = b.facid
                                    AND b.patid = pc.patientid
                              ORDER BY pc.startdt, pc.enddt -- assuming you want then ordered
                              FOR xml PATH ('')
                        )
                        , 1, 1, '')
            ) ca1 (DatePairs)
;

Open in new window

{+ edit} the use of 112 as the date 'style' is arbitrary, please choose the sytyle number that best suits you. see:
http://www.experts-exchange.com/Database/MS-SQL-Server/A_12315-SQL-Server-Date-Styles-formats-using-CONVERT.html
0
 
Star79Author Commented:
portletpaul, that works but pc.startdt and pc.endt has null values for some patients, if thats the case I need to check for null values and then concatenate with emptystring
0
 
PortletPaulCommented:
mmmmm

1. change "cross apply" to "outer apply" (might not be necessary but worth a try)

2. introduce a case expression inside the apply:

SELECT
      CASE
            WHEN [pc.startdt] IS NOT NULL AND [pc.enddt] IS NOT NULL THEN
                      ', '
                      + CONVERT(varchar, [pc.startdt], 112)
                      + ' - '
                      + CONVERT(varchar, [pc.enddt], 112)
            WHEN [pc.startdt] IS NOT NULL THEN
                      ', '
                      + CONVERT(varchar, [pc.startdt], 112)
            WHEN [pc.startdt] IS NULL AND [pc.enddt] IS NULL THEN
                      ', '
            ELSE 
                      ', unexpected'
      END

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now