943,634 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1288
  • MS SQL RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 1st, 2009
0

Re: Crazy CrossTab problems

Here the solution that I got from another forum:

MS SQL Syntax (Toggle Plain Text)
  1. SELECT ROW_NUMBER() OVER (PARTITION BY workID ORDER BY machineID) AS Seq,* INTO #Temp
  2. FROM TABLE
  3.  
  4. DECLARE @MachineList varchar(1000),@Sql varchar(max)
  5.  
  6. SELECT @MachineList= LEFT(ml.MacList,LEN(ml.MacList)-1)
  7. FROM (SELECT DISTINCT CAST(Seq AS varchar(10)) + ','
  8. FROM #Temp
  9. FOR XML PATH('')
  10. )ml(MacList)
  11.  
  12. SET @Sql='SELECT *
  13. FROM (SELECT *
  14. FROM #Temp
  15. )t
  16. PIVOT (SUM(hours) FOR Seq IN (['+ REPLACE(@MachineList,',','],[') + ']))p'
  17. EXEC(@Sql)
  18. DROP TABLE #Temp
Reputation Points: 10
Solved Threads: 0
Newbie Poster
muek is offline Offline
9 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Need help in getting Average using SQL Cross Tab
Next Thread in MS SQL Forum Timeline: Get last month from timestamp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC