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
 General SQL Server Forums
 New to SQL Server Programming
 Help need with old school pivot

Author  Topic 

lemondash
Posting Yak Master

159 Posts

Posted - 2009-11-26 : 07:33:12
Hello all,

I'm current using the below soloution/sql query to get a list of all the document types and how many they are. Each row has entered date. How can i put the latest entered date beside every different document type.


SELECT DATEPART(YEAR, enteredon),
sum(case when ldocumenttypeid = 1 then 1
else 0
end) AS [doctype1],
sum(case when ldocumenttypeid = 2 then 1
else 0
end) AS [doctype2],
sum(case when ldocumenttypeid = 3 then 1
else 0
end) AS [doctype3]

FROM table1
GROUP BY DATEPART(YEAR, enteredon)
ORDER BY DATEPART(YEAR, enteredon)

I would like to get the output as follows.

[doctype1],enteredon, [doctype2], enteredon, [doctype3], enteredon

any help would be great

shaggy
Posting Yak Master

248 Posts

Posted - 2009-11-26 : 08:02:36
Use Pivot
Go to Top of Page

lemondash
Posting Yak Master

159 Posts

Posted - 2009-11-26 : 08:48:04
Would that still be possible with a sql server 2000 (8.0)database ?
Go to Top of Page
   

- Advertisement -