I have not seen such a question, but if there is an answer, let me know.
I need to create an export using a stored procedure. Unfortunately, creating this report in SSRS is not currently possible.
I need to dynamically create a pivot table and merge it into another - or what I thought would work.
Raw data works the same way (I changed the elements to protect my company data):

What they want the data to look in the report is (to save space, I did not use all the dates, but you can get this idea): 
I created a temporary table and created two dynamic pivot tables. Both tables will work separately, but as soon as I use UNION ALL, I get an error message (I will add this below). I include the code that I used to create the two anchor points. Can someone tell me what I'm doing wrong?
Can this be done with just one stronghold?
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 get is the following:
Warning. Null is excluded by an aggregate or other SET operation. Msg 8114, Level 16, State 5, Line 1 Error converting varchar data type to bigint.