Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:03:40+00:00 2026-05-27T17:03:40+00:00

I’m pretty sure I need a pivot to do this but can’t quite figure

  • 0

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…

  1. The there is two records for CompanyID 0003 I’d like it to group into one record

  2. 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 P1
    

    Where @CompanyIDs is 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. 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:03:41+00:00Added an answer on May 27, 2026 at 5:03 pm

    Try this:

    DECLARE @CompanyIDs XML
    DECLARE @Records  TABLE
    (
        ID int,
        CompanyID char(4),
        Year int,
        Revenue decimal,
        Expenses decimal
    )
    DECLARE @CompanyDetails TABLE
    (
        CompanyID char(4),
        Name varchar(50),
        Address varchar(50)
    )
    
    SET @CompanyIDs = '
    <filter>
        <CompanyID>0003</CompanyID>
        <CompanyID>0010</CompanyID>
    </filter>'
    
    INSERT INTO @Records 
    (ID, CompanyID, Year, Revenue, Expenses)VALUES 
    (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)
    
    INSERT INTO @CompanyDetails 
    (CompanyID, Name, Address) VALUES
    ('0003', 'Company A', 'A Street'),
    ('0010', 'Company B', 'B Street')
    
    SELECT *
    FROM 
    (
        SELECT  CompanyID, CAST(YEAR AS varchar) + ' - Revenue' Type,
                Revenue Value FROM @Records 
        UNION ALL
        SELECT  CompanyID, CAST(YEAR AS varchar) + ' - Expenses' Type,
                Expenses  Value FROM @Records 
    ) src
    PIVOT
    (
        SUM(Value) for [Type] in
        ([2011 - Revenue], [2010 - Revenue], [2009 - Revenue],
         [2011 - Expenses], [2010 - Expenses], [2009 - Expenses]
        )
    ) pvt
    JOIN    @CompanyDetails Details
        ON  Details.CompanyId = pvt.CompanyID
    WHERE   pvt.CompanyID IN
    (
        SELECT  T.C.value('.', 'char(4)')
        FROM    @CompanyIDs.nodes('/filter/CompanyID') T(C)
    )
    

    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 pvt output

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.