繁体   English   中英

如何获得正确的最小值和最大值

[英]How to get the correct min and max values

我有以下结构http://sqlfiddle.com/#!9/f8341c并且从这些记录中我预期的输出如下

Name|MinCost |MaxCost | Open | Close | Date
ABC |13.6    | 15.3   | 14.1 | 14.2  | 2015-12-02
DEF |93.2    | 96.3   | 93.7 | 95.4  | 2015-12-02 
ABC |15.1    | 15.6   | 15.1 | 15.2  | 2015-12-03
DEF |97.2    | 97.7   | 97.7 | 97.7  | 2015-12-03

现在我已经这样做了

SELECT t1.name as 'Name',t1.min as 'MinCost',t1.max as 'MaxCost',t2.open ,t3.close,t1.times as 'Date'
  FROM(
        select name, max(cost) as 'max',min(cost) as 'min', date(time) as 'times'  
        from providers 
        group by  times,name 
        order by times
   ) AS t1
   JOIN (
     select name,cost as 'open',  time  
     from providers 
     where TIME(time) = '00:00:00'   
     group by time,name
     order by time
   ) as t2 on t1.name=t2.name
   JOIN (
       select name,cost as 'close', time  
       from providers 
       where TIME(time) = '23:59:59'   
       group by time,name
       order by time
   ) as t3 on t2.name=t3.name 
   GROUP BY t1.times,t1.name
   ORDER BY t1.times,t1.name

这给了我以下输出

| Name | MinCost | MaxCost | open | close |       Date |
|------|---------|---------|------|-------|------------|
|  ABC |    13.6 |    15.3 | 14.1 |  14.2 | 2015-12-02 |
|  DEF |    93.2 |    96.3 | 97.7 |  95.4 | 2015-12-02 |
|  ABC |    15.1 |    15.6 | 14.1 |  14.2 | 2015-12-03 |
|  DEF |    97.2 |    97.7 | 97.7 |  95.4 | 2015-12-03 |

我需要做什么来更正查询?

由于您的姓名不是唯一的,因此您需要在 join on子句中添加time ,以便获得具有nametime的唯一值。

下面的查询应该可以工作,在你的小提琴上进行演示以获得预期的输出

  SELECT t1.name as 'Name',t1.min as 'MinCost',t1.max as 'MaxCost',t2.open ,t3.close,t1.times as 'Date'
  FROM(
        select name, max(cost) as 'max',min(cost) as 'min', date(time) as 'times'  
        from providers 
        group by  times,name 
        order by times
   ) AS t1
   JOIN (
     select name,cost as 'open',  date(time) as 'times' 
     from providers 
     where TIME(time) = '00:00:00'   
     group by time,name
     order by time
   ) as t2 on t1.name=t2.name and t1.times = t2.times
   JOIN (
       select name,cost as 'close', date(time) as 'times' 
       from providers 
       where TIME(time) = '23:59:59'   
       group by time,name
       order by time
   ) as t3 on t2.name=t3.name  and t2.times = t3.times
   GROUP BY t1.times,t1.name
   ORDER BY t1.times,t1.name

干得好:

select
  d.name, d.min_cost, d.max_cost,
  o.cost as open,
  c.cost as close,
  d.d as date
from (
  select
    name,
    min(cost) as min_cost,
    max(cost) as max_cost,
    min(time) as open_time,
    max(time) as close_time,
    date(time) as d
  from providers
  group by name, date(time)
) d
join providers o on o.name = d.name and o.time = d.open_time
join providers c on c.name = d.name and c.time = d.close_time
order by d.d, d.name

结果:

name  min_cost  max_cost  open  close  date
----  --------  --------  ----  -----  ----------
ABC       13.6      15.3  14.1   14.2  2015-12-02
DEF       93.2      96.3  93.7   95.4  2015-12-02
ABC       15.1      15.6  15.1   15.2  2015-12-03
DEF       97.2      97.7  97.7   97.4  2015-12-03

请注意,您的预期结果在最后一行有点错误,因为开盘价是97.7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM