需要Hana SQL逻辑

时间:2018-12-17 05:45:27

标签: sql hana

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

此查询给出的输出类似于:

enter image description here

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

enter image description here

如何使用Hana Sql做到这一点。

4 个答案:

答案 0 :(得分:1)

以下是一个选择:

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;

答案 1 :(得分:1)

来自sql server的后台希望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

答案 2 :(得分:1)

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

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

从( 选择groupid,count(value)作为x 来自#temp 按汇总分组(groupid) 交叉申请 ( 选择计数(值)作为总计 来自#temp )b

答案 3 :(得分:0)

先执行窗口功能,然后执行汇总:

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添加更多列,这样做会更安全。