473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need ROLLUP expert advice!

Hi all,

I need some expert advice on a ROLLUP fine point.

summary_table table has 4 columns:

file_id
primary_site
morphology
primary

I want a rollup by (primary_site,primary,morphology) where the totals
appear at the
bottom of the sumamry lines. So I used a CASE assigning a "ZZ..." name
to the totals:

select case grouping(primary_site)
when 1 then 'ZZ--TOTAL' else primary_site end as primary_site,
case grouping (morphology)
when 1 then 'ZZ--TOTAL' else morphology end as morphology,
case grouping (primary)
when 1 then 'ZZ--TOTAL' else primary end as primary,
count(*) as COUNT from summary_table group by rollup
(primary_site,primary,morphology )
order by primary_site,primary,morphology

This works, but,since I have rows with <nulls> in some columns, the
<null> entries
sort AFTER the "ZZ..." rows, so it looks ugly:

PRIMARY_SITE MORPHOLOGY PRIMARY COUNT
------------------------------------------------------------------
Bladder Carcinoma Primary 1
Bladder Mucinous adenocarcinoma Primary 1
Bladder Transitional cell carcinoma Primary 4
Bladder ZZ--TOTAL Primary 11
Bladder <null> Primary 5
Bladder ZZ--TOTAL ZZ--TOTAL 27
Bladder ZZ--TOTAL <null> 16
Bladder <null> <null> 16
.......................

I'd like one of 2 things:

(a) Remove the total and subtotals from the output or
(b) Find a way for the "ZZ--TOTAL" lines to appear after the <null>s
so the output would look like this:

PRIMARY_SITE MORPHOLOGY PRIMARY COUNT
------------------------------------------------------------------
Bladder Carcinoma Primary 1
Bladder Mucinous adenocarcinoma Primary 1
Bladder Transitional cell carcinoma Primary 4
Bladder <null> Primary 5
Bladder ZZ--TOTAL Primary 11
Bladder <null> <null> 16
Bladder ZZ--TOTAL <null> 16
Bladder ZZ--TOTAL ZZ--TOTAL 27
.......................

Any ideas?

Thanks,

Alejandrina

Apr 3 '06 #1
3 4812
You're right; the NULL values certainly do complicate the task at hand.
The problem I see is that COALESCE by itself doesn't help much either,
since COALESCE(morphology,'') would result in whitespace that would
still show up after the ZZ-TOTAL row in the sort. However, we can set
NULL values to ZY, which places them just before their respective
totals.

There may be more elegant ways to handle this, but I happen to know
that a common table expression could do the job satisfactorily. The
trick is to use the common table expression to change the NULLs to
sort-friendly values (either ZY or ZZ-TOTAL) and then issue a separate
query that selects from that expression. This gives you a two-step
process in which you can modify and then un-modify the values as
needed.

WITH unsorted (primary_sitepre, morphologypre, primarypre, count) AS (
SELECT CASE GROUPING(primary_site)
WHEN 1 THEN 'ZZ-TOTAL' ELSE primary_site END AS primary_sitepre,
CASE GROUPING (morphology)
WHEN 1 THEN 'ZZ-TOTAL' ELSE COALESCE(morphology,'ZY') END AS
morphologypre,
CASE GROUPING (primary)
WHEN 1 THEN 'ZZ-TOTAL' ELSE COALESCE(primary,'ZY') END AS primarypre,
COUNT(*) AS COUNT
FROM session.summary_table
GROUP BY ROLLUP (primary_site,primary,morphology )
)
SELECT
CASE primary_sitepre WHEN 'ZZ-TOTAL' THEN 'TOTAL' ELSE primary_sitepre
END AS primary_site,
CASE morphologypre WHEN 'ZZ-TOTAL' THEN 'TOTAL' WHEN 'ZY' THEN '' ELSE
morphologypre END AS morphology,
CASE primarypre WHEN 'ZZ-TOTAL' THEN 'TOTAL' WHEN 'ZY' THEN '' ELSE
primarypre END AS primary,
count AS count
FROM unsorted
ORDER BY primary_sitepre,primarypre,morphologypre

The second query allows us to create a second set of colums that are
derived from the first set of values in each row. By doing this, we can
display one set of columns (which look nice) while sorting against the
others (which sort properly).

Good luck,

Fred

apattin wrote:
Hi all,

I need some expert advice on a ROLLUP fine point.

summary_table table has 4 columns:

file_id
primary_site
morphology
primary

I want a rollup by (primary_site,primary,morphology) where the totals
appear at the
bottom of the sumamry lines. So I used a CASE assigning a "ZZ..." name
to the totals:

select case grouping(primary_site)
when 1 then 'ZZ--TOTAL' else primary_site end as primary_site,
case grouping (morphology)
when 1 then 'ZZ--TOTAL' else morphology end as morphology,
case grouping (primary)
when 1 then 'ZZ--TOTAL' else primary end as primary,
count(*) as COUNT from summary_table group by rollup
(primary_site,primary,morphology )
order by primary_site,primary,morphology

This works, but,since I have rows with <nulls> in some columns, the
<null> entries
sort AFTER the "ZZ..." rows, so it looks ugly:

PRIMARY_SITE MORPHOLOGY PRIMARY COUNT
------------------------------------------------------------------
Bladder Carcinoma Primary 1
Bladder Mucinous adenocarcinoma Primary 1
Bladder Transitional cell carcinoma Primary 4
Bladder ZZ--TOTAL Primary 11
Bladder <null> Primary 5
Bladder ZZ--TOTAL ZZ--TOTAL 27
Bladder ZZ--TOTAL <null> 16
Bladder <null> <null> 16
......................

I'd like one of 2 things:

(a) Remove the total and subtotals from the output or
(b) Find a way for the "ZZ--TOTAL" lines to appear after the <null>s
so the output would look like this:

PRIMARY_SITE MORPHOLOGY PRIMARY COUNT
------------------------------------------------------------------
Bladder Carcinoma Primary 1
Bladder Mucinous adenocarcinoma Primary 1
Bladder Transitional cell carcinoma Primary 4
Bladder <null> Primary 5
Bladder ZZ--TOTAL Primary 11
Bladder <null> <null> 16
Bladder ZZ--TOTAL <null> 16
Bladder ZZ--TOTAL ZZ--TOTAL 27
......................

Any ideas?

Thanks,

Alejandrina


Apr 4 '06 #2
CLEVER!

BTW, I found out how to remove the totals from the rollup, in case
anyone is interested:

select primary_site , primary, morphology,
count(*) as COUNT from morphology_summary group by rollup
(primary_site,primary,morphology )
HAVING GROUPING(primary_site)=0 AND GROUPING(morphology)=0 and
grouping(primary)=0
order by primary_site , primary, morphology

the magic is "HAVING GROUPING(xxx)=0 " only selects the non-total rows.

Thanks!

Alejandrina

Apr 4 '06 #3
If you want to suppress totals from a GROUP BY ROLLUP, perhaps you
should use a different grouping method, such as GROUP BY GROUPING SETS,
or a straight GROUP BY(primary_site, primary, morphology)

Am I missing something?

Apr 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: UNIXNewBie | last post by:
Am looking at an Oracle SQL reference book. They have the following SQL for ROLLUP which works SELECT 0.YEAR, TO_CHAR(TO_DATE(O.MONTH, 'MM'), 'Month') MONTH, R.NAME REGION, SUM(O.TOT_SALES)...
2
by: Burt | last post by:
MS has been nice enough to add the Cube and Rollup operators so I can have totals with my results. But out of shear meanness they won't let me use them unless I use a Group By clause in my...
1
by: Frank Py | last post by:
I found this great rollup query example that uses grouping and sub totals. Would it be possible to expand on this and include a group within a group and subtotal each group? For example, parent...
1
by: c.le_roq | last post by:
Hello, Using rollup I want to count the number of rows of a table called Table1 which is LEFT JOINED with a table called Table2. The problem is that we can have more than 1 rows in Table2 that...
1
by: js | last post by:
I have a SQL Server 2003 stored procedure that uses "select ... group by ... with rollup" syntax. The total of columns from the query varies from 3 to 4. The query returns several rollup...
0
by: Ike | last post by:
When I use "With rollup," I am getting totals on columns which are numeric, however, non-numeric columns are copying down to the rollup column (i.e. the last column's non-numeric columns are being...
1
by: vanandwiz | last post by:
Please find the code below. SELECT COMPANY.NAME, COUNT(ITEM.ID) FROM ITEM, COMPANY WHERE ITEM.COMPANYID = COMPANY.ID GROUP BY ROLLUP(COMPANY.NAME)
3
by: traceable1 | last post by:
I installed the SQL Server 2005 SP2 update 2 rollup on my 64-bit server and the performance has tanked! I installed rollup 3 on some of them, but that did not seem to help. I thought it...
0
by: onegative | last post by:
G'day Y'all, I was hoping to get some expert feedback on a proposal I am considering regarding a new internal application to help fill some gaps in our IT department. I have some configuration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.