all groups > sql server (alternate) > january 2006 >
You're in the

sql server (alternate)

group:

Help wanted msaccess PIVOT-query --> MS-SQL view/sp



Help wanted msaccess PIVOT-query --> MS-SQL view/sp Gert v O
1/18/2006 7:49:49 PM
sql server (alternate): Can someone help me parsing this ms-access PIVOT sql-statement to a
ms-sql-server sql-statement?
Many thanks in advance

TRANSFORM Count(KlantenStops.id) AS AantalVanid
SELECT KlantenStops.Uitvoerder, KlantenStops.Klant
FROM KlantenStops
GROUP BY KlantenStops.Uitvoerder, KlantenStops.Klant
PIVOT DatePart("m",leverdatum,1,0) In
("1","2","3","4","5","6","7","8","9","10","11","12");

Re: Help wanted msaccess PIVOT-query --> MS-SQL view/sp Hugo Kornelis
1/18/2006 9:38:55 PM
[quoted text, click to view]

Hi Gert,

I'm not exactly sure how the Access PIVOT syntax works (including table
structure, sample data and expected results would have been a good idea;
check www.aspfaq.com/2006 for the best format to supply this info), but
the query below will do what I think the Access format does:

SELECT Uitvoerder, Klant,
COUNT(CASE WHEN DATEPART(month, leverdatum) = 1 THEN 'TelMe'
END) AS Januari,
COUNT(CASE WHEN DATEPART(month, leverdatum) = 2 THEN 'TelMe'
END) AS Februari,
.....,
COUNT(CASE WHEN DATEPART(month, leverdatum) = 12 THEN 'TelMe'
END) AS December
FROM KlantenStops
GROUP BY Uitvoerder, Klant

If you're using SQL Server 2005, you can also use the new PIVOT syntax.
You'll have to check Books Online for the details, though, as I haven't
had a chance to play with the new syntax yet.

--
Re: Help wanted msaccess PIVOT-query --> MS-SQL view/sp Gert v O
1/19/2006 11:01:10 PM
[quoted text, click to view]


Thanx Hugo

AddThis Social Bookmark Button