gpt4 book ai didi

sql - 无法形成 SQL 连接查询

转载 作者:行者123 更新时间:2023-11-29 14:08:56 24 4
gpt4 key购买 nike

我是 SQL 的新手,正在尝试编写查询来查找类似以下场景的逻辑:

wk_id | start_date | end_date | status

----- ---------- ------ ------
1 20160101 20160101 SUCCEEDED
2 20160101 20160101 SUCCEEDED
3 20160101 20160101 SUCCEEDED
4 20160101 20160101 SUCCEEDED
5 20160101 20160101 FAILED
6 20160102 20160102 SUCCEEDED
7 20160102 20160102 SUCCEEDED
8 20160102 20160102 FAILED
9 20160102 20160102 FAILED
10 20160101 20160101 FAILED
11 20160101 20160101 FAILED

所以在这 3 天里,提交了一些作品,一些失败了,一些成功了。

我想编写一个查询,返回每天的作品总数、成功的作品和失败的作品。

我肯定做错了,但这就是我的想法。

请指出错误并说明逻辑。

select 
w1.end_date,
w1.status,COUNT(*) as total_instances,
COUNT(*) as as succeeded_instances,
COUNT(*) as as failed_instances
from
work_instances w1 ,
work_instances w2
where
w1.status LIKE 'SUCCEEDED'
and w2.status NOT LIKE 'SUCCEEDED'
and w1.wk_id = w2.wk_id
group by
w1.end_date,w1.status;

最佳答案

在什么情况下使用聚合函数

  select 
w1.end_date,COUNT(*) AS total_work,
sum(case when w1.status='SUCCEEDED' then 1 else 0 end ) as SUCCEEDED_task,
sum(case when w1.status='FAILED' then 1 else 0 end ) as FAILED_task
from
work_instances w1 group by w1.end_date

关于sql - 无法形成 SQL 连接查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com