473,785 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,p rimary,morpholo gy) 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(primar y_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,p rimary,morpholo gy )
order by primary_site,pr imary,morpholog y

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 4838
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(morpho logy,'') 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_sitepr e, morphologypre, primarypre, count) AS (
SELECT CASE GROUPING(primar y_site)
WHEN 1 THEN 'ZZ-TOTAL' ELSE primary_site END AS primary_sitepre ,
CASE GROUPING (morphology)
WHEN 1 THEN 'ZZ-TOTAL' ELSE COALESCE(morpho logy,'ZY') END AS
morphologypre,
CASE GROUPING (primary)
WHEN 1 THEN 'ZZ-TOTAL' ELSE COALESCE(primar y,'ZY') END AS primarypre,
COUNT(*) AS COUNT
FROM session.summary _table
GROUP BY ROLLUP (primary_site,p rimary,morpholo gy )
)
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,mor phologypre

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,p rimary,morpholo gy) 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(primar y_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,p rimary,morpholo gy )
order by primary_site,pr imary,morpholog y

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_summ ary group by rollup
(primary_site,p rimary,morpholo gy )
HAVING GROUPING(primar y_site)=0 AND GROUPING(morpho logy)=0 and
grouping(primar y)=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
5326
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) FROM ORDERS O, REGION R
2
7585
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 select. I have a proc with 25 fields, and have no desire to Group By anything- when a certain field changes, I just want a row with the total of that field. Is this possible?
1
5383
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 product then child product? Help appreciated. Thanks. Frank SQL: SELECT CASE WHEN (Grouping(CategoryName)=1) THEN 'MainTotal'
1
2566
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 matched 1 row in Table1. As a consequence the count of rows in table1 is bigger than the real number of rows contained in table1. For example:
1
1563
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 generated null column rows. In an ASP.Net form, I have a datagrid.DataSource bound to the query result from a DataReader. The grid has AutoGenerateColumns = false. I manually add 1st and 2nd columns of the datagrid. The query result is bound to...
0
3905
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 duplicated on the rollup column). For example. if I order by date with rollup and, I am also outputting emplyee, and some numeric value , at the rollup row, the totals appear, that date, of course, properly appears, but the employee name from...
1
3484
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
4285
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 was just a linked server performance issue, but my optimization started running today on one of the "update 2" instances and so far it's been running about 10 hours longer than it normally
0
1530
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 data that would work well (very well in fact) defined as XML documents and I want to enrich that data with additional User defined information (child and/or attribute) using an XML application. Document specifics 1. Document size 150k - 300k...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.