MAX和MIN添加查询

时间:2013-11-27 04:49:58

标签: mysql sql

SELECT a.price, b.price, a.delivery_hour, a.resource_id, b.delivery_date
             FROM mms_realtime_dispatch_prices_report AS a
             INNER JOIN mms_realtime_expost_prices_report AS b 
             ON a.resource_id = b.resource_id
             AND a.delivery_hour = b.delivery_hour
             AND a.delivery_date = b.delivery_date
             WHERE a.delivery_date = '2013-09-21'
             ORDER BY a.resource_id ASC 

这是我的查询。是否可以添加一个新列来确定价格的最大值和最小值?

2 个答案:

答案 0 :(得分:1)

  SELECT MIN(a.price) as MIN_PRICE, MAX(a.price) as MAX_PRICE, b.price, a.delivery_hour, a.resource_id, b.delivery_date
         FROM mms_realtime_dispatch_prices_report AS a
         INNER JOIN mms_realtime_expost_prices_report AS b 
         ON a.resource_id = b.resource_id
         AND a.delivery_hour = b.delivery_hour
         AND a.delivery_date = b.delivery_date
         WHERE a.delivery_date = '2013-09-21'
         GROUP BY b.price, a.delivery_hour, a.resource_id, b.delivery_date
         ORDER BY a.resource_id ASC 

答案 1 :(得分:0)

尝试这样的事情:

SELECT max(a.price), min(b.price), a.delivery_hour, a.resource_id, b.delivery_date
         FROM mms_realtime_dispatch_prices_report AS a
         INNER JOIN mms_realtime_expost_prices_report AS b 
         ON a.resource_id = b.resource_id
         AND a.delivery_hour = b.delivery_hour
         AND a.delivery_date = b.delivery_date
         WHERE a.delivery_date = '2013-09-21'
         GROUP BY a.delivery_hour, a.resource_id, b.delivery_date
         ORDER BY a.resource_id ASC