sql server - GROUPING_ID functionality -
sql server - GROUPING_ID functionality -
i don't quite understand how function works, i've been looking on below documentation, , have issues.
http://msdn.microsoft.com/en-us/library/bb510624.aspx
so, understand how grouping()
works perfectly, output grouping_id
quite impossible me comprehend how it's done because it's not same explanation.
for illustration have next string of ones , zeroes: 010
. in documentation says it's equal 2. read in sql book each byte (the rightmost) is equal
2 @ powerfulness of byte position minus one.
so, (2^2 - 1) + (2^1 - 1 ) + (2^0 - 1), isn't same each binary number? (100/101/110/etc), , result isn't 2 either....
edit 1 : how explanation book is:
another function can utilize identify grouping sets grouping_id. function accepts list of grouped columns inputs , returns integer representing bitmap. rightmost bit represents rightmost input. bit 0 when respective element part of grouping set , 1 when isn’t. each bit represents 2 raised powerfulness of bit position minus 1; rightmost bit represents 1, 1 left of 2, 4, 8, , on. result integer sum of values representing elements not part of grouping set because bits turned on. here’s query demonstrating utilize of function.
there has error because there no way number calculated 2^(position) - 1, error ? i've been calculating 2^(bitposition) *1 , outputs correct. illustration i've done this
grouping_id(a,b,c), grouping(a), grouping(b), grouping(c)
and let's have next output
3, 0, 1, 1
so our binary string 011 , 3 output of grouping_id
function, if calculate string
2^0 * 1 + 2^1 * 1 + 2^0 *2 = 1 + 2 + 0 = 3
there no other logic see here, cannot calculate minus upper quote says, thing on msdn weirder definition seems similar one:
each grouping_id argument must element of grouping list. grouping_id () returns integer bitmap lowest n bits may lit.
a lit bit indicates corresponding argument not grouping column given output row. lowest-order bit corresponds argument n, , n-1th lowest-order bit corresponds argument 1.
first of all, when say
each bit represents 2 raised powerfulness of bit position minus 1
they not mean 2position - 1
rather 2position - 1
. apparently, purpose of description chose count bits 1 (for rightmost bit) rather 0.
secondly, each bit represents said value when set, i.e. when 1. so, naturally, not just
21 - 1 + 22 - 1 + ... + 2n - 1but rather
bit1 × 21 - 1 + bit2 × 22 - 1 + ... + bitn × 2n - 1which normal way of converting binary representation decimal 1 , method have shown near end of question.
sql-server tsql
Comments
Post a Comment