根据其他列值选择MIN,MAX对应列

时间:2019-11-14 07:01:46

标签: mysql sql

我有下表:

Date        Open        High        Low         Close
1-Oct-19    225.070007  228.220001  224.199997  224.589996
2-Oct-19    223.059998  223.580002  217.929993  218.960007
3-Oct-19    218.429993  220.960007  215.130005  220.820007
4-Oct-19    225.639999  227.490005  223.889999  227.009995
7-Oct-19    226.270004  229.929993  225.839996  227.059998
8-Oct-19    225.820007  228.059998  224.330002  224.399994
9-Oct-19    227.029999  227.789993  225.639999  227.029999
10-Oct-19   227.929993  230.440002  227.300003  230.089996
11-Oct-19   232.949997  237.639999  232.309998  236.210007
14-Oct-19   234.899994  238.130005  234.669998  235.869995
15-Oct-19   236.389999  237.649994  234.880005  235.320007
16-Oct-19   233.369995  235.240005  233.199997  234.369995
17-Oct-19   235.089996  236.149994  233.520004  235.279999
18-Oct-19   234.589996  237.580002  234.289993  236.410004
21-Oct-19   237.520004  240.990005  237.320007  240.509995
22-Oct-19   241.160004  242.199997  239.619995  239.960007
23-Oct-19   242.100006  243.240005  241.220001  243.179993
24-Oct-19   244.509995  244.800003  241.809998  243.580002
25-Oct-19   243.160004  246.729996  242.880005  246.580002
28-Oct-19   247.419998  249.25  246.720001  249.050003
29-Oct-19   248.970001  249.75  242.570007  243.289993
30-Oct-19   244.759995  245.300003  241.210007  243.259995
31-Oct-19   247.240005  249.169998  237.259995  248.759995

对于给定的日期范围(例如03Oct19到10Oct19之间),我想获取HIGH列的MAXIMUM值及其对应的日期值,LOW列的MINIMUM值及其对应的日期。 在上面的示例中,预期结果应为

|  MAX(High)  | High-Date |  MIN(Low)  | Min-Date  |
+-------------+-----------+------------+-----------+
|  230.44002  | 10-Oct-19 | 215.130005 | 03-Oct-19 |

我正在尝试以下查询:

SELECT max(high)
       , Date
       , min(low)
       , Date 
FROM tbl1 
where date>='2019-10-03' 
and date<='2019-10-10' 
group by date

但是,上面的代码缺少一些内容,因为它只是返回了所有行。

任何想法都应该使用(联接?还是嵌套查询?)

谢谢

4 个答案:

答案 0 :(得分:2)

尝试这样的事情:

select * from 
(SELECT high as "Max(high)"
       , Date_c as "High-Date"
FROM tbl1 
where Date_c >= '2019-10-03' 
and Date_c <= '2019-10-10' 
And high = (select max(high) from tbl1)) a
cross join
(SELECT low as "MIN(Low)"
       , Date_c as "Min-Date"
FROM tbl1 
where Date_c >= '2019-10-03' 
and Date_c<= '2019-10-10' 
And low = (select min(low) from tbl1)) b

这是MySQL 5.7的一个小DEMO

或者如果由于某些原因您有两行相同,而您只想要一个结果:

select a.high as "Max(high)"
       , a.Date_c "High-Date" 
       , b.low as "MIN(Low)"
       , b.Date_c as "Min-Date"
from 
(SELECT max(high) as "high"
       , max(Date_c) as "Date_c"
FROM tbl1 
where high = (select max(high) 
              from tbl1 
              where Date_c between '2019-10-03' and '2019-10-10')) a
cross join
(SELECT min(low) as "low"
       , max(Date_c) as "Date_c"
FROM tbl1 
where low = (select min(low) 
             from tbl1 
             where Date_c between '2019-10-03' and '2019-10-10')) b

这里是DEMO

这是根据@SalmanA(Cheers!)建议的代码:

select A.high as "Max(high)"
       , A.Date_c "High-Date" 
       , B.low as "MIN(Low)"
       , B.Date_c as "Min-Date"
from 
(SELECT High, Date_c 
FROM tbl1
where Date_c between '2019-10-03' and '2019-10-10'
ORDER BY High DESC LIMIT 1) A
CROSS JOIN 
(SELECT Low, Date_c 
FROM tbl1 
where Date_c between '2019-10-03' and '2019-10-10'
ORDER BY Low LIMIT 1
) B;

还有DEMO

答案 1 :(得分:1)

您可以尝试以下操作:

SELECT
T1.*,T2.*
FROM
(
SELECT  DATE AS  YOUR_MX_DATE,HIGH AS YOUR_HG_VALUE
FROM TBL1 
WHERE   HIGH IN (
SELECT MAX(HIGH) AS MX_VALUE  FROM TBL1 
WHERE   Date >='2019-10-03' 
AND Date <='2019-10-10'  
)
)T1 CROSS JOIN
(
SELECT DATE AS YOUR_MN_DATE,HIGH AS YOUR_MN_VALUE
FROM TBL1 
WHERE LOW IN (
SELECT MIN(LOW) AS MN_VALUE  FROM TBL1 
WHERE   Date >='2019-10-03' 
AND Date <='2019-10-10' 
)
) T2

答案 2 :(得分:1)

您首先需要找到指定日期范围内的最大值和最小值,然后与主表结合以获取相应的日期。结果将具有一行(同一日期的最高值和最低值)或两行(两个日期的最高值和最低值)或更多行(多个日期的同一最高值或最低值)。使用条件聚合来完善结果:

SELECT MIN(CASE WHEN t.High = v.High THEN t.High END) AS High     -- MIN/MAX does not matter here
     , MIN(CASE WHEN t.High = v.High THEN t.Date END) AS HighDate -- MIN for first date, MAX for last date
     , MIN(CASE WHEN t.Low  = v.Low  THEN t.Low  END) AS Low      -- MIN/MAX does not matter here
     , MIN(CASE WHEN t.Low  = v.Low  THEN t.Date END) AS LowDate  -- MIN for first date, MAX for last date
FROM (
    SELECT MAX(High) AS High
         , MIN(Low) AS Low
    FROM t
    WHERE Date BETWEEN '2019-10-03' AND '2019-10-10' 
) AS v
JOIN t ON t.High = v.High OR t.Low = v.Low
WHERE Date BETWEEN '2019-10-03' AND '2019-10-10'

Demo on db<>fiddle

答案 3 :(得分:1)

您可以使用此查询来获得所需的结果。它会在兴趣期内找到MAX(high)MIN(low)值,然后JOIN将这些值返回原始表以找到相应的日期。请注意,我们在日期值上使用MIN()以避免出现超过一天的高/低值的情况;这给出了最早发生的日期。如果您希望出现高/低值的最新日期,请将其更改为MAX()

SELECT hl.high, 
       MIN(t1.date) AS high_date, 
       hl.low, 
       MIN(t2.date) AS low_date
FROM (SELECT MAX(high) AS high, MIN(low) AS low
      FROM tbl1
      WHERE `Date` BETWEEN '2019-10-03' AND '2019-10-10') hl
JOIN tbl1 t1 ON t1.high = hl.high AND t1.date BETWEEN '2019-10-03' AND '2019-10-10'
JOIN tbl1 t2 ON t2.low = hl.low AND t2.date BETWEEN '2019-10-03' AND '2019-10-10'
GROUP BY hl.high, hl.low

输出:

high    low     high_date   low_date
230.44  215.13  2019-10-10  2019-10-03

Demo on dbfiddle