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
 Pivot table with two columns to pivot
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

subsoft
Starting Member

2 Posts

Posted - 08/20/2014 :  22:48:18  Show Profile  Reply with Quote
Hi

I have a table with this data structure (Before section)



I have the query below to generate date as in (After section)

But I would like to have (Desired section) view where start and end tasks are next to each other

Any help will be great

Here is the query
-----------------------------


SELECT 
       ID
      ,[Final] AS [Final Start]
      ,[Edit] as [Edit Start]
      ,[Proof] as [Proof Start]
      ,[Stage] as [Stage Start]
      ,[Marketing] as [Marketing Start]
      ,[Developer] as [Developer Start]
      ,[Promotion] as [Promotion Start]
     
      FROM 
        (SELECT ID,TaskName,convert(varchar,[TaskStartDate],103) AS [TaskStartDate] FROM #TempTask) a 
        PIVOT (max(TaskStartDate) FOR TaskName IN (
        
       [Final]
      ,[Edit]
      ,[Proof]
      ,[Stage]
      ,[Marketing]
      ,[Developer]
      ,[Promotion]

       )) AS pvt

Edited by - subsoft on 08/20/2014 22:49:23

khtan
In (Som, Ni, Yak)

Singapore
17658 Posts

Posted - 08/20/2014 :  23:01:16  Show Profile  Reply with Quote
you can use the CASE WHEN method to do the pivoting

SELECT ID,
       MAX(CASE WHEN TaskName = 'Final' THEN TaskStartDate END) as [Final Start],
       MAX(CASE WHEN TaskName = 'Final' THEN TaskEndDate END)   as [Final End],
       MAX(CASE WHEN TaskName = 'Edit' THEN TaskStartDate END) as [Edit Start],
       MAX(CASE WHEN TaskName = 'Edit' THEN TaskEndDate END)   as [Edit End],
       . . . 
FROM   #TempTask
GROUP BY ID



KH
Time is always against us


Edited by - khtan on 08/20/2014 23:01:41
Go to Top of Page

subsoft
Starting Member

2 Posts

Posted - 08/21/2014 :  05:00:19  Show Profile  Reply with Quote
Thank you Khtan that worked .... perfect
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.06 seconds. Powered By: Snitz Forums 2000