Pivot Table in SQL Server vs. Access

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Pivot Table in SQL Server vs. Access

Post by kwvh »

I have been using a "Crosstab" query in Access for quite some time. Have now moved the data to SQL Server, and want to use SQL Server to create the crosstab/Pivot query. Below is my SQL Statement in SQL Server that gives me an error, followed by the SQL Statement from Access that works correctly:

Code: Select all

SELECT strUnique as RefNum ,dtmAsOf, curChangeInAllocation AS [Total ITD Allocation]
FROM 
(Select strUnique, dtmAsOf ,curChangeInAllocation
from tblCAChangeInAllocation) Chng
  
PIVOT
( 
Sum(curChangeInAllocation) 
)as pvt

Code: Select all

TRANSFORM Sum(tblChangeInAllocation.curChangeInAllocation) AS SumOfChangeInAllocation
SELECT tblChangeInAllocation.strUnique, Sum(tblChangeInAllocation.curChangeInAllocation) AS [Total ITD Allocation]
FROM tblChangeInAllocation
GROUP BY tblChangeInAllocation.strUnique
PIVOT tblChangeInAllocation.dtmAsOf;
Is there anything obviously apparent that I am missing? A closing or opening parenthesis, or a bracket or key word?

Any help is GREATLY appreciated.

Respectfully,

Ken

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pivot Table in SQL Server vs. Access

Post by HansV »

Which version of SQL Server are you using?
Best wishes,
Hans

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Re: Pivot Table in SQL Server vs. Access

Post by kwvh »

SQL Server 2008 R2

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pivot Table in SQL Server vs. Access

Post by HansV »

OK, so pivot should be available. I can't help you with the syntax, though, I hope that someone who knows SQL Server will have a suggestion for you.
Best wishes,
Hans

User avatar
Wendell
4StarLounger
Posts: 482
Joined: 24 Jan 2010, 15:02
Location: Colorado, USA

Re: Pivot Table in SQL Server vs. Access

Post by Wendell »

To be honest, I continue to use the Access Crosstab query with SQL Server linked tables, rather than trying to sort my way though usint the PIVOT statement in SQL Server. But if that isn't an option, it appears to me that your basic syntax is correct, but the examples I see always have an IN() clause the enumerates the categories that are to be displayed as column headers. There are numerous tutorials and examples available but maybe this one will give you some further clues. It would also help to see some sample data that you are trying to analyze.
Wendell
You can't see the view if you don't climb the mountain!

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Re: Pivot Table in SQL Server vs. Access

Post by kwvh »

Awesome Wendell! Thanks!