获取最小,最大行

时间:2017-11-15 11:03:23

标签: mysql sql group-by select-query presto

我有以下样本数据,我希望获得每个连续状态的最小和最大时间。

cat     subcat  status  logtime
fruits  apple   0   30-10-2017 06:00
fruits  apple   0   30-10-2017 06:03
fruits  apple   0   30-10-2017 06:06
fruits  apple   0   30-10-2017 06:09
fruits  apple   0   30-10-2017 06:12
fruits  apple   0   30-10-2017 06:15
fruits  apple   0   30-10-2017 06:18
fruits  apple   0   30-10-2017 06:21
fruits  apple   0   30-10-2017 06:24
fruits  apple   0   30-10-2017 06:27
fruits  apple   0   30-10-2017 06:30
fruits  apple   0   30-10-2017 06:33
fruits  apple   0   30-10-2017 06:36
fruits  apple   0   30-10-2017 06:39
fruits  apple   0   30-10-2017 06:42
fruits  apple   0   30-10-2017 06:45
fruits  apple   0   30-10-2017 06:48
fruits  apple   0   30-10-2017 06:51
fruits  apple   0   30-10-2017 06:54
fruits  apple   0   30-10-2017 06:57
fruits  strawberry  1   30-10-2017 06:00
fruits  strawberry  1   30-10-2017 06:03
fruits  strawberry  1   30-10-2017 06:06
fruits  strawberry  2   30-10-2017 06:09
fruits  strawberry  1   30-10-2017 06:12
fruits  strawberry  1   30-10-2017 06:15
fruits  strawberry  1   30-10-2017 06:18
fruits  strawberry  1   30-10-2017 06:21
fruits  strawberry  1   30-10-2017 06:24
fruits  strawberry  1   30-10-2017 06:27
fruits  strawberry  1   30-10-2017 06:30
fruits  strawberry  1   30-10-2017 06:33
fruits  strawberry  1   30-10-2017 06:36
fruits  strawberry  1   30-10-2017 06:39
fruits  strawberry  2   30-10-2017 06:42
fruits  strawberry  1   30-10-2017 06:45
fruits  strawberry  1   30-10-2017 06:48
fruits  strawberry  1   30-10-2017 06:51
fruits  strawberry  1   30-10-2017 06:54
fruits  strawberry  1   30-10-2017 06:57
fruits  blueberry   0   30-10-2017 06:00
fruits  blueberry   0   30-10-2017 06:03
fruits  blueberry   0   30-10-2017 06:06
fruits  blueberry   2   30-10-2017 06:09
fruits  blueberry   0   30-10-2017 06:12
fruits  blueberry   0   30-10-2017 06:15
fruits  blueberry   0   30-10-2017 06:18
fruits  blueberry   0   30-10-2017 06:21
fruits  blueberry   0   30-10-2017 06:24
fruits  blueberry   0   30-10-2017 06:27
fruits  blueberry   0   30-10-2017 06:30
fruits  blueberry   0   30-10-2017 06:33
fruits  blueberry   0   30-10-2017 06:36
fruits  blueberry   0   30-10-2017 06:39
fruits  blueberry   2   30-10-2017 06:42
fruits  blueberry   0   30-10-2017 06:45
fruits  blueberry   0   30-10-2017 06:48
fruits  blueberry   0   30-10-2017 06:51
fruits  blueberry   0   30-10-2017 06:54
fruits  blueberry   0   30-10-2017 06:57
fruits  guaua   1   30-10-2017 06:00
fruits  guaua   1   30-10-2017 06:03
fruits  guaua   1   30-10-2017 06:06
fruits  guaua   2   30-10-2017 06:09
fruits  guaua   1   30-10-2017 06:12
fruits  guaua   1   30-10-2017 06:15
fruits  guaua   1   30-10-2017 06:18
fruits  guaua   1   30-10-2017 06:21
fruits  guaua   1   30-10-2017 06:24
fruits  guaua   1   30-10-2017 06:27
fruits  guaua   1   30-10-2017 06:30
fruits  guaua   1   30-10-2017 06:33
fruits  guaua   1   30-10-2017 06:36
fruits  guaua   1   30-10-2017 06:39
fruits  guaua   2   30-10-2017 06:42
fruits  guaua   1   30-10-2017 06:45
fruits  guaua   1   30-10-2017 06:48
fruits  guaua   1   30-10-2017 06:51
fruits  guaua   1   30-10-2017 06:54
fruits  guaua   1   30-10-2017 06:57

我正在尝试获取cat和subcat状态的每个状态的最小值,最长时间。

输出结果应如下:

cat     subcat      status  mintime maxtime
fruits  apple       0   30-10-2017 06:00    30-10-2017 06:57
fruits  strawberry  1   30-10-2017 06:00    30-10-2017 06:06
fruits  strawberry  2   30-10-2017 06:09    30-10-2017 06:09
fruits  strawberry  1   30-10-2017 06:12    30-10-2017 06:39
fruits  strawberry  2   30-10-2017 06:42    30-10-2017 06:42
fruits  strawberry  1   30-10-2017 06:45    30-10-2017 06:57
fruits  blueberry   0   30-10-2017 06:00    30-10-2017 06:06
fruits  blueberry   2   30-10-2017 06:09    30-10-2017 06:09
fruits  blueberry   0   30-10-2017 06:12    30-10-2017 06:39
fruits  blueberry   2   30-10-2017 06:42    30-10-2017 06:42
fruits  blueberry   0   30-10-2017 06:45    30-10-2017 06:57
fruits  guaua       1   30-10-2017 06:00    30-10-2017 06:06
fruits  guaua       2   30-10-2017 06:09    30-10-2017 06:09
fruits  guaua       1   30-10-2017 06:12    30-10-2017 06:39
fruits  guaua       2   30-10-2017 06:42    30-10-2017 06:42
fruits  guaua       1   30-10-2017 06:45    30-10-2017 06:57

尝试使用以下sql查询,但无法获得高于输出而不是给出最小,最大总小时。

SELECT cat,
        subcat,
        status,        
        min(logtime) as minlog,
        max(logtime) as maxlog
FROM fruits_log
WHERE 
    cat = 'fruits'
    AND date_format(logtime,'%Y-%m-%d %H) = '2017-10-30 06'
    AND status IN ('0','1','2')
GROUP BY cat,subcat,date_format(logtime,'%Y-%m-%d %H),status
ORDER BY cat,subcat,minlog ;

请帮助我在上述查询中缺少的内容

2 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情

select  t.cat, t.subcat, t.status, min(t.logdate), max(t.logdate)
from
(
    select t1.cat, t1.subcat, t1.status, t1.logdate, count(t2.cat) grpnum
    from data t1
    left join data t2 on t1.cat = t2.cat and t1.subcat = t2.subcat and 
                         t1.status != t2.status and 
                         t1.logdate < t2.logdate
    group by t1.cat, t1.subcat, t1.status, t1.logdate
) t
group by t.cat, t.subcat, t.status, t.grpnum

我已经测试过你的数据非常小sample,但似乎没问题。内部查询使用自联接来隔离具有相同catsubcatstatus值的连续行。准备就绪后,您只需为数据中的每组行执行group by

答案 1 :(得分:0)

Select cat,subcat,minlog,maxlog from ( SELECT cat,
            subcat,
            status,        
            min(logtime) as minlog,
            max(logtime) as maxlog
    FROM fruits_log
    WHERE 
        cat = 'fruits'
        AND date_format(logtime,'%Y-%m-%d %H') = '2017-10-30 06'
        AND status IN ('0','1','2')
    GROUP BY cat,subcat,date_format(logtime,"%Y-%m-%d %H-%i"),status) t1 order by cat,subcat,status;

试试这个......