• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

SQL Pivot table

I am working in SQL Server 2008 and trying to pivot some data.  SQL is telling me that I have incorrect syntax near 'NetWorth'.  I'm not seeing it.  Does anyone out there see it?  When I execute the code without the PIVOT the select does work.

Thanks in advance
PIVOT.txt
0
RichNH
Asked:
RichNH
  • 2
  • 2
1 Solution
 
Barry CunneyCommented:
Wrap the 'NetWorth' and all the other entries in [] square brackets instead of single quotes
0
 
RichNHAuthor Commented:
Thank you BCUNNEY, that fixed part of it.  Apparently you also need to reference the fields from the pivot instead of fields from the table *RJZ_Financials because it wasn't recognizing the fields I had in the select.  But when I referenced the PVT fields, that worked.  

However there is still one field it doesn't recognize and I can't figure out why, FD.VariableName is in the same set as FD.ValueUSD but the editor isn't recognizing it as a valid field.

In this code:
MAX(FD.ValueUSD) FOR FD.VariableName IN  
it accepts FD.ValueUSD but doesn't accept FD.VariableName.

The FD.VariableName has a red squiggly line under it.  What am I doing wrong?

I have reattached the code with the corrections so far.
PIVOT.txt
0
 
Barry CunneyCommented:
See if you can first get the INNER query(FD) and the OUTER APPLY part working on it's own - forget about the PIVOT for the moment - like see if you can build the OUTER apply into the FD query set first and then if this works do the pivoting afterwards in the outer query
0
 
RichNHAuthor Commented:
As it is, I got the whole thing to work,  sorry for the delay, month end you know.  Here's your points and thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now