Return 在分组依据中计数 0
Return Count 0 in group by
我已经 table 调用了 Errorlogs
并提供了以下数据
+----+-------------+------------+
| ID | sps_Bereich | Date |
+----+-------------+------------+
| 10 | helpeu1 | 2020-07-31 |
+----+-------------+------------+
| 11 | helpeu2 | 2020-07-31 |
+----+-------------+------------+
| 12 | helpeu3 | 2020-07-31 |
+----+-------------+------------+
| 13 | helpeu3 | 2020-07-31 |
+----+-------------+------------+
| 14 | helpeu4 | 2020-07-31 |
+----+-------------+------------+
| 15 | helpeu3 | 2020-07-30 |
+----+-------------+------------+
| 16 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
| 17 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
| 18 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
我还有一个 table 叫 Bereich
+----+---------+
| ID | Name |
+----+---------+
| 1 | helpeu1 |
+----+---------+
| 2 | helpeu2 |
+----+---------+
| 3 | helpeu3 |
+----+---------+
| 4 | helpeu4 |
+----+---------+
我想按 sps_bereich 和日期分组,我正在使用以下查询
select sps_bereich, [Date], count(*) as [Nr of errors] from [ErrorLogs]
where SPS_Bereich IN (
select Name from [Bereich]
)
group by SPS_Bereich, Date
order by date desc, SPS_Bereich asc
它给了我以下输出。
+-------------+------------+--------------+
| sps_bereich | Date | Nr of errors |
+-------------+------------+--------------+
| helpeu1 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-31 | 2 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-30 | 1 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-30 | 3 |
+-------------+------------+--------------+
我想要的是在没有数据的地方包含 sps_bereich
。
在这种情况下,我想将 helpeu1,helpeu2
添加为 0
日期 2020-07-30
我想要以下输出。
+-------------+------------+--------------+
| sps_bereich | Date | Nr of errors |
+-------------+------------+--------------+
| helpeu1 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-31 | 2 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu1 | 2020-07-30 | 0 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-30 | 0 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-30 | 1 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-30 | 3 |
+-------------+------------+--------------+
我在想添加一个 IN 子句必须 return 我的所有值,但事实并非如此。
这里是 SQLFiddle.
使用左连接 -
select A.Name, A.date,coalesce(B.cnt,0) as no_of_errors
from
(select distinct date,name from Errorlogs cross apply Bereich)A
left join
(select sps_Bereich, [Date],count(*) cnt
from Errorlogs group by sps_Bereich, [Date]
)B
on A.date=B.date and A.Name=B.sps_Bereich order by A.date desc, A.name asc
试试这个。
它构建一个完整的姓名和日期网格,然后用计数填充它。
select grid.name,grid.date,coalesce(cnt.cnt,0) from
(select distinct e.date, d.name from errorlogs e, Bereich d) grid
left outer join
(
select b.sps_Bereich, b.date, count(*) as cnt from [ErrorLogs] b
group by b.sps_Bereich,b.date
) cnt
on grid.date=cnt.date and grid.name=cnt.sps_bereich
我建议将查询写成:
select b.name, d.date, count(el.id) as num_errors
from Bereich b cross join
(select distinct date from errorlogs) d left join
errorlogs el
on el.date = d.date and el.sps_Bereich = b.name
group by b.name, d.date
order by d.date desc, b.name;
这个想法很简单。使用 cross join
在结果集中生成您想要的行。然后用left join
把已有的数据带进来
Here 是一个 db<>fiddle.
我已经 table 调用了 Errorlogs
并提供了以下数据
+----+-------------+------------+
| ID | sps_Bereich | Date |
+----+-------------+------------+
| 10 | helpeu1 | 2020-07-31 |
+----+-------------+------------+
| 11 | helpeu2 | 2020-07-31 |
+----+-------------+------------+
| 12 | helpeu3 | 2020-07-31 |
+----+-------------+------------+
| 13 | helpeu3 | 2020-07-31 |
+----+-------------+------------+
| 14 | helpeu4 | 2020-07-31 |
+----+-------------+------------+
| 15 | helpeu3 | 2020-07-30 |
+----+-------------+------------+
| 16 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
| 17 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
| 18 | helpeu4 | 2020-07-30 |
+----+-------------+------------+
我还有一个 table 叫 Bereich
+----+---------+
| ID | Name |
+----+---------+
| 1 | helpeu1 |
+----+---------+
| 2 | helpeu2 |
+----+---------+
| 3 | helpeu3 |
+----+---------+
| 4 | helpeu4 |
+----+---------+
我想按 sps_bereich 和日期分组,我正在使用以下查询
select sps_bereich, [Date], count(*) as [Nr of errors] from [ErrorLogs]
where SPS_Bereich IN (
select Name from [Bereich]
)
group by SPS_Bereich, Date
order by date desc, SPS_Bereich asc
它给了我以下输出。
+-------------+------------+--------------+
| sps_bereich | Date | Nr of errors |
+-------------+------------+--------------+
| helpeu1 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-31 | 2 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-30 | 1 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-30 | 3 |
+-------------+------------+--------------+
我想要的是在没有数据的地方包含 sps_bereich
。
在这种情况下,我想将 helpeu1,helpeu2
添加为 0
日期 2020-07-30
我想要以下输出。
+-------------+------------+--------------+
| sps_bereich | Date | Nr of errors |
+-------------+------------+--------------+
| helpeu1 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-31 | 2 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-31 | 1 |
+-------------+------------+--------------+
| helpeu1 | 2020-07-30 | 0 |
+-------------+------------+--------------+
| helpeu2 | 2020-07-30 | 0 |
+-------------+------------+--------------+
| helpeu3 | 2020-07-30 | 1 |
+-------------+------------+--------------+
| helpeu4 | 2020-07-30 | 3 |
+-------------+------------+--------------+
我在想添加一个 IN 子句必须 return 我的所有值,但事实并非如此。
这里是 SQLFiddle.
使用左连接 -
select A.Name, A.date,coalesce(B.cnt,0) as no_of_errors
from
(select distinct date,name from Errorlogs cross apply Bereich)A
left join
(select sps_Bereich, [Date],count(*) cnt
from Errorlogs group by sps_Bereich, [Date]
)B
on A.date=B.date and A.Name=B.sps_Bereich order by A.date desc, A.name asc
试试这个。 它构建一个完整的姓名和日期网格,然后用计数填充它。
select grid.name,grid.date,coalesce(cnt.cnt,0) from
(select distinct e.date, d.name from errorlogs e, Bereich d) grid
left outer join
(
select b.sps_Bereich, b.date, count(*) as cnt from [ErrorLogs] b
group by b.sps_Bereich,b.date
) cnt
on grid.date=cnt.date and grid.name=cnt.sps_bereich
我建议将查询写成:
select b.name, d.date, count(el.id) as num_errors
from Bereich b cross join
(select distinct date from errorlogs) d left join
errorlogs el
on el.date = d.date and el.sps_Bereich = b.name
group by b.name, d.date
order by d.date desc, b.name;
这个想法很简单。使用 cross join
在结果集中生成您想要的行。然后用left join
把已有的数据带进来
Here 是一个 db<>fiddle.