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 2005 Forums
 Transact-SQL (2005)
 retrieve result based on language exsistance

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2008-12-16 : 04:07:50
Hi

I have a table that looks like this...

ID Partno Lang
1 1234 GB
2 1234 ES
3 1234 FI
4 4567 ES
5 4567 SE
6 7889 GB


Is it possible to retrieve a result like this..

Partno Lang_GB Lang_ES Lang_FI Lang_SE
1234 1 1 1 0
4567 0 1 0 1
789 1 0 0 0


Best Regards

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-16 : 04:29:12
Read about PIVOT operator in sql server help file

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-16 : 04:37:10
[code]
SELECT Partno,
SUM(CASE WHEN Lang='GB' THEN 1 ELSE 0 END) AS Lang_GB,
SUM(CASE WHEN Lang='ES' THEN 1 ELSE 0 END) AS Lang_ES,
SUM(CASE WHEN Lang='FI' THEN 1 ELSE 0 END) AS Lang_FI,
SUM(CASE WHEN Lang='SE' THEN 1 ELSE 0 END) AS Lang_SE
FROM table
GROUP BY Partno
[/code]
Go to Top of Page

magmo
Aged Yak Warrior

558 Posts

Posted - 2008-12-16 : 04:43:00
Excellent, just what I was looking for, Thanks !
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-16 : 04:46:06
Welcome
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-16 : 04:54:00
SELECT partno,GB as 'lang_gb',ES as 'lang_es',FI as 'lang_fi',SE as 'lang_se'
from ( select * from string ) as i
pivot ( count(id) for lang in ( GB,ES,FI,SE)) as p

I Struggle For Excellence
Go to Top of Page
   

- Advertisement -