我从类似于以下结构的视图中获取数据
Column A Column B Column C Column D Column E
Platform Total Plant A Product A Date NULL
Platform 1 Plant A Product A Date 100
Platform 2 Plant A Product A Date 200
Platform Total Plant B Product B Date NULL
Platform 5 Plant B Product B Date 150
Platform 6 Plant B Product B Date 250
如何以下列格式获取数据
Column A Column B Column C Column D Column E
Platform Total Plant A Product A Date 300
Platform 1 Plant A Product A Date 100
Platform 2 Plant A Product A Date 200
Platform Total Plant B Product B Date 400
Platform 5 Plant B Product B Date 150
Platform 6 Plant B Product B Date 250
即,E列应为E列中的值的总和,其中A列='平台总数',B列和C列具有相同的值。尝试自我加入,但无法弄明白。
感谢您抽出宝贵时间回答这个问题。
答案 0 :(得分:3)
您可以使用窗口函数和条件参数:
select t.*,
coalesce(t.e,
sum(e) over (partition by t.b, t.c)
) as e
from t;
实际上,这只是查找null
值。您希望将其设置为a
中的特定值:
select t.a, t.b, t.c, t.d,
(case when t.a = 'Platform Total'
then sum(t.e) over (partition by t.b, t.c)
else t.e
end) as e
from t;
答案 1 :(得分:0)
只需使用UNION
即可SELECT A,B,C,D FROM Table1
UNION
SELECT 'Total',SUM(B),SUM(C),SUM(D) FROM Table2
答案 2 :(得分:0)
select ColumnA, ColumnB, ColumnC, ColumnD, Column E
from table
where ColumnA <> 'Platform Total'
union all
select 'Platform Total', ColumnB, ColumnC, max(ColumnD), sum(Column E)
from table
where ColumnA <> 'Platform Total'
group by ColumnB, ColumnC