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 2000 Forums
 SQL Server Development (2000)
 Query single col into several cols result set

Author  Topic 

yoavb
Starting Member

4 Posts

Posted - 2004-10-24 : 08:01:45
Hello,

I have a table with 1 column containing numeric values, and I need to create a query which displays the count of values for each set of conditions. For example, if the table contains the values 1,2,3,4,5 and 6, the output of the query should look like this:

L M H
-----------------------
3 2 1

(L are values below 4, M between 4 and 5, H is 6 and above)

Any suggestions?

TIA

Kristen
Test

22859 Posts

Posted - 2004-10-24 : 10:45:15
[code]
SELECT [L] = SUM(CASE WHEN MyColumn < 4 THEN 1 ELSE 0 END),
[M] = SUM(CASE WHEN MyColumn >= 4 AND MyColumn <= 5 THEN 1 ELSE 0 END),
[H] = SUM(CASE WHEN MyColumn >= 6 THEN 1 ELSE 0 END)
FROM MyTable
[/code]
Kristen
Go to Top of Page

yoavb
Starting Member

4 Posts

Posted - 2004-10-28 : 01:30:11
That works beautifully! thanks a lot.
Go to Top of Page
   

- Advertisement -