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
 General SQL Server Forums
 New to SQL Server Programming
 Case Date

Author  Topic 

stwp86
Starting Member

42 Posts

Posted - 2012-10-02 : 10:00:45
Hey Everyone . . .

Quick question. I would like to add a case statement to my select, so that when the date field is one of the 4 earilest dates a temporary column writes out "Baseline" and when it is not one of the 4 earliest dates it writes out "Trending". Here is my first attempt:


SELECT n.Client_mnemonic,round((([Total Elapsed Time (sec)]/WeeklyUserCount)/60.0),2) AS "Total Time" ,m.WeekMinDate as "Work Week", n.Group_Type,
CASE WHEN m.weekmindate Top 4 THEN 'Baseline'
ELSE 'Trending'
END as "BarType"


I dont really understand Case statements that well, so I am a bit perplexed on how to get the four earliest dates. I thought something like TOP 4, but that didn't seem to work out, or I did it wrong.

Any thoughts?

Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-02 : 10:28:14
m.weekmindate Top 4 is not a valid syntax. i think what you need is this


SELECT *,
CASE WHEN Seq <= 4 THEN 'Baseline'
ELSE 'Trending'
END as "BarType",
...

FROM
(
SELECT n.Client_mnemonic,round((([Total Elapsed Time (sec)]/WeeklyUserCount)/60.0),2) AS "Total Time" ,m.WeekMinDate as "Work Week", n.Group_Type,ROW_NUMBER() OVER (ORDER BY m.weekMinDate) AS Seq
...

)t


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

Go to Top of Page

stwp86
Starting Member

42 Posts

Posted - 2012-10-02 : 10:37:28
Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-02 : 10:41:09
welcome

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

Go to Top of Page
   

- Advertisement -