在SQL的一列中计算不同的行

时间:2019-02-04 13:32:16

标签: sql google-bigquery

我有一个包含以下信息的表,我正在使用Google BigQuery。我正在尝试根据person_ID根据一些不同类型的计算,中间和初始,结束和初始以及结束和初始之间的天数进行汇总。

|Person_ID|Action |Date       |
|100      |Initial|22/12/2018 |
|100      |Middle |23/12/2018 |
|100      |End    |29/12/2018 |
|100      |Close  |31/12/2018 |
|150      |Initial|02/01/2019 |
|150      |Middle |04/01/2019 |
|150      |End    |07/01/2019 |
|150      |Close  |10/01/2019 |

我试图得出如下结果

|Person_ID|Middle_Minus_initial|End_Minus_initial|Close_Minus_initial|
|100      |         1          |         7       |          9        |
|150      |         2          |         5       |          8        |

我真的不太确定该怎么做,因为我是SQL的初学者,因此可以提供任何帮助。谢谢。

3 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT Person_ID,
  DATE_DIFF(Middle, Initial, DAY) AS Middle_Minus_initial,
  DATE_DIFF(`End`, Initial, DAY) AS End_Minus_initial,
  DATE_DIFF(Close, Initial, DAY) AS Close_Minus_initial
FROM (
  SELECT Person_ID, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Initial', `Date`, NULL))) AS Initial, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Middle', `Date`, NULL))) AS Middle, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'End', `Date`, NULL))) AS `End`, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Close', `Date`, NULL))) AS Close
  FROM `project.dataset.table`
  GROUP BY Person_ID
)

您可以使用问题中的示例数据来测试,玩游戏,如下例所示

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 100 Person_ID, 'Initial' Action, '22/12/2018' `Date` UNION ALL 
  SELECT 100, 'Middle', '23/12/2018' UNION ALL 
  SELECT 100, 'End', '29/12/2018' UNION ALL 
  SELECT 100, 'Close', '31/12/2018' UNION ALL 
  SELECT 150, 'Initial', '02/01/2019' UNION ALL 
  SELECT 150, 'Middle', '04/01/2019' UNION ALL 
  SELECT 150, 'End', '07/01/2019' UNION ALL 
  SELECT 150, 'Close', '10/01/2019' 
)
SELECT Person_ID,
  DATE_DIFF(Middle, Initial, DAY) AS Middle_Minus_initial,
  DATE_DIFF(`End`, Initial, DAY) AS End_Minus_initial,
  DATE_DIFF(Close, Initial, DAY) AS Close_Minus_initial
FROM (
  SELECT Person_ID, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Initial', `Date`, NULL))) AS Initial, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Middle', `Date`, NULL))) AS Middle, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'End', `Date`, NULL))) AS `End`, 
    PARSE_DATE('%d/%m/%Y', MAX(IF(Action = 'Close', `Date`, NULL))) AS Close
  FROM `project.dataset.table`
  GROUP BY Person_ID
)
-- ORDER BY Person_ID

有结果

Row Person_ID   Middle_Minus_initial    End_Minus_initial   Close_Minus_initial  
1   100         1                       7                   9    
2   150         2                       5                   8    

答案 1 :(得分:0)

一种方法是条件聚合:

select person_id,
       date_diff(max(case when action = 'Middle' then date end),
                 max(case when action = 'Initial' then date end),
                 day) as middle_minus_initial,
       date_diff(max(case when action = 'End' then date end),
                 max(case when action = 'Initial' then date end),
                 day) as end_minus_initial,
       date_diff(max(case when action = 'Close' then date end),
                 max(case when action = 'Initial' then date end),
                 day) as close_minus_initial
from t
group by person_id;

答案 2 :(得分:0)

另一个避免使用聚合的选项是加入多个子查询,例如:

SELECT
    t.personid, 
    DATEDIFF(tm.date, ti.date, day) Middle_Minus_initial,
    DATEDIFF(te.date, ti.date, day) End_Minus_initial,
    DATEDIFF(tc.date, ti.date, day) Close_Minus_initial 
FROM 
    (SELECT DISTINCT personid FROM mytable) t
    LEFT JOIN mytable ti ON ti.personid = t.personid  AND ti.action = 'Initial'
    LEFT JOIN mytable tm ON tm.personid = t.personid AND tm.action = 'Middle'
    LEFT JOIN mytable te ON te.personid = t.personid AND te.action = 'End'  
    LEFT JOIN mytable tc ON tc.personid = t.personid AND tc.action = 'Close'