连接多个动态数据透视表
我还没有看到这样的问题,但如果有已经得到解答的问题,请告诉我。
我必须使用存储过程创建导出。不幸的是,目前无法在 SSRS 中创建此报告。
我需要做的是动态创建一个数据透视表并将其合并到另一个数据透视表 - 或者这就是我认为可行的方法。
原始数据的工作方式与此类似(我已更改项目以保护我公司的数据):
他们想要什么报告中的数据如下(为了节省空间,我没有使用所有日期,但您可以理解):
我创建了一个临时表并创建了两个动态数据透视表。两个表都将单独工作,但是一旦我使用 UNION ALL,我就会收到一条错误消息(我将在下面添加该消息)。我包含了用于创建两个枢轴的代码。有人可以告诉我我做错了什么吗?
是否可以仅通过一个支点来完成此操作?
/*
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)
我收到的错误消息如下:
警告:聚合或其他 SET 操作消除了空值。 消息 8114,16 级,状态 5,第 1 行 将数据类型 varchar 转换为 bigint 时出错。
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):
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):
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的想法是列不匹配(列数、列顺序和数据类型)。如果我正确读取您的查询,如果 item1 和 item2 的发布日期不匹配,那么无论如何您都可能会得到不匹配的列。如果不看到这两个查询的输出,真的很难判断。
您确定不想要基于商店 ID 的 JOIN 吗?
类似于:
这是我所做的动态查询。得到的列是数据生成的:
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 :
Here is a dynamic query I did. got the columns are data-generated:
这似乎更适合使用 ETL 工具来完成。
我不太了解 Microsoft 工具集,但我认为他们的数据仓库包可以做到这一点。在 Pentaho 的“数据集成”(Kettle)中,您可以使用行反规范化步骤< /a>,或行扁平化步骤。
This seems like something more appropriately done with an ETL tool.
I don't know the Microsoft tool-set very well, but I assume their data-warehousing package has something for doing that. In Pentaho's "Data Integration" (Kettle), you'd use a row denormalizer step, or the row flattener step.