Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulate ROLLUP, CUBE, and GROUPING SETS where they're not supported natively #1123

Open
lukaseder opened this issue Jul 22, 2012 · 0 comments

Comments

@lukaseder
Copy link
Member

lukaseder commented Jul 22, 2012

Here's how:

-- ROLLUP() with one argument 
SELECT AUTHOR_ID, COUNT(*)
FROM T_BOOK
GROUP BY ROLLUP(AUTHOR_ID)

-- ROLLUP() with two arguments
SELECT AUTHOR_ID, PUBLISHED_IN, COUNT(*)
FROM T_BOOK
GROUP BY ROLLUP(AUTHOR_ID, PUBLISHED_IN)

With UNION ALL:

-- The same query using UNION ALL:
  SELECT AUTHOR_ID, COUNT(*) FROM T_BOOK GROUP BY (AUTHOR_ID)
UNION ALL
  SELECT NULL, COUNT(*) FROM T_BOOK GROUP BY ()
ORDER BY 1 NULLS LAST

-- The same query using UNION ALL:
  SELECT AUTHOR_ID, PUBLISHED_IN, COUNT(*) 
  FROM T_BOOK GROUP BY (AUTHOR_ID, PUBLISHED_IN)
UNION ALL
  SELECT AUTHOR_ID, NULL, COUNT(*) 
  FROM T_BOOK GROUP BY (AUTHOR_ID)
UNION ALL
  SELECT NULL, NULL, COUNT(*) 
  FROM T_BOOK GROUP BY ()
ORDER BY 1 NULLS LAST, 2 NULLS LAST

This is also nicely explained here:
http://msdn.microsoft.com/en-us/library/bb510427(v=sql.105)

Prerequisites for this task:

@ghost ghost assigned lukaseder Jul 22, 2012
@lukaseder lukaseder changed the title Simulate ROLLUP, CUBE, and GROUPING SETS where they're not supported natively Emulate ROLLUP, CUBE, and GROUPING SETS where they're not supported natively May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant