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)
 Select Distinct and count....help

Author  Topic 

micrapip
Starting Member

11 Posts

Posted - 2008-12-02 : 12:54:53
Hi I haven't done SQL in about 4-5 years so I'm a bit rusty...

Have the following table...

transit type
00018 Laptop
00018 Laptop
00018 Desktop
00025 Laptop
00025 Desktop

I can do a select destinct that will show the following

transit
00018
00025

What I'm looking for is select distinct transit and count the type = 'Laptop' so the output looks like this...

Transit Count
00018 2
00025 1

Can someone help me out?

this is all from one table so no need for joins etc

thanks

Micrapip

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-12-02 : 12:57:18
Can you please show what you tried?
Go to Top of Page

micrapip
Starting Member

11 Posts

Posted - 2008-12-02 : 13:15:12
Sure... but I know it's not right because I get errors...

select distinct [BTR Transit list] from AMDB, select COUNT([Category_class]) from AMDB where [Category_class] = 'Laptop' and [BTR Transit list] = '00018'

Doesn't like the secont select statement, when I change it to

select distinct [BTR Transit list] from AMDB and Select COUNT([Category_class]) from AMDB where [Category_class] = 'Laptop' and [BTR Transit list] = '00018'

Still doesn't like the Select Stmt

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-02 : 13:20:50
quote:
Originally posted by micrapip

Hi I haven't done SQL in about 4-5 years so I'm a bit rusty...

Have the following table...

transit type
00018 Laptop
00018 Laptop
00018 Desktop
00025 Laptop
00025 Desktop

I can do a select destinct that will show the following

transit
00018
00025

What I'm looking for is select distinct transit and count the type = 'Laptop' so the output looks like this...

Transit Count
00018 2
00025 1

Can someone help me out?

this is all from one table so no need for joins etc

thanks

Micrapip






SELECT transit,SUM(CASE WHEN type='Laptop' THEN 1 ELSE 0 END) AS Count
FROM Table
GROUP BY transit
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-02 : 13:23:19
or if you want to use count


SELECT transit,COUNT(CASE WHEN type='Laptop' THEN type ELSE NULL END) AS Count
FROM Table
GROUP BY transit
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-12-02 : 14:17:56
and if you want to get a complete overview:
create table #test(
transit char(5),
[type] varchar(25)
)
insert #test
select '00018', 'Laptop' union all
select '00018', 'Laptop' union all
select '00018', 'Desktop' union all
select '00025', 'Laptop' union all
select '00025', 'Desktop'

select
transit,
[type],
count(*) as counter
from #test
group by transit,[type]
order by transit,[type]

-- gives you:
-- 00018 Desktop 1
-- 00018 Laptop 2
-- 00025 Desktop 1
-- 00025 Laptop 1
drop table #test


Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-12-02 : 16:39:19
the answer to your original question is just this:


select transit, count(*) as [count] from #test where type = 'laptop' group by transit


transit count
------- -----------
00018 2
00025 1


Be One with the Optimizer
TG
Go to Top of Page

micrapip
Starting Member

11 Posts

Posted - 2008-12-03 : 08:26:34
Thanks ALL,

This site rocks!!!
Go to Top of Page
   

- Advertisement -