我正在嘗試結合兩個查詢
select s.id , sum(h.val) as [VAL]
from identity s inner join price_net h on s.date = h.date and s.num_id = h.num_id and s.rec_type = h.rec_type
where s.date = '10/25/21'
and s.rec_type = 'D'
and s.tier = 'P'
group by s.
.
select s.id , sum(h.val) as [VAL]
from identity s inner join price_net h on s.date = h.date and s.num_id = h.num_id and s.rec_type = h.rec_type
where s.date = '10/26/21'
and s.rec_type = 'D'
and s.tier = 'P'
group by s.
我想要它,所以有一個表,其中一列用于 s.id,接下來的列是每個日期的 VAL 列。如果某個值在特定日期不可用于給定 s.id,則應顯示 NA。
uj5u.com熱心網友回復:
我假設您正在尋找條件聚合(CASE WHEN在聚合函式內)。
select
i.id,
sum(case when i.date = date '2021-10-25' then pn.val end) as val_20211025,
sum(case when i.date = date '2021-10-26' then pn.val end) as val_20211026
from identity i
join price_net pn on pn.date = i.date
and pn.num_id = i.num_id
and pn.rec_type = i.rec_type
where i.tier = 'P'
and i.rec_type = 'D'
and i.date in (date '2021-10-25', date '2021-10-26')
group by i.id
order by i.id;
uj5u.com熱心網友回復:
您可以使用 case when 對于這兩個條件
select s.id , sum(CASE WHEN s.date = '10/25/21' THEN h.val eLSE O END) as [VAL]
, sum(CASE WHEN s.date = '10/26/21' THEN h.val eLSE O END) as [VAL2]
from identity s inner join price_net h on s.date = h.date and s.num_id = h.num_id and s.rec_type = h.rec_type
where
as.rec_type = 'D'
and s.tier = 'P'
group by s.ìd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/365533.html
