Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
HelloI'm using SQL Server 2008I need to know if it is possible to use expression in a Count()The purpose is to count the number of row for a specific valueFor example
Select Name,COUNT([date]>'20110101') AS Since2011,COUNT([date]>'20120101') AS Since2012FROM MyTableGROUP BY Name
Thank for your help
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts
Posted - 2012-02-16 : 09:19:01
perhaps this
Select Name,SUM(CASE WHEN [date]>'20110101' THEN 1 ELSE 0 END) AS Since2011,SUM(CASE WHEN [date]>'20120101' THEN 1 ELSE 0 END) AS Since2012FROM MyTableGROUP BY Name
http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
Thank you DontAtWorkIndeedBut I use a count this waycount(case WHEN [date] > '20111001' then 1 end)Thank you BrettNo I do not really need a range (the name of the fields are Since2011 and Since2012 : obviously 2012 is contained in Since2011)