比较日期范围的最新值? (SQL/Snowflake)
Latest value of compared date range? (SQL/Snowflake)
我有值 Table-A 喜欢:
Patient|Invoice|Date
A,111,2021-02-01
A,222,2021-01-01
B,333,2021-03-01
B,444,2021-02-01
C,555,2021-04-01
C,666,2021-03-01
Table-B 中的值如:
Patient|Value|Date
A,2,2021-01-05
A,3,2021-01-05
A,3,2021-02-05
B,1,2021-02-05
B,1,2021-03-05
C,6,2021-01-01
我想加入这两个表,以便我可以看到 Table-B 中截至日期 Table-A 中给定患者的最新累积值总和。
Patient|Invoice|Latest Value|Date
A,111,5,2021-02-01
A,222,0,2021-01-01
B,333,1,2021-03-01
B,444,0,2021-02-01
C,555,6,2021-04-01
C,666,6,2021-03-01
我如何按日期连接这两个表才能完成此操作?
第一步似乎是基本的 SQL 加入:
select patient, invoice, sum(value), date
from table1 a
join table2 b
on a.patient=b.patient
and a.date=b.date
group by patient, invoice, date
但是您可以应用 sum() over()
:
而不是普通的 sum()
select patient, invoice
, sum(value) over(partition by patient order by date)
, date
from table1 a
join table2 b
on a.patient=b.patient
and a.date=b.date
group by patient, invoice, date
我认为首先我们需要计算发票有效的时间间隔(使用LAG function), then calculate the cumulative SUM。
WITH A AS (
SELECT Patient, Invoice, Date, IFNULL(LAG(Date) OVER(PARTITION BY Patient ORDER BY Date), '1900-01-01') AS LG
FROM Table_A
)
SELECT DISTINCT A.Patient, A.Invoice, IFNULL(SUM(B.Value) OVER(PARTITION BY A.Patient ORDER BY A.Date), 0) AS Latest_Value, A.Date
FROM A
LEFT JOIN Table_B AS B
ON A.Patient = B.Patient
AND B.Date >= A.LG AND B.Date < A.Date
GROUP BY A.Patient, A.Invoice, A.Date, B.Value
ORDER BY A.Patient, A.Invoice, A.Date;
我有值 Table-A 喜欢:
Patient|Invoice|Date
A,111,2021-02-01
A,222,2021-01-01
B,333,2021-03-01
B,444,2021-02-01
C,555,2021-04-01
C,666,2021-03-01
Table-B 中的值如:
Patient|Value|Date
A,2,2021-01-05
A,3,2021-01-05
A,3,2021-02-05
B,1,2021-02-05
B,1,2021-03-05
C,6,2021-01-01
我想加入这两个表,以便我可以看到 Table-B 中截至日期 Table-A 中给定患者的最新累积值总和。
Patient|Invoice|Latest Value|Date
A,111,5,2021-02-01
A,222,0,2021-01-01
B,333,1,2021-03-01
B,444,0,2021-02-01
C,555,6,2021-04-01
C,666,6,2021-03-01
我如何按日期连接这两个表才能完成此操作?
第一步似乎是基本的 SQL 加入:
select patient, invoice, sum(value), date
from table1 a
join table2 b
on a.patient=b.patient
and a.date=b.date
group by patient, invoice, date
但是您可以应用 sum() over()
:
sum()
select patient, invoice
, sum(value) over(partition by patient order by date)
, date
from table1 a
join table2 b
on a.patient=b.patient
and a.date=b.date
group by patient, invoice, date
我认为首先我们需要计算发票有效的时间间隔(使用LAG function), then calculate the cumulative SUM。
WITH A AS (
SELECT Patient, Invoice, Date, IFNULL(LAG(Date) OVER(PARTITION BY Patient ORDER BY Date), '1900-01-01') AS LG
FROM Table_A
)
SELECT DISTINCT A.Patient, A.Invoice, IFNULL(SUM(B.Value) OVER(PARTITION BY A.Patient ORDER BY A.Date), 0) AS Latest_Value, A.Date
FROM A
LEFT JOIN Table_B AS B
ON A.Patient = B.Patient
AND B.Date >= A.LG AND B.Date < A.Date
GROUP BY A.Patient, A.Invoice, A.Date, B.Value
ORDER BY A.Patient, A.Invoice, A.Date;