| Author |
Topic |
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-21 : 13:57:01
|
| I am trying to collect the last five order dates, if available, for a patient and I want each of the dates to be in its own column.ExamplePatient OrderDat1 OrderDat2 OrderDat3 OrderDat4 OrderDat5Not all patients with have an order and if they do there may not be five previous or there could be more than five.Any help is appreciated. |
|
|
DBAPBFL
Starting Member
11 Posts |
Posted - 2011-10-21 : 16:34:27
|
| I did it for 3. it this works for you, you can modify it for 5 so you understand the code...create table #PatientOrders (Patient varchar(10),OrderDate datetime)insert #PatientOrders values ('Patient1','1/1/2011')insert #PatientOrders values ('Patient1','1/2/2011')insert #PatientOrders values ('Patient2','2/1/2011')insert #PatientOrders values ('Patient2','2/2/2011')insert #PatientOrders values ('Patient2','2/3/2011')insert #PatientOrders values ('Patient2','2/4/2011')select * from #PatientOrdersselect *from(select Patient ,'OrderDat'+ cast(row_number() over (partition by Patient order by OrderDate) as varchar) as OrderDateNum ,OrderDatefrom #PatientOrders) as spivot(max(OrderDate)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3])) AS p |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-22 : 04:39:51
|
quote: Originally posted by DBAPBFL I did it for 3. it this works for you, you can modify it for 5 so you understand the code...create table #PatientOrders (Patient varchar(10),OrderDate datetime)insert #PatientOrders values ('Patient1','1/1/2011')insert #PatientOrders values ('Patient1','1/2/2011')insert #PatientOrders values ('Patient2','2/1/2011')insert #PatientOrders values ('Patient2','2/2/2011')insert #PatientOrders values ('Patient2','2/3/2011')insert #PatientOrders values ('Patient2','2/4/2011')select * from #PatientOrdersselect *from(select Patient ,'OrderDat'+ cast(row_number() over (partition by Patient order by OrderDate desc ) as varchar) as OrderDateNum ,OrderDatefrom #PatientOrders) as spivot(max(OrderDate)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3])) AS p
as it says last x dates------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-22 : 04:43:11
|
quote: Originally posted by DBAPBFL I did it for 3. it this works for you, you can modify it for 5 so you understand the code...create table #PatientOrders (Patient varchar(10),OrderDate datetime)insert #PatientOrders values ('Patient1','1/1/2011')insert #PatientOrders values ('Patient1','1/2/2011')insert #PatientOrders values ('Patient2','2/1/2011')insert #PatientOrders values ('Patient2','2/2/2011')insert #PatientOrders values ('Patient2','2/3/2011')insert #PatientOrders values ('Patient2','2/4/2011')select * from #PatientOrdersselect *from(select Patient ,'OrderDat'+ cast(row_number() over (partition by Patient order by OrderDate) as varchar) as OrderDateNum ,OrderDatefrom #PatientOrders) as spivot(max(OrderDate)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3])) AS p
also its better to specify the length when casting to varchar though in this case it wont cause much harmhttp://visakhm.blogspot.com/2010/02/importance-of-specifying-length-in.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-10-22 : 08:51:08
|
| you can specify varchar(max)Please mark answer as accepted if it helped you.Thanks,Jassi Singh |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-22 : 09:08:32
|
quote: Originally posted by jassi.singh you can specify varchar(max)Please mark answer as accepted if it helped you.Thanks,Jassi Singh
only if you're sure that you need to store data above 8000 character limit------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-10-23 : 04:35:16
|
| if you are sure about the character limit then you can limit to 4000Please mark answer as accepted if it helped you.Thanks,Jassi Singh |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-23 : 10:52:53
|
quote: Originally posted by jassi.singh if you are sure about the character limit then you can limit to 4000Please mark answer as accepted if it helped you.Thanks,Jassi Singh
Hmm... not necessarily. there's no mandatory condition that it should have minimum 4000 characters length. The best bet is it depends on your data------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-24 : 11:58:25
|
| [code]select *from(selectPatient,'OrderDat'+ cast(dense_rank() over (partition by Patient order by OrderDate desc) as varchar(3)) as OrderDateNum,OrderDatefrom #PatientOrders) as spivot(max(OrderDate)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3])) AS p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-24 : 12:03:44
|
| Thank you, this works perfectly. I was able to tweek it a bit as I might have multiple orders for a patient in a day and also I wanted them to be in descending order. Got it all done and it looks great. I will now incorportate it into the rest of my stored procedure.I would be very happy to mark that your answer was appropriate but not sure how to do that. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-24 : 12:07:02
|
quote: Originally posted by PGallahe Thank you, this works perfectly. I was able to tweek it a bit as I might have multiple orders for a patient in a day and also I wanted them to be in descending order. Got it all done and it looks great. I will now incorportate it into the rest of my stored procedure.I would be very happy to mark that your answer was appropriate but not sure how to do that.
not any direct way to do that other than appending [solved] to topic title ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-24 : 12:52:27
|
| Ok, now I am stumped again. I am not sure how to join this information with the rest of the data in my stored procedure.I have multiple selects where I am gathering information into several declared tables so that I can output all the needed information. How do I join this data with the others? I hope all this makes sense. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-24 : 13:36:26
|
| would you mind showing your rest of stored proc?best bet is to put this in a temporary table and join to others on common fields------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-24 : 13:52:05
|
| Here is what I have:Declare @Patient Table ( PatientOid int ,PatientVisitOid int ,CollectedDT datetime ,UserAbbrName varchar(100) ,FindingAbbr varchar(100) ,Value varchar(100) ,FindingName varchar(100) ,PatientAccountID varchar(20) ,POLST varchar(10) ,DNRDT1 datetime ,DNRDT2 datetime ,DNRDT3 datetime ,DNRDT4 datetime ,DNRDT5 datetime )Declare @DNR Table ( PatientOid int ,OrderDT datetime ) Insert into @DNRselect PatientOid = P.PatientOid ,OrderDT = o.StatusEnteredDateTimeFrom @Patient p Inner Join Order o with(Nolock) on p.PatientOid = o.Patient_oidwhere o.OrderAbbreviation in ('1010011', '1010002', '1010001') insert into @Patientselect PatientOid = p.PatientOid ,PatientVisitOid = p.PatientVisitOid ,CollectedDT = ha.CollectedDT ,UserAbbrName = ha.UserAbbrName ,FindingAbbr = O.FindingAbbr ,Value = o.Value ,FindingName = o.FindingName ,PatientAccountID = p.PatientAccountID ,POLST = '' ,DNRDT1 = NULL ,DNRDT2 = NULL ,DNRDT3 = NULL ,DNRDT4 = NULL ,DNRDT5 = NULLFrom Assessment a with(Nolock) Inner Join Observation o with(nolock) on a.Patient_Oid = o.Patient_oid and a.AssessmentID = o.AssessmentID and a.IsVersioned = o.IsVersionedWhere o.FindingAbbr = 'A_HomeEnviron' AND (O.Value like '%Assisted Living%' or o.Value like '%SNF%' or o.Value like '%Nursing%' or o.Value like '%ECF%') and a.AssessmentStatus = 'Complete' Update @Patient Set POLST = o.ValueFrom @Patient p Inner Join Assessment a with(Nolock) on p.PatientOid = a.Patient_oid and p.PatientVisitOid = a.PatientVisit_oid Inner Join Observation o with(nolock) on p.PatientOID = o.Patient_oid and a.AssessmentID = o.AssessmentID and a.IsVersioned = o.IsVersionedWhere o.FindingAbbr = 'A_DB_ADB POLST' and o.Value <> ' ' and a.AssessmentStatus = 'Complete' select *from (select PatientOid ,'OrderDat'+ cast(row_number() over (partition by PatientOid Order by OrderDT desc) as varchar) as OrderDateNum ,OrderDTfrom @DNRGroup by PatientOid ,OrderDT) as spivot(max(OrderDT)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3], [OrderDat4], [OrderDat5])) AS p What I need is to have the dates some how or another be part of the @Patient declared table. There will not be a one to one on the @patient information and the @dnr information, there will be more in @patient. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-25 : 00:03:13
|
| so how will be data from @Patient after these additions? can you show sample structure with data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-25 : 11:48:39
|
| I want the data to look like this:I want to have the data look like this:CollectedDT UserAbbrName FindingAbbr Value FindingName PatientAccountID POLST DNRDT1 DNRDT2 DNRDT3 DNRDT4 DNRDT5DNRDT1 through DNRDT5 will represent the data that is in [OrderDat1],[OrderDat2],[OrderDat3], [OrderDat4], [OrderDat5]. I hope that makes sense. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-26 : 00:56:27
|
replace last pivot statement with thisupdate pset p.DNRDT1 = m.[OrderDat1] ,p.DNRDT2 = m.[OrderDat2],p.DNRDT3 = m.[OrderDat3],p.DNRDT4 = m.[OrderDat4],p.DNRDT5 = m.[OrderDat5]from @patient pjoin(select *from(selectPatientOid,'OrderDat'+ cast(row_number() over (partition by PatientOid Order by OrderDT desc) as varchar) as OrderDateNum,OrderDTfrom @DNRGroup by PatientOid,OrderDT) as spivot(max(OrderDT)for OrderDateNum in ([OrderDat1],[OrderDat2],[OrderDat3], [OrderDat4], [OrderDat5])) AS p)mon m.PatientOid = p.PatientOid ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
PGallahe
Starting Member
10 Posts |
Posted - 2011-10-27 : 15:53:11
|
Again, thank you so much. This resolved the problem and I am getting exactly what I want. I just wish that I understood the code - in time I guess. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|