Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ckedwards65
Starting Member
4 Posts |
Posted - 2013-06-07 : 18:51:48
|
I am really stuck. I have been using SQL for awhile, but only for surface level stuff and I have not been able to figure out how to get what I need done. I would appreciate any help I can get on getting this query to work. Even if it is just to point me to a website or give me some ideas on how I need to code this.Here is the scenarioThere are two tablespatient and labresultskey between two tables is idpatient fieldthere are only two fields in the table that I am concerned with Patd - idpatientlabresults - idpatient and idtlI need to produce a list of patients that have at least one record in the labresults table for 5 different tests. the idtl values are listed below. idtl nametestthere is only 1 idtl value for these tests1159 URR1683 Height CM1714 WEIGHT-POST (KG)[/red]2343 HCT CALC (HGBX3)this test can have the following values1730 KT/V CMH1744 KT/V TOTAL STD1747 KT/V TOTAL FRIIS1760 KT/V BOY SHORT1761 KT/V BOY TALL1762 KT/V GIRLS SHORT1763 KT/V GIRL TALL1780 spKt/V Total1828 spKdt/V Dialysis1832 stdKT/V Total2838 KT/V Total PD2848 KT/V Total PDthis is what I have, but it is still resulting in multiple records being generated for each patientSelect distinct p.idpatient ,(Case When c.idtl = 1159 Then c.idtl End ) ,(Case When c.idtl = 1683 Then c.idtl End ) ,(Case When c.idtl = 2423 Then c.idtl End ) ,(Case When c.idtl = 1714 Then c.idtl End ) ,(Case When c.idtl in (2848,1832,1828,1780,1763,1762,1761,1760,1747,1744,1730) Then c.idtl End ) FROM patd pjoin close_results con p.idpatient=c.idpatientBut the result is idpatient urr hgt hct wgt ktv11251 1159 NULL NULL NULL NULL11251 NULL 1683 NULL NULL NULL11251 NULL NULL NULL 1714 NULL11251 NULL NULL NULL NULL NULL11251 NULL NULL 2343 NULL 1780What I need it to be is this:idpatient urr hgt hct wgt ktv11251 1159 1683 2343 1714 1780And the row should only display i all of the five test fields have a value.I will really appreciate any help I can get with this. I am stuck. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-08 : 03:48:20
|
[code]Select p.idpatient,MAX(Case When c.idtl = 1159 Then c.idtl End ),MAX(Case When c.idtl = 1683 Then c.idtl End ),MAX(Case When c.idtl = 2423 Then c.idtl End ),MAX(Case When c.idtl = 1714 Then c.idtl End ),MAX(Case When c.idtl in (2848,1832,1828,1780,1763,1762,1761,1760,1747,1744,1730) Then c.idtl End )FROM patd pjoin close_results con p.idpatient=c.idpatientgroup by p.idpatient[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|