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

This query gives output like :

enter image description here

What i need is to display the value 5 for all the groupids in a column say Total. Something like this :

enter image description here

how can this be achieved using Hana Sql.

4 Answers 4

1

Am from sql server background hope sum() over() will exist in Hana, try the below solution

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
1

One option would be following:

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

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 * from ( select groupid,count(value) as x from #temp group by rollup(groupid) )a cross apply ( select count(value) as Total from #temp )b

1
  • Cross Apply Works best for your Scenario Dec 17, 2018 at 7:54
0

Do the window function first, then the rollup:

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);

This is safer if you want to add more columns to the group by.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.