|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Convert Query from accessHi,
I'm learning to use sql server ('00) and am trying to convert this query from access that will count the # of increases, declines, and no changes for a field. SELECT dDate, Grp, Sum(IIf([DRet]>0,1,0)) AS Adv, Sum(IIf([DRet]<0,1,0)) AS [Dec], Sum(IIf([DRet]=0,1,0)) AS NoChg FROM DailyData GROUP BY dDate, Grp ORDER BY dDate, Grp; Since IIF is not a function in SQL server, I'm a little stumped. Thank you. kohai T-SQL's equivalent to Access's IIF function is the CASE expression. Look it
up in Books Online. SELECT dDate ,Grp ,Sum(case when [DRet]>0 then 1 else 0 end)) AS Adv ,Sum(case when [DRet]<0 then 1 else 0 end)) AS [Dec] ,Sum(case when [DRet]=0 then 1 else 0 end)) AS NoChg FROM DailyData GROUP BY dDate ,Grp ORDER BY dDate ,Grp; ML --- http://milambda.blogspot.com/ Ah, crap!
Too many parentheses. SELECT dDate ,Grp ,Sum(case when [DRet]>0 then 1 else 0 end) AS Adv ,Sum(case when [DRet]<0 then 1 else 0 end) AS [Dec] ,Sum(case when [DRet]=0 then 1 else 0 end) AS NoChg FROM DailyData GROUP BY dDate ,Grp ORDER BY dDate ,Grp; ML --- http://milambda.blogspot.com/ ML,
Thanks for the quick reply. Works like a champ!! Thank you very much. I'll read up on the Case statement. kohai Show quote "ML" wrote: > T-SQL's equivalent to Access's IIF function is the CASE expression. Look it > up in Books Online. > > SELECT dDate > ,Grp > ,Sum(case when [DRet]>0 then 1 else 0 end)) AS Adv > ,Sum(case when [DRet]<0 then 1 else 0 end)) AS [Dec] > ,Sum(case when [DRet]=0 then 1 else 0 end)) AS NoChg > FROM DailyData > GROUP BY dDate > ,Grp > ORDER BY dDate > ,Grp; > > > ML > > --- > http://milambda.blogspot.com/
Other interesting topics
|
|||||||||||||||||||||||