哈娜 SQL 需要逻辑

Hana SQL Logic needed

select  groupid,count(value) as x
from test_distinct_count
group by rollup (groupid);

此查询给出如下输出:

我需要的是在总计列中显示所有 groupid 的值 5。 像这样:

如何使用 Hana Sql。

一个选项如下:

with grp as ( select  groupid, count(value) as x
              from test_distinct_count
              group by rollup (groupid) ),
     cnt as ( select count(value) as total from test_distinct_count )
select grp.groupid, grp.x, cnt.total from grp cross join cnt;

来自sql服务器后台希望sum() over()会存在于Hana中,试试下面的解决方案

create table #temp(groupid int,value int)

insert into #temp values(1,1)
insert into #temp values(1,1)
insert into #temp values(1,1)
insert into #temp values(2,2)
insert into #temp values(2,2)

select 
a.*,sum(x) over()/2 as Total
from (
select  groupid,count(value) as x
from #temp
group by rollup (groupid)
)a

创建 table #temp(groupid int,value int)

插入#temp 值(1,1) 插入#temp 值(1,1) 插入#temp 值(1,1) 插入#temp 值(2,2) 插入#temp 值(2,2)

select * 来自 ( select groupid,count(value) 作为 x 来自#temp 按汇总分组(groupid) )交叉申请 ( select count(value) 作为总计 来自#temp )b

先做window函数,再做汇总:

select groupid, count(value), max(total) as total
from (select groupid, value,
             sum(case when value is not null then 1 end) over () as total
      from test_distinct_count
      group by groupid
     ) t
group by rollup (groupid);

如果您想向 group by 添加更多列,这会更安全。