对于这个示例,我是否需要一个数据透视查询,或者它比这更简单吗?

发布于 2024-12-09 07:50:09 字数 1158 浏览 0 评论 0原文

我有一个表记录其处理的每个项目的特定作业运行的成功/失败。

一些测试数据和我的一些测试如下所示。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-12-16 07:50:09

这看起来对你有用:

SELECT BatchId, 
       ItemType, 
       Task
       ,sum(CASE WHEN [status] = 1 THEN 1 ELSE 0 END) as [Success]
       ,sum(CASE WHEN [status] = 2 THEN 1 ELSE 0 END) AS [Failed]
FROM #TestData
GROUP BY BatchId, ItemType, Task
ORDER BY BatchId

This looks like it will work for you:

SELECT BatchId, 
       ItemType, 
       Task
       ,sum(CASE WHEN [status] = 1 THEN 1 ELSE 0 END) as [Success]
       ,sum(CASE WHEN [status] = 2 THEN 1 ELSE 0 END) AS [Failed]
FROM #TestData
GROUP BY BatchId, ItemType, Task
ORDER BY BatchId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文