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

  • Home
  • SEARCH
  • 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 4569772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:16:02+00:00 2026-05-21T19:16:02+00:00

I haven’t seen a question like this, but if there is one out there

  • 0

I haven’t seen a question like this, but if there is one out there that has been answered, please let me know.

I have to create an export, using a stored procedure. Unfortunately, at this time, creating this report in SSRS is not possible.

What I need to do is dynamically create a pivot table and union it to another – or that’s what I thought would work.

The raw data works similar to this (I’ve changed items to protect my company’s data):

Data Sample

What they want the data to look like in the report is this (To save space, I didn’t use all dates, but you can get the idea):
Report Sample

I’ve created a temporary table and created two dynamic pivot tables. Both tables will work separately, but once I use a UNION ALL, I receive an error message (I’ll add that below). I’m including the code I have used to create the two pivots. Can someone tell me what I’m doing wrong?

Is it possible to do this in just one pivot?

/*
    Use dynamic SQL to find all 
    Issue Dates for column headings
*/
DECLARE @Jquery VARCHAR(8000)
DECLARE @query VARCHAR(4000)
DECLARE @years VARCHAR(2000)
SELECT  @years = STUFF(( SELECT DISTINCT
                        '],[' + 'Item 1' + ' ' + (IssueDate)
                        FROM    #GroupData GroupData
                        ORDER BY '],[' + 'Item 1' + ' ' + (IssueDate)
                        FOR XML PATH('')
                        ), 1, 2, '') + ']'

SET @query =
'SELECT * FROM
(
    SELECT LocationID, StoreName, StoreState AS State, "Item 1" + " " + (IssueDate) AS IssueDate, MoneyOrder
    FROM #GroupData GroupData
) MoneyOrderIssued
PIVOT (MAX(MoneyOrder) FOR IssueDate
IN ('+@years+')) AS pvt'

DECLARE @queryMOUsed VARCHAR(4000)
DECLARE @MOUsedYear VARCHAR(2000)
SELECT  @MOUsedYear = STUFF(( SELECT DISTINCT
                        '],[' + 'Item 2' + ' ' + (IssueDate)
                        FROM    #GroupData GroupData
                        ORDER BY '],[' + 'Item 2' + ' ' + (IssueDate)
                        FOR XML PATH('')
                        ), 1, 2, '') + ']'

SET @queryMOUsed =
'SELECT * FROM
(
    SELECT LocationID, StoreName, StoreState AS State, "Item 2" + " " + (IssueDate) AS IssueDate, MOUsed
    FROM #GroupData GroupData
)SCRMoneyOrders
PIVOT (MAX(MOUsed) FOR IssueDate
IN ('+@MOUsedYear+')) AS pvt'

SET @Jquery = @query + ' UNION ALL ' +  @queryMOUsed


EXECUTE (@query) -- Only in here to show that this works w/out UNION ALL
EXECUTE (@queryMOUsed) -- Only in here to show that this works w/out UNION ALL

EXECUTE (@Jquery)

The error message I receive is the following:

Warning: Null value is eliminated by an aggregate or other SET operation.
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to bigint.

  • 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-21T19:16:03+00:00Added an answer on May 21, 2026 at 7:16 pm

    My idea is that the columns do not match (in number of columns, order of columns, and data type). If I’m reading your query correctly, if the issue dates for item1 and item2 do not match, you may get mismatched columns anyway. It is really hard to tell without seeing the output of those two queries.

    Are you sure you don’t want a JOIN based on store ID?

    Something like :

    WITH Item1Data as (
    --pivot query for item 1
    ),
    Item2Data as (
     --pivot query for item 2
    )
    SELECT columns
    FROM Item1DATA i1
    LEFT JOIN Item2Data i2
    ON i1.SoteID = i2.StoreID
    

    Here is a dynamic query I did. got the columns are data-generated:

     --Get string of aggregate columns for pivot.  The aggregate columns are the last 5 NRS Years.
                        DECLARE @aggcols NVARCHAR(MAX)
    
                        SELECT  @aggcols = STUFF(( SELECT   '],['
                                                            + CAST(ny2.NRS_YEAR AS CHAR(4))
                                                   FROM     mps.NRS_YEARS ny2
                                                   WHERE    ny2.NRS_YEAR BETWEEN @NRS_Year
                                                            - 5 AND @NRS_Year
                                                   ORDER BY '],['
                                                            + CAST(ny2.NRS_YEAR AS CHAR(4))
                                                 FOR
                                                   XML PATH('') ) , 1 , 2 , '')
                                + ']' ;
    
    --While we're at it, get a sum of each year column.  we'll do a union query instead of  rollup because that's how we roll.
    
                        DECLARE @sumcols NVARCHAR(MAX) ;
                        SELECT  @sumcols = STUFF(( SELECT   ']),sum(['
                                                            + CAST(ny2.NRS_YEAR AS CHAR(4))
                                                   FROM     mps.NRS_YEARS ny2
                                                   WHERE    ny2.NRS_YEAR BETWEEN @NRS_Year
                                                            - 5 AND @NRS_Year
                                                   ORDER BY ']),sum(['
                                                            + CAST(ny2.NRS_YEAR AS CHAR(4))
                                                 FOR
                                                   XML PATH('') ) , 1 , 3 , '')
                                + '])' ;
    
                        DECLARE @Query NVARCHAR(MAX) ;
    --Construct dynamic pivot query
    
                        SET @Query = N'SELECT MonthName as Month, ' + @aggcols
                            + N'
          into ##MonthHourPivot
    FROM
     (SELECT nc.MONTHNAME, nc.MonthOfNRS_Yr, nc.NRS_YEAR, st.Hours
     FROM mps.NRS_Calendar nc 
     INNER JOIN dbo.StudentTime st
     ON nc.Date = /*00:00:00 AM*/ DATEADD(dd, DATEDIFF(dd, 0, /*On*/ st.EntryDateTime), 0)
     LEFT JOIN mps.vw_ScheduleRoomBuilding srb
     ON st.ScheduleID = srb.ScheduleID
     WHERE (st.EntryDateTime <= GETDATE() and st.SiteCode = ''' + @SiteCode
                            + N''' or ''' + @SiteCode + N''' = ''All'')
     AND (srb.Abbreviation = ''' + @Building + N''' or ''' + @Building
                            + N''' = ''All'')) p
     PIVOT
     (
     sum(p.Hours)
     FOR NRS_Year IN
    ( ' + @aggcols + N' )
    ) AS pvt 
    ' ;
    
    --Execute It.
                        EXECUTE(@Query) ;
    
                        SET @Query = N'Select [Month], ' + @aggcols
                            + N'FROM ##MonthHourPivot UNION ALL SELECT ''Total'' as [Month], '
                            + @sumcols + ' FROM ##MonthHourPivot' ;
                         Execute (@Query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Haven't been able to find this one out. How are Bitmaps stored in memory
Haven't seen anything about it here but it seems to solve one of the
Haven't seen anything, but I have seen that you can create albums in the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Haven't seen this before so hopefully someone has a easy solve, I have a
Haven't seen many Geneva related questions yet, I have posted this question in the
Haven't seen this feature anywhere else. I know that the 32nd bit is used
I haven't used the STL much before, but I started to on this huffman
I haven't been able to get this working and all of the sample code
I haven't found information about this anywhere. Is there a minimal required length for

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.