﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Teradata Forums / Data Warehousing  / Teradata   / Grouping sets / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>Teradata Forums</description><link>http://www.teradata.com/teradataforum/</link><webMaster>info@teradata.com</webMaster><lastBuildDate>Wed, 03 Dec 2008 17:52:46 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Grouping sets</title><link>http://www.teradata.com/teradataforum/Topic2446-1-1.aspx</link><description>Dear Feroz,&lt;br&gt;&lt;br&gt;Unfortunately I will deal with this topic not before next week. But thanks for your ansver!&lt;br&gt;&lt;br&gt;</description><pubDate>Wed, 18 Jan 2006 07:39:51 GMT</pubDate><dc:creator>Apukad</dc:creator></item><item><title>RE: Grouping sets</title><link>http://www.teradata.com/teradataforum/Topic2446-1-1.aspx</link><description>Apukad, ( if you already go the results let me know if there is a good way)&lt;br&gt;i am not sure if this will work for you or not. I tried this example and could get results by using derived tables.&lt;br&gt;&lt;br&gt;in my example i had the following rows. ( id did not use all the tables that u had i just made a dummy table with the following rows.&lt;br&gt;&lt;br&gt;select * from devdata.feroz_test_group&lt;br&gt;order by 1&lt;br&gt;&lt;br&gt;	sale_date	sale_amt&lt;br&gt;	2005-01-11	30&lt;br&gt;	2005-01-11	20&lt;br&gt;	2005-01-11	10&lt;br&gt;	2005-01-12	33&lt;br&gt;	2005-01-12	22&lt;br&gt;	2005-01-12	11&lt;br&gt;	2006-01-01	300&lt;br&gt;	2006-01-01	200&lt;br&gt;	2006-01-01	100&lt;br&gt;	2006-01-02	303&lt;br&gt;	2006-01-02	202&lt;br&gt;	2006-01-02	101&lt;br&gt;&lt;br&gt;Now i am trying to get the totals by day, totals by month and grand total.&lt;br&gt;&lt;br&gt;select coalesce(a.month_num, 'Grand_Total') MONTHS, &lt;br&gt;case when a.month_num is  NULL then 'Grand_Total' &lt;br&gt;else (case when a.day_num is null then 'Monthly_Total' else a.day_num end) end DAYS, &lt;br&gt;a.gr_totals&lt;br&gt;from (&lt;br&gt;select&lt;br&gt;coalesce(cast((sale_date/100) as varchar(18)),'Total') month_id, &lt;br&gt;coalesce(cast(sale_date as varchar(18)), 'Total' ) day_of_month,&lt;br&gt;sum(sale_amt) as Totals&lt;br&gt;from devdata.feroz_test_group&lt;br&gt;group by grouping sets ((month_id,day_of_month ),(month_id),()) ) a(month_num, day_num, gr_totals)&lt;br&gt;order by 1 asc, 2 asc&lt;br&gt;&lt;br&gt;	MONTHS	         DAYS	        gr_totals&lt;br&gt;	10501	         2005-01-11	60&lt;br&gt;	10501	         2005-01-12	66&lt;br&gt;	10501	         Monthly_Total	126&lt;br&gt;	10601	         2006-01-01	600&lt;br&gt;	10601	         2006-01-02	606&lt;br&gt;	10601	         Monthly_Total	1206&lt;br&gt;	Grand_Total	 Grand_Total	1332&lt;br&gt;&lt;br&gt;&lt;br&gt;I could not get the same result as you got by using the coalesce statements, when i did this with out using the derived table &lt;br&gt;&lt;br&gt;select coalesce(cast((sale_date/100) as varchar(18)),'Total') month_id, &lt;br&gt; coalesce(cast(sale_date as varchar(18)), 'Total' ) day_of_month,&lt;br&gt; sum(sale_amt) as Totals&lt;br&gt;from devdata.feroz_test_group&lt;br&gt;group by grouping sets ((month_id,day_of_month ),(month_id),())&lt;br&gt;order by 1 asc, 2 asc&lt;br&gt;&lt;br&gt;	month_id	day_of_month	Totals&lt;br&gt;	?	        ?	        1332&lt;br&gt;	10501	        ?	        126&lt;br&gt;	10501	        2005-01-11	60&lt;br&gt;	10501	        2005-01-12	66&lt;br&gt;	10601	        ?	        1206&lt;br&gt;	10601	        2006-01-01	600&lt;br&gt;	10601	        2006-01-02	606&lt;br&gt;&lt;br&gt;this was not populating the NULL values I am not sure why??&lt;br&gt;&lt;br&gt;anyways try this if this is acceptable for you.&lt;br&gt;</description><pubDate>Thu, 12 Jan 2006 10:51:15 GMT</pubDate><dc:creator>feroz_shaik</dc:creator></item><item><title>Grouping sets</title><link>http://www.teradata.com/teradataforum/Topic2446-1-1.aspx</link><description>Hello All,&lt;br&gt;&lt;br&gt;I have a problem, because i would like to order the query result with totals. But the ordered fields format are decimal(8,0)-month_id and date ('YYYY-MM-DD'). I tried to correct with coalesce, but it does not work in case of month_id.&lt;br&gt;for example:&lt;br&gt;Select	&lt;br&gt;coalesce(cast((c.month_id) as varchar(18)), 'total') month_id,&lt;br&gt;coalesce(cast((a.date_of_day) as varchar(18)), 'total') date_of_day,&lt;br&gt;sum(a.sales) (decimal (18,0)) turnover_of_store,&lt;br&gt;sum(a.Inv) invoices_of_stores,&lt;br&gt;sum(b.sales) (decimal (18,0)) turnover_of_petrol_station,&lt;br&gt;sum(b.inv) invoices_of_petrol_station,&lt;br&gt;sum(e.sales) (decimal (18,0)) store_and_ps_both,&lt;br&gt;sum(e.inv) invoices_of_ps_and_st,&lt;br&gt;sum(f.sales) (decimal (18,0)) only_in_petrol_station,&lt;br&gt;sum(f.inv) invoices_only_ps,&lt;br&gt;sum(g.sales) (decimal (18,0)) non_cust_turnover,&lt;br&gt;sum(h.sales) (decimal (18,0)) noncust_petrol,&lt;br&gt;sum(g.inv) invoices_of_non_custs&lt;br&gt;&lt;br&gt;from&lt;br&gt;a&lt;br&gt;full outer join&lt;br&gt;b -- everywhere on ( a.date_of_day=b/c/d(...).date_of_day)&lt;br&gt;full outer join&lt;br&gt;d&lt;br&gt;full outer join&lt;br&gt;e &lt;br&gt;full outer join&lt;br&gt;f &lt;br&gt;full outer join&lt;br&gt;g&lt;br&gt;full outer join&lt;br&gt;h&lt;br&gt;left outer join&lt;br&gt;c&lt;br&gt;group by grouping sets ((c.month_id, a.date_of_day),(c.month_id),())&lt;br&gt;order by 1,2&lt;br&gt;&lt;br&gt;And the result is:&lt;br&gt;200507.	2005-07-28	58317315&lt;br&gt;200507.	2005-07-29	54006999&lt;br&gt;200507.	2005-07-30	38717771&lt;br&gt;200507.	2005-07-31	22883162&lt;br&gt;200507.	total	173925246&lt;br&gt;200508.	2005-08-01	43818067&lt;br&gt;200508.	2005-08-02	49287062&lt;br&gt;200508.	2005-08-03	48032243&lt;br&gt;200508.	2005-08-04	55144685&lt;br&gt;200508.	2005-08-05	63540968&lt;br&gt;200508.	2005-08-06	46978105&lt;br&gt;200508.	2005-08-07	26707536&lt;br&gt;200508.	2005-08-08	52050283&lt;br&gt;200508.	2005-08-09	42215595&lt;br&gt;200508.	2005-08-10	40481405&lt;br&gt;200508.	2005-08-11	94318002&lt;br&gt;200508.	2005-08-12	68535172&lt;br&gt;200508.	2005-08-13	48584137&lt;br&gt;200508.	2005-08-14	30915682&lt;br&gt;200508.	2005-08-15	49016040&lt;br&gt;200508.	2005-08-16	60961325&lt;br&gt;200508.	2005-08-17	56177854&lt;br&gt;200508.	2005-08-18	64274925&lt;br&gt;200508.	2005-08-19	60341100&lt;br&gt;200508.	2005-08-21	28196843&lt;br&gt;200508.	2005-08-22	47896784&lt;br&gt;200508.	2005-08-23	50897546&lt;br&gt;200508.	2005-08-24	42467145&lt;br&gt;200508.	2005-08-25	72119850&lt;br&gt;200508.	2005-08-26	61551026&lt;br&gt;200508.	2005-08-27	43815616&lt;br&gt;200508.	2005-08-28	25631179&lt;br&gt;200508.	2005-08-29	48931290&lt;br&gt;200508.	2005-08-30	51073884&lt;br&gt;200508.	2005-08-31	49427548&lt;br&gt;200508.	total	1523388897&lt;br&gt;200508.	total	2283454153 - it would be the 'total, total'&lt;br&gt;200509.	2005-09-01	50205687&lt;br&gt;200509.	2005-09-02	59044531&lt;br&gt;200509.	2005-09-03	44247926&lt;br&gt;200509.	2005-09-04	25701351&lt;br&gt;200509.	2005-09-05	44528623&lt;br&gt;200509.	2005-09-06	50506438&lt;br&gt;200509.	2005-09-07	50908409&lt;br&gt;200509.	2005-09-08	71730314&lt;br&gt;200509.	2005-09-09	64542775&lt;br&gt;200509.	2005-09-10	48846958&lt;br&gt;200509.	2005-09-11	31428499&lt;br&gt;200509.	2005-09-12	44448498&lt;br&gt;200509.	total	586140009&lt;br&gt;&lt;br&gt;I do not understand why it is 200508. total (or 200507. total, because it seems to me random month_id). By the way values are correct, but I would like to see grand total at the bottom.&lt;br&gt;&lt;br&gt;Could you please inform me, if you have any idea in this topic? We have just updated our database to V2R6. &lt;br&gt;&lt;br&gt;Thanks in advance!</description><pubDate>Wed, 14 Sep 2005 04:56:22 GMT</pubDate><dc:creator>Apukad</dc:creator></item></channel></rss>