如何从 SQL Clickhouse 中的一行中减去其他行

How to substract from one row the other ones in SQL Clickhouse

我对行减法有疑问。 例如,执行此查询后:

select 
    case 
        when source in ('all') then 'all'
        when source in ('source1') then 'source1'
        when source in ('source2') then 'source2'
        when source in ('source3') then 'source3'
    end as source,
    
    sum(value) as sum
    
from table
group by 
    source

我有一个table:

source sum
all 100
source 1 1
source 2 1
source 3 1

我想向此 table 添加 'all-source1-source2-source3' 值为 100-1-1-1=97 的新行。你能帮帮我吗?

select (arrayJoin(flatten([sa, ss, sd])) as x).1 source, x.2 sum
FROM (
       select groupArrayIf((source, sum), source='all') sa, 
              groupArrayIf((source, sum), source!='all') ss,
              [('diff', arraySum(sa.2) - arraySum(ss.2))] sd
       from (
               select  'all'  source, 100 sum union all
               select  'source 1', 1 union all
               select  'source 2', 1 union all
               select  'source 3', 1 
       )
    )

┌─source───┬─sum─┐
│ all      │ 100 │
│ source 1 │   1 │
│ source 2 │   1 │
│ source 3 │   1 │
│ diff     │  97 │
└──────────┴─────┘