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 |
|
Rosembm1
Starting Member
15 Posts |
Posted - 2009-04-01 : 07:37:59
|
| Hello Experts, I am fairly new to T-SQL and am trying to use an over clause instead of a tedious subquery because of all the joins. I want to create an aggregate sum of Swab_YN by Each Unit_GroupHere is the code below: There is an error message with the swab_YN groupingselect lu.unit_group , gen.udfleadingzeroformat(cast(datepart(mm,adt.admitdate)as varchar(2)),2) as [ICU Month],cast(datepart(yy,adt.admitdate) as varchar(4)) as [ICU Year], [Total Swabs]= sum(o.Swab_YN),-- this aggregate query does not work [total swabs by unit] = sum(o.Swab_YN) OVER (partition by lu.unit_group) from [cc-clbisql51\sqlp105].qpsi_data.inf.ICUAdmit_Outcomes as O inner join [cc-clbisql51\sqlp105].qpsi_data.Adt.ICUAdmissions as ADT on ADT.icuadmit_ID= O.icuadmit_ID left join [cc-clbisql51\sqlp105].qpsi_data.inf.SwabResults as swab on O.swab_YN_ID=swab.swab_id and swab.collectdate between adt.admitdate and adt.dischargedate left join [cc-clbisql51\sqlp105].qpsi_data.clr.mupirocin as clr on clr.mupirocin_id = o.mupirocin_id left join qpsi_data.inf.Unit_Lookup as LU on adt.Unit=LU.Nursing_Unit and (adt.admitdate between unitchangedate and unitenddate) where O.HospitalAdmitDate is not null and O.HospitalAdmitDate >= '1/1/2008'and unit_group is not null group by unit_group, year(adt.admitdate ) , month(adt.admitdate )order by unit_group, year(adt.admitdate ), month(adt.admitdate ) I tried an order by with the partition but it doesn't work either. Hopefully, I am overlooking something simple.Thanks for your helpMWRMWRosenblatt |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-01 : 09:20:53
|
| Follow the first link in my signature. If you then restate your question with the requested DDL and DML, you will get an answer MUCH faster. (bet someone answers your question before you follow the link, and then i will look silly. )[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-01 : 12:34:53
|
| are you using sql 2005? over works only in sql 2005 & above |
 |
|
|
Rosembm1
Starting Member
15 Posts |
Posted - 2009-04-01 : 12:47:07
|
quote: Originally posted by visakh16 are you using sql 2005? over works only in sql 2005 & above
Yes - 2005. The query is against a detail table with the group by- is that a problem? I tested the query against a summary table and it does work, so I think I have the basics. MWRosenblatt |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-01 : 13:03:55
|
quote: Originally posted by Rosembm1
quote: Originally posted by visakh16 are you using sql 2005? over works only in sql 2005 & above
Yes - 2005. The query is against a detail table with the group by- is that a problem? I tested the query against a summary table and it does work, so I think I have the basics. MWRosenblatt
you've not included some fields in group by like [ICU Month],[ICU Year],... thats why the error. also you might need to wrap the partition by sum inside derived table and take group by outside likeselect unit_group, [ICU Year] , [ICU Month],[total swabs by unit],[Total Swabs]= sum(o.Swab_YN)from(select lu.unit_group , gen.udfleadingzeroformat(cast(datepart(mm,adt.admitdate)as varchar(2)),2) as [ICU Month],cast(datepart(yy,adt.admitdate) as varchar(4)) as [ICU Year], o.Swab_YN,-- this aggregate query does not work [total swabs by unit] = sum(o.Swab_YN) OVER (partition by lu.unit_group) from [cc-clbisql51\sqlp105].qpsi_data.inf.ICUAdmit_Outcomes as O inner join [cc-clbisql51\sqlp105].qpsi_data.Adt.ICUAdmissions as ADTon ADT.icuadmit_ID= O.icuadmit_ID left join [cc-clbisql51\sqlp105].qpsi_data.inf.SwabResults as swab on O.swab_YN_ID=swab.swab_idand swab.collectdate between adt.admitdate and adt.dischargedate left join [cc-clbisql51\sqlp105].qpsi_data.clr.mupirocin as clron clr.mupirocin_id = o.mupirocin_idleft join qpsi_data.inf.Unit_Lookup as LUon adt.Unit=LU.Nursing_Unit and (adt.admitdate between unitchangedate and unitenddate)where O.HospitalAdmitDate is not null and O.HospitalAdmitDate >= '1/1/2008'and unit_group is not null )tgroup by unit_group, [ICU Year] , [ICU Month],[total swabs by unit]order by unit_group, [ICU Year] , [ICU Month] also converting month,year,... to varchar will affect sorting as it will try to take alphabetical sequence rather than numeric sequence. |
 |
|
|
Rosembm1
Starting Member
15 Posts |
Posted - 2009-04-01 : 13:17:32
|
| Thank you for pointing me in the correct direction!MWRosenblattSpins Yak |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-01 : 14:02:11
|
| welcome |
 |
|
|
|
|
|
|
|