如何计算同一列中但不同类别的日期之间的差异?

时间:2016-12-17 00:09:11

标签: sql google-bigquery tableau

我确信有人会指出这是一个基本的重塑问题,但我想我在Googling很穷,所以我将问题提交给Stack Overflow的众神。

我的问题是我想计算来自同一列但在另一列(Rank)中具有不同值的两个日期(等级1和等级2)之间的天数。以下是数据集架构的示例:

User   Date        Rank
Bob    2016-12-01    3
Bob    2016-12-07    2
Bob    2016-12-10    1

我想要的是:

User   Date1        Date2        DaysBetween
Bob    2016-12-07   2016-12-10        3

理想情况下,我想在Tableau中执行此操作,但SQL / BigQuery也可以。

2 个答案:

答案 0 :(得分:1)

尝试以下

WITH YourTable AS (
  SELECT 'Bob' AS User, DATE '2016-12-01' AS Date, 3 AS Rank UNION ALL
  SELECT 'Bob' AS User, DATE '2016-12-07' AS Date, 2 AS Rank UNION ALL
  SELECT 'Bob' AS User, DATE '2016-12-10' AS Date, 1 AS Rank 
)
SELECT 
  User, 
  MAX(CASE WHEN Rank = 2 THEN Date END) AS Date1,
  MAX(CASE WHEN Rank = 1 THEN Date END) AS Date2,
  DATE_DIFF(MAX(CASE WHEN Rank = 1 THEN Date END), 
            MAX(CASE WHEN Rank = 2 THEN Date END), DAY) AS DaysBetween
FROM YourTable
GROUP BY User

注意 - 这是在BigQuery Standard SQL

答案 1 :(得分:1)

另一个选择

WITH YourTable AS (
  SELECT 'Bob' AS User, DATE '2016-12-01' AS Date, 3 AS Rank UNION ALL
  SELECT 'Bob' AS User, DATE '2016-12-07' AS Date, 2 AS Rank UNION ALL
  SELECT 'Bob' AS User, DATE '2016-12-10' AS Date, 1 AS Rank 
)
SELECT 
  User, Date1, Date2, 
  DATE_DIFF(Date2, Date1, DAY) AS DaysBetween
FROM (
  SELECT 
    User, Rank, Date as Date2, 
    LEAD(Date) OVER(PARTITION BY User ORDER BY Date DESC) AS Date1
  FROM YourTable
)
WHERE Rank = 1