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
Star79Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
PortletPaulfreelancerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
PortletPaulfreelancerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.