If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Help on rollup

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-21-06, 11:05
arjun_me arjun_me is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Help on rollup

Hi

I need the equivalent of the following query in oracle without the rollup construct which gives the same result set as using the rollup.

SELECT
DISTINCT
decode(a1.a, null, 0, 1) dec1 ,
a1.a, a1.b,a1.c from test_groupby a1
GROUP BY rollup(a1.a, a1.b, a1.c)


thanks
Reply With Quote
  #2 (permalink)  
Old 01-21-06, 16:55
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Something like this?
Code:
SELECT dec1, a, b, c FROM (
  SELECT DECODE(a, NULL, 0, 1) dec1, a, b, c FROM test_groupby GROUP BY a, b, c
    UNION
  SELECT DECODE(a, NULL, 0, 1) dec1, a, b, NULL FROM test_groupby GROUP BY a, b, NULL
    UNION
  SELECT DECODE(a, NULL, 0, 1) dec1, a, NULL, NULL FROM test_groupby GROUP BY a, NULL, NULL
    UNION
  SELECT DECODE(a, NULL, 0, 1) dec1, NULL, NULL, NULL FROM test_groupby
)
ORDER BY a, b, c;
Reply With Quote
  #3 (permalink)  
Old 01-23-06, 01:55
arjun_me arjun_me is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Hi Littlefoot

Thanks for the equivalent query .
Reply With Quote
  #4 (permalink)  
Old 01-23-06, 05:24
arjun_me arjun_me is offline
Registered User
 
Join Date: Jan 2006
Posts: 9
Hi

I applied the same method for the following query

SELECT DISTINCT (decode(a1.a, null, 0, 1) + decode(a1.b, null, 0, 1) )dec1 ,
a1.a, a1.b,a1.c from test_groupby a1
GROUP BY rollup(a1.a, a1.b, a1.c)

The below query without rollup throws the error Error: ORA-00979: not a GROUP BY expression

SELECT dec1, a, b, c FROM (
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, b, c FROM test_groupby GROUP BY a, b, c
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, b, NULL FROM test_groupby GROUP BY a, b, NULL
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, NULL, NULL FROM test_groupby GROUP BY a, NULL, NULL
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, NULL, NULL, NULL FROM test_groupby
)
ORDER BY a, b, c

So, I added the column b in the group by statement of

SELECT dec1, a, b, c FROM (
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, b, c FROM test_groupby GROUP BY a, b, c
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, b, NULL FROM test_groupby GROUP BY a, b, NULL
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, a, NULL, NULL FROM test_groupby GROUP BY a,b, NULL, NULL
UNION
SELECT (decode(a, null, 0, 1) + decode(b, null, 0, 1) ) dec1, NULL, NULL, NULL FROM test_groupby
)ORDER BY a, b, c

but the result set of the query is not the same as the query with the rollup construct.

Please explain what is wrong with the query and why the result set varies?.
Reply With Quote
  #5 (permalink)  
Old 01-23-06, 07:05
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
You should have added

(DECODE(a, NULL, 0, 1) + DECODE(b, NULL, 0, 1))

into the GROUP BY clause instead of only "b".

Why are results different? You mean, a new query has value of 2 in the "dec1" column, while the old query has value 1 in there?

Old: DECODE(a, NULL, 0, 1)
New: (DECODE(a, NULL, 0, 1) + DECODE(b, NULL, 0, 1))

See the difference?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On