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 |
scottjon
Starting Member
3 Posts |
Posted - 2012-03-26 : 06:50:07
|
Hi, I am trying to end a case when statement with nested queries and I can't get the ending right and get the error message; Incorrect syntax near the keyword 'group'...Any assistance gratefully received. and case when locode='DB' then (case when datename(dw,wdate)='Friday' then 'N' when datename(dw,wdate)='Saturday' then 'N' else 'Y' end) else (case when tc.tkcal ='SA' then (case when datename(dw,wdate)='Thursday' then 'N' when datename(dw,wdate)='Friday' then 'N' else (case when datename(dw,wdate)='Saturday' then 'N' when datename(dw,wdate)='Sunday' then 'N' else 'Y' end) end group by loregion1,ldesc ,tkloc, |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-03-26 : 06:59:41
|
Don't know about the logic, but you need two more brackets and two more "END"s.casewhen locode='DB' then(casewhen datename(dw,wdate)='Friday' then 'N'when datename(dw,wdate)='Saturday' then 'N'else 'Y' end)else(case when tc.tkcal ='SA' then(casewhen datename(dw,wdate)='Thursday' then 'N'when datename(dw,wdate)='Friday' then 'N'else(casewhen datename(dw,wdate)='Saturday' then 'N'when datename(dw,wdate)='Sunday' then 'N'else 'Y' end) end ) END ) END |
|
|
scottjon
Starting Member
3 Posts |
Posted - 2012-03-26 : 07:09:26
|
Hi,Probably worth sending all the code. I have added the parts suggested below and now get; Msg 4145, Level 15, State 1, Line 98An expression of non-boolean type specified in a context where a condition is expected, near 'group'.declare @earliest datetimedeclare @to datetimedeclare @days intset @earliest = '01 March 2011'set @to = dbo.cf_getdateonly(getdate())-1set @days = 28select detail.Region,detail.Office,detail.PGroup,count(*) [No_of_Fee_Earners],sum(Last20) [Total_Last20],round(avg(Days),1) [Average_Outstanding],sum(Prior20) [Total_Prior20],sum(Offset) [Offset]from (selectloregion1 [Region],tkloc [loc],ldesc [Office],case when loregion1 = 'LON' then udvalue1 else ''end [PGroup],wtkinit [Initials],sum(7-(isnull(tdb,0)+isnull(tdnb,0))) [Missing_Hours],sum(CEILING(( 7-(isnull(tdb,0)+isnull(tdnb,0)))/7)) [Days],sum(case when wdate >=(@to-@days-isnull(offset,0)) then 1 else 0 end ) [Last20] ,sum(case when wdate <(@to-@days-isnull(offset,0)) then 1 else 0 end)[Prior20] , sum(offset) [Offset]from(selectwdate,tkinit wtkinitfromlov_cal,timekeepwherewdate between @earliestand @toand wdate>=tkemdateand tktmdate is nulland tkeflag='Y') wleft join timekeep on wtkinit=timekeep.tkinitleft join location on tkloc=locodeleft join udfvlst on tksect = udvalue and udfvlst = 'LNPA'left join tkdaily on wdate=tddate and wtkinit=tdinitleft join (select lhloc, count(*) offset from lochldywhere lhdate between getdate()-@days and getdate() group by lhloc) offset on lhloc = tkloc left join timecal as tc on wtkinit = tc.tkinitwherewdate not in(select lhdate from lochldy where lhloc=locode)and 7-(isnull(tdb,0)+isnull(tdnb,0))>0 and case when locode='DB' then (case when datename(dw,wdate)='Friday' then 'N' when datename(dw,wdate)='Saturday' then 'N' else 'Y' end) else (case when tc.tkcal ='SA' then (case when datename(dw,wdate)='Thursday' then 'N' when datename(dw,wdate)='Friday' then 'N' else (case when datename(dw,wdate)='Saturday' then 'N' when datename(dw,wdate)='Sunday' then 'N' else 'Y' end) end) end) end group by loregion1,ldesc ,tkloc,case when loregion1 = 'LON' then udvalue1else ''end,wtkinit) detail group by Region,Office,PGroup |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-03-26 : 07:14:57
|
You have to indicate what the result of the nested CASE expression needs to be compare to - for example:...wherewdate not in(select lhdate from lochldy where lhloc=locode)and 7-(isnull(tdb,0)+isnull(tdnb,0))>0and casewhen locode='DB' then(casewhen datename(dw,wdate)='Friday' then 'N'when datename(dw,wdate)='Saturday' then 'N'else 'Y' end)else(case when tc.tkcal ='SA' then(casewhen datename(dw,wdate)='Thursday' then 'N'when datename(dw,wdate)='Friday' then 'N'else(casewhen datename(dw,wdate)='Saturday' then 'N'when datename(dw,wdate)='Sunday' then 'N'else 'Y' end) end) end) end = YesNoColumngroup by loregion1,ldesc ,tkloc,case when loregion1 = 'LON' then udvalue1else ''end,wtkinit) detail Here, the YesNoColumn is the column from some table in your data. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-26 : 13:07:35
|
your case expression looks horribly complicated. Can you explain what exactly you're trying to achieve here giving data sample?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|