SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count of distinct types
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

emailuser
Yak Posting Veteran

74 Posts

Posted - 02/07/2013 :  12:29:08  Show Profile  Reply with Quote
Hi everyone , i think this is a fairly easy sql script but i cannot work it out , i have data in a table in the format

Dept Type

IT a
Finance a
IT a
Sales b
Sales c
Finance c
IT b

and need an output :

Dept Type a Type b Type c
IT 2 1 0
Sales 0 1 1
Finance 1 0 1

Any help greatly appreciated

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/07/2013 :  16:19:24  Show Profile  Reply with Quote
SELECT
	Dept,
	a AS [Type a],
	b AS [Type b],
	c AS [Type c]
FROM
	TheTable
PIVOT (COUNT([type]) FOR [Type] IN ([a],[b],[c]))P;
Go to Top of Page

emailuser
Yak Posting Veteran

74 Posts

Posted - 02/08/2013 :  05:29:56  Show Profile  Reply with Quote
Hi James, many thanks for your reply , will this work for SQL2000 ? .. tried it but got errors
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52319 Posts

Posted - 02/08/2013 :  05:37:18  Show Profile  Reply with Quote
quote:
Originally posted by emailuser

Hi James, many thanks for your reply , will this work for SQL2000 ? .. tried it but got errors


Nope PIVOT is available only from 2005 onwards

for 2000 you need to use


SELECT Dept,
SUM(CASE WHEN Type='a' THEN 1 ELSE 0 END) AS [Type a],
SUM(CASE WHEN Type='b' THEN 1 ELSE 0 END) AS [Type b],
SUM(CASE WHEN Type='c' THEN 1 ELSE 0 END) AS [Type c]
FROM Table
GROUP BY Dept


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 02/08/2013 :  06:40:26  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
This is for dynamic number of types http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52319 Posts

Posted - 02/08/2013 :  07:08:20  Show Profile  Reply with Quote
quote:
Originally posted by madhivanan

This is for dynamic number of types http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail


OP is still on 2000
so i think this is what he should be using for dynamic crosstabbing

http://beyondrelational.com/modules/2/blogs/70/posts/10791/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 02/08/2013 :  07:16:50  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by madhivanan

This is for dynamic number of types http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail


OP is still on 2000
so i think this is what he should be using for dynamic crosstabbing

http://beyondrelational.com/modules/2/blogs/70/posts/10791/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Thanks. Then the link you posted is correct

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

emailuser
Yak Posting Veteran

74 Posts

Posted - 02/13/2013 :  11:32:26  Show Profile  Reply with Quote
thanks everyone and to visakh16 for the solution .. just upgraded to sql2005 over weekend so a few more options now :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52319 Posts

Posted - 02/13/2013 :  11:33:59  Show Profile  Reply with Quote
ok...cool


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000