所有分钟分组时甲骨文案
我准备了一个SQL,该SQL按每分钟计算交易。但是我想在没有交易的情况下获得分钟。我找不到任何解决方案。
我的SQL:
SELECT SUM(CASE
WHEN UPPER(situation) = 'OK' THEN
1
ELSE
0
END) AS OK,
SUM(CASE
WHEN UPPER(situation) = 'NOK' THEN
1
ELSE
0
END) AS NOK,
TO_CHAR(q1.start_date, 'DD/MM/YYYY HH24:MI') AS StartDate,
TO_CHAR(q1.start_date, 'YYYY/MM/DD HH24:MI') AS FormatStartDate
FROM table1 q1
WHERE q1.start_date >=
TO_DATE(TO_CHAR((sysdate - 4 / 24), 'dd/mm/yyyy hh24:mi'),
'dd/mm/yyyy hh24:mi')
AND q1.start_date <=
TO_DATE(TO_CHAR((sysdate - 0 / 24), 'dd/mm/yyyy hh24:mi'),
'dd/mm/yyyy hh24:mi')
AND q1.type = 'TEST'
GROUP BY q1.type,
TO_CHAR(q1.start_date, 'DD/MM/YYYY HH24:MI'),
TO_CHAR(q1.start_date, 'YYYY/MM/DD HH24:MI')
ORDER BY FormatStartDate
SQL结果:
OK | Nok | StartDate | FormatStartDate |
---|---|---|---|
2 | 0 | 26/04/2022 11:51 | 2022/04/26 11:51 |
3 | 0 | 26/04/2022 11:52 | 2022/04/04/26 11:52 11:52 |
1 | 0 | 26/26/26/26/26/ 04/2022 11:54 | 2022/04/26 11:54 |
2 | 0 | 26/04/2022 11:56 | 2022/04/26 11:56 |
您可以在结果中看到,线26/04/2022 11:53 26/04/2022 11:55缺少。即使是0,我也想在结果中看到这些行。
I prepared a sql that counts transactions on a per minute basis. But I want to get the minutes with no transactions as 0. I couldn't find any solution.
My sql:
SELECT SUM(CASE
WHEN UPPER(situation) = 'OK' THEN
1
ELSE
0
END) AS OK,
SUM(CASE
WHEN UPPER(situation) = 'NOK' THEN
1
ELSE
0
END) AS NOK,
TO_CHAR(q1.start_date, 'DD/MM/YYYY HH24:MI') AS StartDate,
TO_CHAR(q1.start_date, 'YYYY/MM/DD HH24:MI') AS FormatStartDate
FROM table1 q1
WHERE q1.start_date >=
TO_DATE(TO_CHAR((sysdate - 4 / 24), 'dd/mm/yyyy hh24:mi'),
'dd/mm/yyyy hh24:mi')
AND q1.start_date <=
TO_DATE(TO_CHAR((sysdate - 0 / 24), 'dd/mm/yyyy hh24:mi'),
'dd/mm/yyyy hh24:mi')
AND q1.type = 'TEST'
GROUP BY q1.type,
TO_CHAR(q1.start_date, 'DD/MM/YYYY HH24:MI'),
TO_CHAR(q1.start_date, 'YYYY/MM/DD HH24:MI')
ORDER BY FormatStartDate
Sql Result:
OK | NOK | StartDate | FormatStartDate |
---|---|---|---|
2 | 0 | 26/04/2022 11:51 | 2022/04/26 11:51 |
3 | 0 | 26/04/2022 11:52 | 2022/04/26 11:52 |
1 | 0 | 26/04/2022 11:54 | 2022/04/26 11:54 |
2 | 0 | 26/04/2022 11:56 | 2022/04/26 11:56 |
As you can see in the result, the lines 26/04/2022 11:53 and 26/04/2022 11:55 are missing. I want to see these lines in the result even if it is 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在数据仓库中,您有一个日期维度表,每分钟有1个记录(或任何相关间隔的业务案例。或使用通过连接来生成查询的一部分
- 这是虚拟数据,因为您没有提供任何示例数据。
In a data warehouse you're have a date dimension table, with 1 record per minute (or whatever the relevant interval is for your business case. Left join to date dimension table so you get a record for each minute. Either create that table, or generate it as part of the query using
CONNECT BY LEVEL
.Example using
CONNECT BY
- this is dummy data as you didn't provide any sample data.您可以生成日历,然后使用
分区
ed 外部Join 生成所有行:对于示例数据:
输出:
You can generate a calendar and then use a
PARTITION
edOUTER JOIN
to generate all the rows:Which, for the sample data:
Outputs:
db<>fiddle here