cancel
Showing results for 
Search instead for 
Did you mean: 

SAP HANA Usage of Grouping

Former Member
0 Kudos

Hi folks,

I'm new to SAP HANA and need your help with understanding how GROUPING SETS on HANA works.

By executing of this statement


SELECT      COUNTRY, LANGU, "SHORT TEXT",

GROUPING(COUNTRY), GROUPING(LANGU), GROUPING("SHORT TEXT"),

GROUPING_ID(COUNTRY, LANGU, "SHORT TEXT")

FROM COUNTRIES

GROUP BY GROUPING SETS ((COUNTRY, LANGU), (LANGU, "SHORT TEXT"))

on this raw table

COUNTRYLANGUSHORT TEXT
DEDDeutschland
DEEGermany
USDUSA
USEUSA

I'm getting these results:

COUNTRYLANGUSHORT TEXTGROUPING(COUNTRY)GROUPING(LANGU)GROUPING(SHORT TEXT)GROUPING_ID
DED?0011
USD?0011
DEE?0011
USE?0011
?DDeutschland1004
?EGermany1004
?DUSA1004
?EUSA1004

Two questions raised here:

  • Why is Grouping(...) returning 1 when attribute is NULL and 0 when grouped? Has it not to be reversely?
  • And what about Grouping_ID, I have expected to see 1 and 2, but not 4... What are respective rules here?

Regards,

Oleg

Accepted Solutions (1)

Accepted Solutions (1)

lbreddemann
Active Contributor
0 Kudos

See GROUPING_ID - SAP HANA SQL and System Views Reference - SAP Library

and GROUPING - SAP HANA SQL and System Views Reference - SAP Library

Indeed the output of GROUPING seems to be exactly inverted to what the documentation tells us.

In this example GROUPING returns  0 for columns used in the current grouping.

GROUPING_ID seems to be calculated correctly though:



first group :    ((COUNTRY, LANGU,  "SHORT TEXT"))

bit vector:         0     0        1        -> 001 -> 1

second group:  ((COUNTRY, LANGU , "SHORT TEXT"))

bit vector:         1      0       0        -> 100 -> 4

- Lars

Former Member
0 Kudos

Thanks Lars.


I just was wondering why I'm getting different results as showed in the documentation.

alanmcshane
Advisor
Advisor
0 Kudos

FYI I found a couple of bugs with GROUPING about a month ago and raised an internal bug.

The 2 issues are...

1. GROUPING( <column name> ) returns 1 when should be 0 and 0 when should be 1

2. GROUPING can return values > 1.

Good news - the issue has been fixed internally so is just waiting to be merged into internal releases/testing etc.

regards,

Alan

lbreddemann
Active Contributor
0 Kudos

Great feedback on these issues Alan!

Thanks for sharing this.

I guess the GROUPING SETS related functions are heavily underused, so there might still be a few surprises in there...

- Lars

Answers (0)