SQL 查询以显示过去一天的多个计数
SQL query to show multiple counts over the last day
我如何制定一个 sql 查询,我可以在其中显示最近一天的 table 的成功计数和错误计数以及警告。所以我可以在仪表板中创建可视化。最终目标是让用户知道当天有多少健康的管道,有多少不健康的管道。
我试过类似的东西,但我把它复杂化了。
SELECT status, COUNT(*) as Total, cast(cast(time as Timestamp) as date) as time,
SUM(CASE WHEN Status = 'Success' THEN 1 ELSE 0 END) as Success,
SUM(CASE WHEN Status = 'Warning' THEN 1 ELSE 0 END) as Warning,
SUM(CASE WHEN Status = 'Error' THEN 1 ELSE 0 END) as Error
FROM table-name
where (cast(cast(time as Timestamp) as date) in ({{ Time }}) or {{ Time }} = 'All')
GROUP BY 1, cast(cast(time as Timestamp) as date)
order by cast(cast(time as Timestamp) as date) desc
假设这是给定的 table
How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day .
你可以这样使用:
SELECT DATE(time) as date, COUNT(*) as Total, ,
SUM(Status = 'Success') as Success,
SUM(Status = 'Warning') as Warning,
SUM(Status = 'Error') as Error
FROM table-name
WHERE date(time) = (SELECT MAX(date(time)) FROM table-name)
GROUP BY 1;
我如何制定一个 sql 查询,我可以在其中显示最近一天的 table 的成功计数和错误计数以及警告。所以我可以在仪表板中创建可视化。最终目标是让用户知道当天有多少健康的管道,有多少不健康的管道。
我试过类似的东西,但我把它复杂化了。
SELECT status, COUNT(*) as Total, cast(cast(time as Timestamp) as date) as time,
SUM(CASE WHEN Status = 'Success' THEN 1 ELSE 0 END) as Success,
SUM(CASE WHEN Status = 'Warning' THEN 1 ELSE 0 END) as Warning,
SUM(CASE WHEN Status = 'Error' THEN 1 ELSE 0 END) as Error
FROM table-name
where (cast(cast(time as Timestamp) as date) in ({{ Time }}) or {{ Time }} = 'All')
GROUP BY 1, cast(cast(time as Timestamp) as date)
order by cast(cast(time as Timestamp) as date) desc
假设这是给定的 table
How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day .
你可以这样使用:
SELECT DATE(time) as date, COUNT(*) as Total, ,
SUM(Status = 'Success') as Success,
SUM(Status = 'Warning') as Warning,
SUM(Status = 'Error') as Error
FROM table-name
WHERE date(time) = (SELECT MAX(date(time)) FROM table-name)
GROUP BY 1;