sql根据另一列选择整行的特定信息

时间:2016-07-22 20:42:51

标签: sql date conditional-statements

我有下表的信息:

Location_id |  Date_1    | Ex_Start    | EX_End    | Condition | Price
------------+------------+-------------+-----------------------+-------
   L_1      | 23-JUL-16  | 08-JUN-16   | 25-JUL-16 | EX        |  109
   L_1      | 23-JUL-16  | 28-JUL-16   | 31-JUL-16 | Non_EX    |  109
   L_1      | 24-JUL-16  | 08-JUN-16   | 25-JUL-16 | EX        |  89
   L_1      | 24-JUL-16  | 28-JUL-16   | 31-JUL-16 | Non_EX    |  89
   ...            ...          ...           ...       ...     
   L_2      | 23-JUL-16  | 24-JUL-16   | 15-AUG-16 | Non_EX    |  99
   L_2      | 23-JUL-16  | 26-OCT-16   | 29-JAN-17 | Non_EX    |  99
   L_2      | 24-JUL-16  | 24-JUL-16   | 15-AUG-16 | EX        |  79
   L_2      | 24-JUL-16  | 26-OCT-16   | 29-JAN-17 | Non_EX    |  79
   ...            ...          ...           ...       ...

“Date_1”是从当前日期到明年的同一天。 对于“条件”列,“EX”表示“Date_1”属于EX时段(在“EX_Start”和“Ex_End”之间)。 对于“条件”列,“Non_EX”表示它不属于EX时段,“EX”表示它落入。 我想在所有Location_id中选择特定的Date_1信息,如果在同一日期下每个位置有一个“Ex”,则条件将返回“EX”。或者它将返回“Non_EX”。

例如。该表将返回:

Location_id |  Date_1    | Condition   | Price
------------+------------+-------------+-----------------------
   L_1      | 23-JUL-16  | EX          |  109
   L_1      | 24-JUL-16  | EX          |  89
   ...         ...          ...          ...
   L_2      | 23-JUL-16  | Non_EX      |  99
   L_2      | 24-JUL-16  | EX          |  79
   ...         ...          ...          ...

谢谢!

2 个答案:

答案 0 :(得分:0)

这适用于您的情况,请尝试。

 SELECT location,date1, min(condition),Price 
from location group by location, date1,Price

更新以包含价格。

答案 1 :(得分:0)

如果位置 date_1 的每个组合价格相同,请关注@Prashant Majhwar。但是,如果各个 EX Non_EX 之间的价格可能不同,您可能需要尝试

WITH
Data_Raw (location_ID, date_1, ex_Start, ex_End, condition, price) AS (
  SELECT 'L_1', '23-JUL-16', '08-JUN-16', '25-JUL-16', 'EX', 109 FROM DUAL UNION ALL
  SELECT 'L_1', '23-JUL-16', '28-JUL-16', '31-JUL-16', 'Non_EX', 110 FROM DUAL UNION ALL
  SELECT 'L_1', '24-JUL-16', '08-JUN-16', '25-JUL-16', 'EX', 89 FROM DUAL UNION ALL
  SELECT 'L_1', '24-JUL-16', '28-JUL-16', '31-JUL-16', 'Non_EX', 90 FROM DUAL UNION ALL
  SELECT 'L_2', '23-JUL-16', '24-JUL-16', '15-AUG-16', 'Non_EX', 99 FROM DUAL UNION ALL
  SELECT 'L_2', '23-JUL-16', '26-OCT-16', '29-JAN-17', 'Non_EX', 99 FROM DUAL UNION ALL
  SELECT 'L_2', '24-JUL-16', '24-JUL-16', '15-AUG-16', 'EX', 79 FROM DUAL UNION ALL
  SELECT 'L_2', '24-JUL-16', '26-OCT-16', '29-JAN-17', 'Non_EX', 80 FROM DUAL
),
Data (location_ID, date_1, ex_Start, ex_End, condition, price) AS (
SELECT
  location_Id, TO_DATE(date_1, 'DD-MON-RR'), TO_DATE(ex_Start, 'DD-MON-RR')
  , TO_DATE(ex_End, 'DD-MON-RR'), condition, price
FROM Data_Raw
)
SELECT DISTINCT
  M.location_ID
  , TO_CHAR (M.date_1, 'DD-MON-RR') date_1
  , M.condition
  , D.price
FROM  
  (SELECT
    location_ID
    , date_1
    , MIN(condition) condition
  FROM Data
  GROUP BY location_ID, date_1
  ) M
JOIN Data D
  ON M.location_ID = D.location_ID
  AND M.date_1 = D.date_1
  AND M.condition = D.condition
ORDER BY location_id, date_1
;

有:

  • 使用子选择放大所需的条件
  • JOIN返回您的数据以提取价格(并且允许与其他列一起执行此操作 - 正如您的问题标题所示),并且
  • DISTINCT以防止 Non_EX
  • 的重复行

请评论是否需要调整/进一步详细说明。