SQL, left join table, 如何只保留一个值?
SQL, left join table, How to keep only one value?
我想加入两个 table。
table 1 是这样的:
id Date
a 01.01.2021
a 02.01.2021
a 03.01.2021
b 01.01.2021
b 02.01.2021
b 03.01.2021
c 01.01.2021
c 02.01.2021
c 03.01.2021
table2是这样的:
id value
a 12
a 8
b 50
作为最终结果,我想要一个像这样的 table:
对于来自 table 2 的现有 id,相同 id 的值的总和应存储在 table 1.
的最后日期
id Date Value_FINAL
a 01.01.2021 0
a 02.01.2021 0
a 03.01.2021 20
b 01.01.2021 0
b 02.01.2021 0
b 03.01.2021 50
c 01.01.2021 0
c 02.01.2021 0
c 03.01.2021 0
我一开始尝试用left join来连接这两个table,
with t3 as ( select id, sum(value) Value_FINAL from t2 group by id)
select t1.*, t3.value_FINAL from t1 left join t3 on t1.id = t3.id;
在此之后,我可以得到这个:
id Date Value_FINAL
a 01.01.2021 20
a 02.01.2021 20
a 03.01.2021 20
b 01.01.2021 50
b 02.01.2021 50
b 03.01.2021 50
c 01.01.2021 0
c 02.01.2021 0
c 03.01.2021 0
但是,这不是我想要的。有人可以帮忙吗?如何仅将值保留在 'value_FINAL' 列的最后一个日期中
我也在考虑使用last_value(value) over (partition by id order by date)
。但我需要创建一个额外的 table 或列。
也许有人知道如何处理这个问题?
使用row_number()
。一种方法是:
select t1.*,
(case when row_number() over (partition by id order by date desc) = 1
then (select coalesce(sum(t2.value), 0) from table2 t2 where t2.id = t1.id)
else 0
end) as value_final
from table1 t1;
您可以使用 ROW_NUMBER
来确定要放置总值的行。
例如:
select
t1.id, t1.date, b.total,
case when
row_number() over(partition by t1.id order by t1.date desc) = 1
then b.total
else 0 end as value_final
from t1
left join (select id, sum(value) as total from t2 group by id) b
on b.id = t1.id
基于 join
的替代方案
select a.*, coalesce(c.value,0)
from t1 a
left join (select id, max(date) date from t1 group by id) b on a.id = b.id and a.date = b.date
left join (select id, sum(value) value from t2 group by id) c on b.id = c.id
我想加入两个 table。 table 1 是这样的:
id Date
a 01.01.2021
a 02.01.2021
a 03.01.2021
b 01.01.2021
b 02.01.2021
b 03.01.2021
c 01.01.2021
c 02.01.2021
c 03.01.2021
table2是这样的:
id value
a 12
a 8
b 50
作为最终结果,我想要一个像这样的 table: 对于来自 table 2 的现有 id,相同 id 的值的总和应存储在 table 1.
的最后日期 id Date Value_FINAL
a 01.01.2021 0
a 02.01.2021 0
a 03.01.2021 20
b 01.01.2021 0
b 02.01.2021 0
b 03.01.2021 50
c 01.01.2021 0
c 02.01.2021 0
c 03.01.2021 0
我一开始尝试用left join来连接这两个table,
with t3 as ( select id, sum(value) Value_FINAL from t2 group by id)
select t1.*, t3.value_FINAL from t1 left join t3 on t1.id = t3.id;
在此之后,我可以得到这个:
id Date Value_FINAL
a 01.01.2021 20
a 02.01.2021 20
a 03.01.2021 20
b 01.01.2021 50
b 02.01.2021 50
b 03.01.2021 50
c 01.01.2021 0
c 02.01.2021 0
c 03.01.2021 0
但是,这不是我想要的。有人可以帮忙吗?如何仅将值保留在 'value_FINAL' 列的最后一个日期中
我也在考虑使用last_value(value) over (partition by id order by date)
。但我需要创建一个额外的 table 或列。
也许有人知道如何处理这个问题?
使用row_number()
。一种方法是:
select t1.*,
(case when row_number() over (partition by id order by date desc) = 1
then (select coalesce(sum(t2.value), 0) from table2 t2 where t2.id = t1.id)
else 0
end) as value_final
from table1 t1;
您可以使用 ROW_NUMBER
来确定要放置总值的行。
例如:
select
t1.id, t1.date, b.total,
case when
row_number() over(partition by t1.id order by t1.date desc) = 1
then b.total
else 0 end as value_final
from t1
left join (select id, sum(value) as total from t2 group by id) b
on b.id = t1.id
基于 join
的替代方案
select a.*, coalesce(c.value,0)
from t1 a
left join (select id, max(date) date from t1 group by id) b on a.id = b.id and a.date = b.date
left join (select id, sum(value) value from t2 group by id) c on b.id = c.id