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)
 beginner question

Author  Topic 

splash
Starting Member

2 Posts

Posted - 2012-01-04 : 11:11:44
I have a table that looks something like this

Fund1 23
Fund2 58
Fund3 17
Fund4 77
Fund5 12

I want a select statement that will return 4 fields: 0-25, 26-50, 51-75, 76-100. The value in each field will be the count of how many funds fall into each bracket. So the results would look like this:

Q1 Q2 Q3 Q4
3 0 1 1

Can anyone help me? Thanks.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-04 : 11:25:15
[code]SELECT
SUM(CASE WHEN Qtr BETWEEN 0 AND 25 THEN 1 ELSE 0 END ) AS Q1,
SUM(CASE WHEN Qtr BETWEEN 26 AND 50 THEN 1 ELSE 0 END ) AS Q2,
SUM(CASE WHEN Qtr BETWEEN 51 AND 75 THEN 1 ELSE 0 END ) AS Q3,
SUM(CASE WHEN Qtr BETWEEN 76 AND 100 THEN 1 ELSE 0 END ) AS Q4
FROM
YourTable;[/code]That is assuming that the second column name is Qtr.
Go to Top of Page

splash
Starting Member

2 Posts

Posted - 2012-01-04 : 11:29:59
Awesome, thanks! Is the BETWEEN condition inclusive?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-04 : 11:33:42
yes...look in books online

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -