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 2008 Forums
 Transact-SQL (2008)
 SQL QUERY SYNTAX

Author  Topic 

vipinjha123
Starting Member

45 Posts

Posted - 2012-05-09 : 02:30:48
Dear All,

i have a data in below format in sql

Project pass month
WSBS-AR 85 MAR-12
WSBS-Interface 74 MAR-12
WSBS-Payments 88 MAR-12
WSBS-AR 92 FEB-12
WSBS-Interface 85 FEB-12
WSBS-Payments 65 FEB-12
WSBS-AR 45 DEC-11
WSBS-Interface 25 DEC-11
WSBS-Payments 65 DEC-11

i want to show the data in below format

Project mar-12 feb-12 11-Dec
WSBS-AR 85 92 45
WSBS-Interface 74 85 25
WSBS-Payments 88 65 65

so plesae provide the correct sql synatx as i am trying for last 2 days.

thankx & regards,

Vipin jha

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 07:16:06
This needs dynamic pivoting .Go through the below link.



http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-09 : 08:09:25
This will do it:


--Creating Table

Create Table Ex
(Project varchar(30),
pass int,
month varchar(30) )


--Inserting Sample Data

Insert Into Ex
Select 'WSBS-AR', 85, 'MAR-12'
Union ALL
Select 'WSBS-Interface', 74, 'MAR-12'
Union ALL
Select 'WSBS-Payments', 88, 'MAR-12'
Union ALL
Select 'WSBS-AR', 92, 'FEB-12'
Union ALL
Select 'WSBS-Interface', 85, 'FEB-12'
Union ALL
Select 'WSBS-Payments', 65, 'FEB-12'
Union ALL
Select 'WSBS-AR', 45, 'DEC-11'
Union ALL
Select 'WSBS-Interface', 25, 'DEC-11'
Union ALL
Select 'WSBS-Payments', 65, 'DEC-11'


--Query for your requirement

;With CTE
As
(
Select Project,
(Case When month = 'MAR-12' Then SUM(pass) Else NULL End) As MAR_12,
(Case When month = 'FEB-12' Then SUM(pass) Else NULL End) As FEB_12,
(Case When month = 'DEC-11' Then SUM(pass) Else NULL End) As DEC_11
From Ex
Group By Project, month)
Select a.Project, a.MAR_12, b.FEB_12, c.DEC_11 From CTE As a
JOIN CTE as b ON a.Project = b.Project
JOIN CTE as c ON a.Project = c.Project
Where a.MAR_12 IS NOT NULL AND b.FEB_12 IS NOT NULL AND c.DEC_11 IS NOT NULL


N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-09 : 16:24:48
No need of CTE and all those joins. simply this would do


Select Project,
SUM(Case When month = 'MAR-12' Then pass Else 0 End) As MAR_12,
SUM(Case When month = 'FEB-12' Then pass Else 0 End) As FEB_12,
SUM(Case When month = 'DEC-11' Then pass Else 0 End) As DEC_11
From Ex
Group By Project


and if you want to make it dynamic based on month value you need to use like this

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

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-09 : 20:45:46
[code]One more way to do



SELECT PROJECT ,[DEC-11],[FEB-12],[MAR-12]
FROM ( SELECT PROJECT, PASS,[MONTH] FROM #Ex) AS A
PIVOT ( SUM(PASS) FOR [MONTH] IN ( [DEC-11],[FEB-12],[MAR-12]))AS PVT[/code]
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-10 : 01:34:40
quote:
Originally posted by visakh16

No need of CTE and all those joins. simply this would do


Select Project,
SUM(Case When month = 'MAR-12' Then pass Else 0 End) As MAR_12,
SUM(Case When month = 'FEB-12' Then pass Else 0 End) As FEB_12,
SUM(Case When month = 'DEC-11' Then pass Else 0 End) As DEC_11
From Ex
Group By Project


and if you want to make it dynamic based on month value you need to use like this

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

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





OOPS!!....So yumbarrassing....
How did I miss that??...Good one Visakh.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-10 : 12:13:42
quote:
Originally posted by vinu.vijayan

quote:
Originally posted by visakh16

No need of CTE and all those joins. simply this would do


Select Project,
SUM(Case When month = 'MAR-12' Then pass Else 0 End) As MAR_12,
SUM(Case When month = 'FEB-12' Then pass Else 0 End) As FEB_12,
SUM(Case When month = 'DEC-11' Then pass Else 0 End) As DEC_11
From Ex
Group By Project


and if you want to make it dynamic based on month value you need to use like this

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

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





OOPS!!....So yumbarrassing....
How did I miss that??...Good one Visakh.

N 28° 33' 11.93148"
E 77° 14' 33.66384"


tnx

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

Go to Top of Page
   

- Advertisement -