获取单行中的行总和 SQL

Get sum of rows in a single row SQL

我正在从如下结构的视图中获取数据

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 列 = 'Platform Total' 并且 B 列和 C 列具有相同的值。尝试使用自连接但无法弄清楚。

感谢您花时间回答这个问题。

只需使用 UNION

SELECT A,B,C,D FROM Table1
UNION
SELECT 'Total',SUM(B),SUM(C),SUM(D) FROM Table2
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

您可以使用 window 函数和条件参数:

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;