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
 Transact-SQL (2000)
 SQL query

Author  Topic 

madhulatha_b
Starting Member

22 Posts

Posted - 2004-10-01 : 04:18:47
Hi,

I need to write a query to get the number of rows of a category as well total number of rows in that table in a single row. For ex table contains 3 categories like cat1,cat2,cat3. Cat1 contains 2 rows, cat2 contains 3 and cat3 contains 4. The result should be in single row

Result
--------
2,9

Can any body help me to get this result

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-10-01 : 04:51:01
how did you get 2?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-10-01 : 05:07:37
show more detailed sample data...and matching expected results....
the description given is too vague.......
Go to Top of Page

madhulatha_b
Starting Member

22 Posts

Posted - 2004-10-01 : 05:09:30
2 is total number of Cat1 rows
9 is total number of rows in table

That table contains 2 rows with cat1, 3 rows with cat2 and 4 rows with cat3
Go to Top of Page

Shurgenz
Yak Posting Veteran

51 Posts

Posted - 2004-10-01 : 05:10:38
by other words: 2-rows of cat1, 9-total rows of table. Correct?
Go to Top of Page

Shurgenz
Yak Posting Veteran

51 Posts

Posted - 2004-10-01 : 05:12:07
select sum(case when f=cat1 then 1 else 0 end), count(*) from t

t-your table
f-cat field
Go to Top of Page

madhulatha_b
Starting Member

22 Posts

Posted - 2004-10-01 : 05:20:10
Suppose there is a table called CLASS with a column called NAME with
data as below,

NAME
---------
CAR
CAR
SCOOTER
SCOOTER
AEROPLANE
BIKE


Now I want with a single SQL query the number of entries with value
SCOOTER and the total number of entries.
i.e., I expect to get the values 2,6

How do I write such a query?

Hope I am clear.
Go to Top of Page

Shurgenz
Yak Posting Veteran

51 Posts

Posted - 2004-10-01 : 05:21:32
select sum(case when name='scooter' then 1 else 0 end), count(*) from class
Go to Top of Page

madhulatha_b
Starting Member

22 Posts

Posted - 2004-10-01 : 05:38:31
Thanks for your reply. It's working
Go to Top of Page
   

- Advertisement -