dongxing9219 2018-03-06 18:17
浏览 390
已采纳

SQL从日期,最低和最高值中选择不同的最小和最大记录

Let's say i have a table like this:

id  |  date               | price
-------------------------------
1   | 2018-03-06 22:19:10 | $10   
2   | 2018-03-06 13:16:19 | $12 
3   | 2018-03-06 00:12:11 | $18 
4   | 2018-03-05 23:29:10 | $10 
5   | 2018-03-05 03:16:19 | $05 
6   | 2018-03-05 00:11:11 | $11

I want to retrieve distinct date , and for each distinct date its first result (like lowest hour) and its related price and the latest result (like highest hour) and its related price. I also need to retrieve the highest and lowest price for each distinct date.

Is it possible doing this with a single query? If yes, how? What I tried is using distinct(date) but, since there are different hours, it returns all the results. I also tried Trunc but does not work.

Sample result:
date        min_price   max_price   lowest_hour_price   highest_hour_price
2018-03-06  $10         $18         $18                 $10
  • 写回答

1条回答 默认 最新

  • dpndp64206 2018-03-06 18:33
    关注

    I'm using mySql 5.6 and this query works:

    select date(m.min_max_date) as date,
       max(case when m.lbl='min_hr_price' then m.min_max_hr_price else null end) as lowest_hr_price,
       max(case when m.lbl='max_hr_price' then m.min_max_hr_price else null end) as max_hr_price,
       max(case when n.lbl='min_price' then n.min_max_price else null end) as min_price,
       max(case when n.lbl='max_price' then n.min_max_price else null end) as max_price
    from (select 'min_hr_price' as lbl, price as min_max_hr_price, date as min_max_date 
      from tbl 
      where date in (select min(date) as min_date from tbl group by date(date))
    union 
    select 'max_hr_price', price, date   
      from tbl 
      where date in (select max(date) as max_date from tbl group by date(date))) as m,
    (
    select 'min_price' as lbl,
    min(date) as min_max_date,
    min(price) as min_max_price
    from tbl
    group by date(date)
    union
    select 'max_price' as lbl,
    max(date) as min_max_date,
    max(price) as min_max_price
    from tbl
    group by date(date)
    ) n
    where m.min_max_date=n.min_max_date
    group by date(m.min_max_date)
    order by m.min_max_date
    
    Sample result:
    date        lowest_hr_price max_hr_price    min_price   max_price
    2018-03-06  $1102.8         $1821           $1011.6     $1821
    
    INSERT INTO TBL VALUES(1, '2018-03-06 22:19:10', '$1011.6');
    INSERT INTO TBL VALUES(2, '2018-03-06 13:19:11', '$1011.6');
    INSERT INTO TBL VALUES(3, '2018-03-06 03:21:25', '$1106.2');
    INSERT INTO TBL VALUES(4, '2018-03-06 00:26:50', '$1102.8');
    INSERT INTO TBL VALUES(5, '2018-03-06 22:26:17', '$1821');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况