all groups > sql server new users > january 2007 >
You're in the

sql server new users

group:

"crosstab" ??


"crosstab" ?? R
1/18/2007 11:24:37 AM
sql server new users: I need to create the equivalent to an Access crosstab query in SQL. Can
someone help me with this? I'm new to SQL and need this report within the
hour, unfortunately.

The data table has four columns: Code, Value, Month, Year. There are 12
records per Code representing each month of the year. (I will filter for
the current year only.)

I need to create a query/view that will show one record per Code, like this:

Code Jan_Val Feb_Val Mar_Val Apr_Val etc.

I'm very grateful for some quick help with this!


Re: "crosstab" ?? Steve
1/18/2007 1:00:02 PM

[quoted text, click to view]

Search this forum or other SQL Server forumns for crosstab or pivot.
there are already many examples.
Re: "crosstab" ?? George Nicholson
1/18/2007 3:55:15 PM
One approach:
(SQLSrv 2005 Compatibility level 90 required)

USE MyDB;
GOSELECT Code, [Jan] AS Jan_Val, [Feb] AS Feb_ValFROM (SELECT Code, Month,
ValueFROM MyTable) pPIVOT(MAX (Value)FOR Month IN( [Jan],[Feb] )) AS
pvtORDER BY CodeWHERE Year = 2006This assumes that your Month field contains
the values Jan, Feb, etc. Obviously, the above would need to be expanded to
accomodate additional months.



[quoted text, click to view]

Re: "crosstab" ?? Steve Dassin
1/19/2007 12:33:51 PM
[quoted text, click to view]

Check out RAC. Similiar in concept but much more powerful.

www.rac4sql.net

AddThis Social Bookmark Button