对于这个示例,我是否需要一个数据透视查询,或者它比这更简单吗?
我有一个表记录其处理的每个项目的特定作业运行的成功/失败。
一些测试数据和我的一些测试如下所示。
CREATE TABLE #TestData
(
ItemType VARCHAR(10),
Task VARCHAR(10),
Status INT,
BatchId INT)
INSERT INTO #TestData
SELECT 'A', 'D', 1, 5
UNION ALL
SELECT 'A', 'D', 1, 5
UNION ALL
SELECT 'A', 'D', 2, 5
UNION ALL
SELECT 'A', 'U', 1, 5
UNION ALL
SELECT 'A', 'U', 2, 5
UNION ALL
SELECT 'B', 'D', 1, 6
UNION ALL
SELECT 'B', 'D', 1, 6
UNION ALL
SELECT 'B', 'D', 2, 6
UNION ALL
SELECT 'B', 'U', 1, 6
SELECT * FROM #TestData
SELECT BatchId,
ItemType,
Task,
COUNT(CASE WHEN [status] = 1 THEN [status] ELSE 0 END) as [Success],
COUNT(CASE WHEN [status] = 2 THEN [status] ELSE 0 END) AS [Failed]
FROM #TestData
GROUP BY BatchId, ItemType, Task
ORDER BY BatchId
DROP TABLE #TestData
我希望在这里看到的是 BatchId、ItemType 和 Task 的每个组合都有一个不同的行,旁边是成功和失败的计数。
因此,对于上面发布的数据,我希望看到;
Batch ItemType Task Success Failed
5 A D 2 1
5 A U 1 1
6 B D 2 1
6 B U 1 0
我可以在不需要数据透视表的情况下执行此操作吗?非常感谢任何帮助。
I have a table that records the success/failure of a particular job run for each item it processes.
Some test data and a little of my testing is shown below.
CREATE TABLE #TestData
(
ItemType VARCHAR(10),
Task VARCHAR(10),
Status INT,
BatchId INT)
INSERT INTO #TestData
SELECT 'A', 'D', 1, 5
UNION ALL
SELECT 'A', 'D', 1, 5
UNION ALL
SELECT 'A', 'D', 2, 5
UNION ALL
SELECT 'A', 'U', 1, 5
UNION ALL
SELECT 'A', 'U', 2, 5
UNION ALL
SELECT 'B', 'D', 1, 6
UNION ALL
SELECT 'B', 'D', 1, 6
UNION ALL
SELECT 'B', 'D', 2, 6
UNION ALL
SELECT 'B', 'U', 1, 6
SELECT * FROM #TestData
SELECT BatchId,
ItemType,
Task,
COUNT(CASE WHEN [status] = 1 THEN [status] ELSE 0 END) as [Success],
COUNT(CASE WHEN [status] = 2 THEN [status] ELSE 0 END) AS [Failed]
FROM #TestData
GROUP BY BatchId, ItemType, Task
ORDER BY BatchId
DROP TABLE #TestData
What I would like to see here is a distinct row for each combination of BatchId, ItemType and Task and then an alongside that a count of the Success and Failures.
So for the data posted above, I'd expect to see;
Batch ItemType Task Success Failed
5 A D 2 1
5 A U 1 1
6 B D 2 1
6 B U 1 0
Can I do this without the need for a pivot table? Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来对你有用:
This looks like it will work for you: