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 |
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-11 : 19:53:48
|
Hi All,I have the following table with some data:snum: E_IQ: _DATE: _study165 103 2/21/1999 TravelT1165 107 7/13/2000 FraXimagingT11364 123 5/20/1998 TravelT1 FraXimagingT11364 116 11/29/2001 FraXimagingT21364 125 7/23/2004 TravelT21406 65 10/31/1999 TravelT13841 68 6/17/2000 TravelT13841 46 11/20/2004 TravelT23845 72 6/18/2000 TravelT14149 47 9/18/2000 TravelT14149 48 6/8/2004 TravelT24176 66 9/29/2000 TravelT14176 58 11/3/2004 TravelT2 Is that possible to have a report output having all the columns side by side based on the order that is showing below?subj_num165136414063841384541494176_DATE2/21/19995/20/199810/31/19996/17/20006/18/20009/18/20009/29/2000E_IQ1031236568724766_studyTravelT1TravelT1 TravelT1TravelT1TravelT1TravelT1TravelT1_DATEnull7/23/2004null11/20/2004null6/8/200411/3/2004E_IQNull125Null46Null4858_studynullTravelT2nullTravelT2nullTravelT2TravelT2_DATE5/10/2004null12/9/2004null2/11/2004nullnullE_IQ98null67null79null_studyTravelT2nullTravelT2nullTravelT2nullnull I use the following SQL case scrip:select Snum, E_IQ, _DATE ,case when _study like '%t1' then _study end as _study_T1 ,case when _study like '%t2' then _study end as _study_T2from BEH_WISC_III where snum in (165, 1364, 1406, 3841, 3845, 4149, 4176) and (_study like '%T1' or _study like '%T2') But the out put is like the following:snum E_IQ _DATE _study165 103 2/21/1999 TravelT1165 107 7/13/2000 FraXimagingT11364 123 5/20/1998 TravelT1 FraXimagingT11364 116 11/29/2001 FraXimagingT21364 125 7/23/2004 TravelT21406 65 10/31/1999 TravelT13841 68 6/17/2000 TravelT13841 46 11/20/2004 TravelT23845 72 6/18/2000 TravelT14149 47 9/18/2000 TravelT14149 48 6/8/2004 TravelT24176 66 9/29/2000 TravelT14176 58 11/3/2004 TravelT2 Thanks for any help. Abrahim |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-11 : 20:37:24
|
sorry, bit confuse. What is your result that you required ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-11 : 22:53:58
|
Dear Kh,We like to see all the columns side by side based on the order that is showed below:[code]Snum 165136414063841384541494176_DATE2/21/19995/20/199810/31/19996/17/20006/18/20009/18/2000E_IQ1031236568724766_studyTravelT1TravelT1 TravelT1TravelT1TravelT1TravelT1TravelT1_DATEnull7/23/2004null11/20/2004null6/8/200411/3/2004E_IQNull125Null46Null4858_studynullTravelT2nullTravelT2nullTravelT2TravelT2_DATE5/10/2004null12/9/2004null2/11/2004nullnullE_IQ98null67null79null_studyTravelT2nullTravelT2nullTravelT2nullnull[/cod]Somehow I couldnt lineup all the columns in the above example side by side, So, here is the order: Snum : _DATE : E_IQ: _study: _DATE: E_IQ: _study: _DATE: E_IQ: _study:E_IQ: _study:Thanks in advance,Abrahim |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-11 : 23:01:44
|
sorry, i am still totally lost.What do you mean by "side by side" ?the result you posted is all in rows.You want the Snum all in rows follow by E_IQ also in rows ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-12 : 00:03:06
|
I would like the columns to present from left to right across the page with the data beneath each column heading. For example:Column 1 Column 2 Column 3 Column 4data data data data data data data data Does that make sence?Please let me know.Thanks,Abrahim |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-12-12 : 12:38:31
|
So what you really want is one row per SNUM?Like this for example? SNum e_iq Date study e_iq Date study e_iq Date Study 165 103 2/21/1999 TravelT1 107 07/13/2000 FraXimagingT1 NULL NULL NULL1364 123 5/20/1998 TravelT1 FraXimagingT1 116 11/29/2001 FraXimagingT2 125 07/23/2004 TravelT2--etc.. |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-13 : 17:26:56
|
Hi All,Someone Someone suggested me to use table alias and inner quarry, so I have came up with this code but Im getting error:SELECT b2.subj_num AS S_NUM, WSC_FULL_SCALE_IQ AS T1_SCALE_IQ , WSC_which_studyAS T1_study,(SELECT TOP 1 WSC_FULL_SCALE_IQ FROM BEH_WISC_III where subj_num = b2.subj_num)AS T2_WSC_DATE,(SELECT TOP 1 WSC_which_study FROM BEH_WISC_III where subj_num = b2.subj_num)AS T2_which_study FROM BEH_WISC_III b2where subi_num in (1364, 165, 1406,3841, 4149, 4176) Here is the error:Msg 102, Level 15, State 1, Line 3Incorrect syntax near ''.Msg 102, Level 15, State 1, Line 6Incorrect syntax near ''. Thanks for any help, Abrahim |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-15 : 02:48:12
|
Ok,I know everybody is getting sick of this, but I got the script to work but it is not doing what I was expecting:select distinct b.subj_num as SUBJ_NUM, WSC_FULL_SCALE_IQ as IQ, (select top 1 WSC_which_study from BEH_WISC_III where subj_num = b.subj_numand WSC_which_study like 'TravelT1') AS Study1, (select top 1 WSC_which_study from BEH_WISC_III where subj_num = b.subj_numand WSC_which_study like 'TravelT2') AS Study2, (select top 1 WSC_which_study from BEH_WISC_III where subj_num = b.subj_numand WSC_which_study like 'TravelT1 FraXimagingT1') AS StudyFXT1, (select top 1 WSC_which_study from BEH_WISC_III where subj_num = b.subj_numand WSC_which_study like 'FraXimagingT2') AS StudyFXT2 from BEH_WISC_III b where subj_num in (1364, 165, 1406,3841, 4149, 4176) I wish there was a way to attach an Excel spreadsheet to this post whit the example of the report out put that Im wishing for.Abrahim |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-15 : 02:54:18
|
quote: I wish there was a way to attach an Excel spreadsheet to this post whit the example of the report out put that Im wishing for.
Why don't you post the expected output here using the [ code ] [ /code ] (without spaces in between the []), like what Lamprey did ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-15 : 17:43:04
|
Ok,here is the expected output:subj_num WISC_III.WSC_DATE WISC_III.WSC_FULL_SCALE_IQ WISC_III.WSC_which_study WISC_III_1.WSC_DATE WISC_III_1.WSC_FULL_SCALE_IQ WISC_III_1.WSC_which_study WAS_DATE WAS_FULL_SCALE_IQ WAS_which_study165 2/21/1999 103 TravelT1 5/10/2004 98 TravelT21364 5/20/1998 123 TravelT1 7/23/2004 125 TravelT2 1406 10/31/1999 65 TravelT1 12/9/2004 67 TravelT23841 6/17/2000 68 TravelT1 11/20/2004 46 TravelT2 3845 6/18/2000 72 TravelT1 2/11/2004 79 TravelT24149 9/18/2000 47 TravelT1 6/8/2004 48 TravelT2 4176 9/29/2000 66 TravelT1 11/3/2004 58 TravelT2 If you paste all the above output and three tables into Excel sheet, then you should see the columns clearly.Here are the tables:ID table with subj_num:Subj_num165136414063841384541494176here is the BEH_WISC_III table:subj_num WSC_DATE WSC_FULL_SCALE_IQ WSC_which_study165 2/21/1999 103 TravelT1165 7/13/2000 107 FraXimagingT11364 5/20/1998 123 TravelT1 FraXimagingT11364 11/29/2001 116 FraXimagingT21364 7/23/2004 125 TravelT21406 10/31/1999 65 TravelT13841 6/17/2000 68 TravelT13841 11/20/2004 46 TravelT23845 6/18/2000 72 TravelT14149 9/18/2000 47 TravelT14149 6/8/2004 48 TravelT24176 9/29/2000 66 TravelT14176 11/3/2004 58 TravelT2here is BEH_WAIS_III table:subj_num WAS_DATE WAS_FULL_SCALE_IQ165 5/10/2004 981406 12/9/2004 673845 2/11/2004 79 Thanks in advance,Abrahim |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-16 : 08:46:20
|
Your expected output is still mis-aligned. I could only guess this is what you want ?
DECLARE @ID TABLE( subj_num int)INSERT INTO @IDSELECT 165 UNION ALLSELECT 1364 UNION ALLSELECT 1406 UNION ALLSELECT 3841 UNION ALLSELECT 3845 UNION ALLSELECT 4149 UNION ALLSELECT 4176DECLARE @BEH_WISC_III TABLE( subj_num int, WSC_DATE datetime, WSC_FULL_SCALE_IQ int, WSC_which_study varchar(30))INSERT INTO @BEH_WISC_IIISELECT 165, '2/21/1999', 103, 'TravelT1' UNION ALLSELECT 165, '7/13/2000', 107, 'FraXimagingT1' UNION ALLSELECT 1364, '5/20/1998', 123, 'TravelT1 FraXimagingT1' UNION ALLSELECT 1364, '11/29/2001', 116, 'FraXimagingT2' UNION ALLSELECT 1364, '7/23/2004', 125, 'TravelT2' UNION ALLSELECT 1406, '10/31/1999', 65, 'TravelT1' UNION ALLSELECT 3841, '6/17/2000', 68, 'TravelT1' UNION ALLSELECT 3841, '11/20/2004', 46, 'TravelT2' UNION ALLSELECT 3845, '6/18/2000', 72, 'TravelT1' UNION ALLSELECT 4149, '9/18/2000', 47, 'TravelT1' UNION ALLSELECT 4149, '6/8/2004', 48, 'TravelT2' UNION ALLSELECT 4176, '9/29/2000', 66, 'TravelT1' UNION ALLSELECT 4176, '11/3/2004', 58, 'TravelT2'DECLARE @BEH_WAIS_III TABLE( subj_num int, WAS_DATE datetime, WAS_FULL_SCALE_IQ int)INSERT INTO @BEH_WAIS_IIISELECT 165, '5/10/2004', 98 UNION ALLSELECT 1406, '12/9/2004', 67 UNION ALLSELECT 3845, '2/11/2004', 79SELECT [subj_num] = i.subj_num, [WISC_III.WSC_DATE] = MAX(CASE WHEN seq_no = 1 THEN i.WSC_DATE END), [WISC_III.WSC_FULL_SCALE_IQ] = MAX(CASE WHEN seq_no = 1 THEN i.WSC_FULL_SCALE_IQ END), [WISC_III.WSC_which_study] = MAX(CASE WHEN seq_no = 1 THEN i.WSC_which_study END), [WISC_III_1.WSC_DATE] = MAX(CASE WHEN seq_no = 2 THEN i.WSC_DATE END), [WISC_III_1.WSC_FULL_SCALE_IQ] = MAX(CASE WHEN seq_no = 2 THEN i.WSC_FULL_SCALE_IQ END), [WISC_III_1.WSC_which_study] = MAX(CASE WHEN seq_no = 2 THEN i.WSC_which_study END), [WAS_DATE] = MAX(a.WAS_DATE), [WAS_FULL_SCALE_IQ] = MAX(a.WAS_FULL_SCALE_IQ)FROM( SELECT subj_num, WSC_DATE, WSC_FULL_SCALE_IQ, WSC_which_study, seq_no = (SELECT COUNT(*) FROM @BEH_WISC_III x WHERE x.subj_num = i.subj_num AND x.WSC_DATE <= i.WSC_DATE) FROM @BEH_WISC_III i) ileft JOIN @BEH_WAIS_III a ON i.subj_num = a.subj_numGROUP BY i.subj_numORDER BY i.subj_num KH[spoiler]Time is always against us[/spoiler] |
 |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-16 : 21:48:56
|
Hi Kh,Almost there, but I just realized that the information that I posted about BEH_WAIS_III table was not complete, so here is the complete info:About BEH_WAIS_III table:WAS_DATE WAS_FULL_SCALE_IQ WAS_which_study05/10/2004 98 TravelT212/09/2004 67 TravelT202/11/2004 79 TravelT2 Sorry about that.Thanks in advance,Abrahim |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-16 : 21:51:16
|
So is the query does what you want ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
akalehzan
Starting Member
21 Posts |
Posted - 2007-12-16 : 22:49:58
|
Kh,Yes indeed, thats what we ware looking for.Regards,Abrahim |
 |
|
|
|
|
|
|