I’m pretty sure I need a pivot to do this but can’t quite figure it out. (sql newb)
I have data like this:
ID CompanyID Year Revenue Expenses
1 0003 2011 12000 4000
2 0003 2010 9000 6000
3 0003 2009 7000 9000
4 0010 2011 134300 34000
5 0010 2010 43000 46000
6 0010 2009 73000 39000
Can I use a Pivot to display this table like this:
CompanyID 2011-Revenue 2010-Revenue 2009-Revenue 2011-Expenses 2010-Expenses 2009-Expenses
0003 12000 9000 7000 4000 6000 9000
0010 134300 43000 73000 34000 46000 39000
Here is what I have so far…
SELECT P1.*
FROM (SELECT [CompanyID]
,CASE P.[Year] WHEN 2011 THEN P.[Revenue] ELSE NULL END AS '2011-Revenue'
,CASE P.[Year] WHEN 2010 THEN P.[Revenue] ELSE NULL END AS '2010-Revenue'
FROM tblRecords P WHERE P.[CompanyID] = @companyID GROUP BY CompanyID, [Year], [Revenue]) AS P1
Which is returning:
CompanyID 2011-Revenue 2010-Revenue
0003 12000 NULL
0003 NULL 9000
Few problems with my results…
-
The there is two records for CompanyID 0003 I’d like it to group into one record
-
I can only choose 1 company at a time, I need to choose multiple. I tried
FROM tblRecords P WHERE P.[CompanyID] IN (@CompanyIDs) GROUP BY CompanyID, [Year], [Revenue]) AS P1Where
@CompanyIDsis a string like'0003, 0010'– It didn’t fail but the result was just an empty table with the headers and no data..
Any help would be appreciated.. or let me know if I’m misunderstanding pivot?
Thanks a lot!
Thomas
EDIT: Using Microsoft SQL Server Management Studio 2005 Express
UPDATE 2: I’ve figured out joining tables for more details however I still need to be able to pass in the the CompanyIDs as a comma delimited string.. any help on that would be appreciated.
vvvvvvvv I’VE FIGURED OUT BELOW THIS (will post once all is working) vvvvvvv
UPDATE: It looks like what Ruben has proposed is going to work however I’ve just determined I need a bit more functionality to this… Can I join this with another table to have the headers
CompanyID CompanyName CompanyAddress 2011-Revenue 2010-Revenue
Where CompanyName and CompanyAddress come from another table (tblCompanyDetails)
I’ve tried using:
SELECT *
FROM
(
SELECT CompanyID, tblCompanyDetails.CompanyName, tblCompanyDetails.CompanyAddress, CAST(YEAR AS varchar) + ' - Revenue' Type,
Revenue Value FROM tblRecords
FROM tblRecords INNER JOIN tblCompanyDetails ON tblRecords.CompanyID = tblCompanyDetails.CompanyID
UNION ALL
SELECT CompanyID, tblCompanyDetails.CompanyName, tblCompanyDetails.CompanyAddress, CAST(YEAR AS varchar) + ' - Expenses' Type,
Expenses Value FROM tblRecords
FROM tblRecords INNER JOIN tblCompanyDetails ON tblRecords.CompanyID = tblCompanyDetails.CompanyID
) src
PIVOT
(
SUM(Value) for [Type] in
([2011 - Revenue], [2010 - Revenue], [2009 - Revenue],
[2011 - Expenses], [2010 - Expenses], [2009 - Expenses]
)
) pvt
WHERE CompanyID = @CompanyID
I get the error:
Msg 1038, Level 15, State 4, Procedure spCompare, Line 10
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
Try this:
You need to send your company filter as XML and use that subselect to break your data as required for pivot operations. For JOIN operations, just use
pvtoutput