1

我阅读了许多带有best-n-per-group标签的问题,但我找不到比UNION ALL这个 SQL 查询的技巧更好的解决方案:

select * from (select GroupName, JobName, Start, End, Status, (strftime('%s', End) - strftime('%s', Start)) as Length from ReportJobs where PlanDate = '2014-02-13' and GroupName like 'GRP01%' ORDER BY Length DESC LIMIT 10)
UNION ALL
select * from (select GroupName, JobName, Start, End, Status, (strftime('%s', End) - strftime('%s', Start)) as Length from ReportJobs where PlanDate = '2014-02-13' and GroupName like 'GRP04%' ORDER BY Length DESC LIMIT 10)
UNION ALL
select * from (select GroupName, JobName, Start, End, Status, (strftime('%s', End) - strftime('%s', Start)) as Length from ReportJobs where PlanDate = '2014-02-13' and GroupName like 'GRP12%' ORDER BY Length DESC LIMIT 10)
UNION ALL
select * from (select GroupName, JobName, Start, End, Status, (strftime('%s', End) - strftime('%s', Start)) as Length from ReportJobs where PlanDate = '2014-02-13' and GroupName like 'GRP15%' ORDER BY Length DESC LIMIT 10)
UNION ALL
select * from (select GroupName, JobName, Start, End, Status, (strftime('%s', End) - strftime('%s', Start)) as Length from ReportJobs where PlanDate = '2014-02-13' and GroupName like 'GRP20%' ORDER BY Length DESC LIMIT 10);

我想为五个不同的组(GRP01%GRP04%GRP12%GRP15%GRP20%)选择十个最长的工作。

我正在使用 SQLite。和列中的
元素采用 ISO 8601 格式。 我用来计算每个工作的长度,然后是每个组。StartEnd
(strftime('%s', End) - strftime('%s', Start)) as LengthORDER BY Length DESC LIMIT 10

您知道执行此查询的最简单/更好的方法(使用 SQLite)吗?

4

0 回答 0