SQL输出精简

时间:2019-07-08 16:37:03

标签: mysql sql

我有一个运行SQL脚本的批处理文件,该脚本可以运行每周,每两周,每月或每年的报告。

我使用的脚本是:

SELECT name, Output, TrendTime FROM LoopDef
Where TrendTime between ‘01-Jul-19 00:00:01’ and ‘01-Jul-19 01:00:00’
OR LIKE ‘%%-%%-%% %%:15:%%’
OR LIKE ‘%%-%%-%% %%:30:%%’
OR LIKE ‘%%-%%-%% %%:45:%%’
OR LIKE ‘%%-%%-%% %%:00:%%’
Order by name, Output, TrendTime’;

这每隔00、15、30和45分钟输出一次,可惜我得到0-60秒的倍数。

是否有一种省去多余结果的简单方法,而每个LIKE仅具有一个输出值?

2 个答案:

答案 0 :(得分:0)

如果唯一的问题是重复,则只需在SELECT命令之后添加DISTINCT:

SELECT DISTINCT name, ...

答案 1 :(得分:0)

SELECT DISTINCT name, Output, TrendTime FROM LoopDef
WHERE (TrendTime BETWEEN '30-JUN-19 00:00:01' AND '01-JUL-19 01:00:00')
GROUP BY name, Output, TrendTime
HAVING TrendTime LIKE '%%-%%%-%% %%:00:%%'
OR TrendTime LIKE '%%-%%%-%% %%:15:%%'
OR TrendTime LIKE '%%-%%%-%% %%:30:%%'
OR TrendTime LIKE '%%-%%%-%% %%:45:%%'
ORDER BY name, TrendTime;

仍然很难获得多个输出。例如:

"Main_Loop                     19.9 30-JUN-19 00:30:00"
"Main_Loop                     19.8 30-JUN-19 00:30:04"
"Main_Loop                     19.8 30-JUN-19 00:30:56"