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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select query where condition case incorrect syntax

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-08-25 : 12:41:57
I am getting incorrect syntax error on =1

on this line in where condition case:
WHEN 1 THEN month(t.PctCompleteDate)=1

Is it the proper way to have case statement within where condition?


set @ProgID = 59

set @yearnum = 2007

set @QtrYearNum = 3


SELECT t.ProgID, t.taskid,isnull(avg(t.PctComplete),0),isnull(avg(t.award_amt),0), ISNULL(SUM(c.amount),0)
FROM TAB_ccsNetTasksLog t
INNER JOIN tab_ccsnetcostdetails c ON (t.TaskID = c.TaskID and c.EstimateType ='Final' and c.COexecuted = 1 AND t.ProgID=@ProgID)
Inner Join tab_ccsnetco co on (c.coid = co.coid and c.coexecuted=1)
Where
CASE @QtrYearNum
WHEN 1 THEN month(t.PctCompleteDate)=1 or month(t.PctCompleteDate)=2 or month(t.PctCompleteDate)= 3 and year(t.PctCompleteDate) = @yearnum
WHEN 2 THEN month(t.PctCompleteDate)= 4 or month(t.PctCompleteDate)= 5 or month(t.PctCompleteDate)= 6 and year(t.PctCompleteDate) = @yearnum
WHEN 3 THEN month(t.PctCompleteDate)= 7 or month(t.PctCompleteDate)= 8 or month(t.PctCompleteDate)= 9 and year(t.PctCompleteDate) = @yearnum
WHEN 4 THEN month(t.PctCompleteDate)=10 or month(t.PctCompleteDate)= 11 or month(t.PctCompleteDate)= 12 and year(t.PctCompleteDate) = @yearnum
END
GROUP BY t.ProgID, t.taskid, t.award_amt, t.Pctcomplete



Thank you very much for the helpful info.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-25 : 13:00:02
the where should be just this


....
Where datepart(qq,t.PctCompleteDate) = @QtrYearNum and year(t.PctCompleteDate)=@YearNum
....


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-25 : 13:01:53
[code]Where (@QtrYearNum = 1 and month(t.PctCompleteDate)=1 or month(t.PctCompleteDate)=2 or month(t.PctCompleteDate)= 3 and year(t.PctCompleteDate) = @yearnum)
or
(@QtrYearNum = 2 and month(t.PctCompleteDate)= 4 or month(t.PctCompleteDate)= 5 or month(t.PctCompleteDate)= 6 and year(t.PctCompleteDate) = @yearnum)
or
(@QtrYearNum = 3 and month(t.PctCompleteDate)= 7 or month(t.PctCompleteDate)= 8 or month(t.PctCompleteDate)= 9 and year(t.PctCompleteDate) = @yearnum)
or
(@QtrYearNum = 4 and month(t.PctCompleteDate)=10 or month(t.PctCompleteDate)= 11 or month(t.PctCompleteDate)= 12 and year(t.PctCompleteDate) = @yearnum)

[/code]

Visakh's is better. I didn't really evaluate the code, just a quick solution
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-25 : 13:08:37
if you want to utilise existing index on t.PctCompleteDate it should be like


...
where t.PctCompleteDate > = DATEADD(qq,@QtrYearNum-1,DATEADD(yy,@YearNum-1900,0))
and t.PctCompleteDate < DATEADD(qq,@QtrYearNum,DATEADD(yy,@YearNum-1900,0))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -