Grouping set

What is the difference between grouping sets and grouping. Can any one explain to me in easy way. I will be appreciate. Thanks.

i guess u are aware of this thread on grouping sets and grouping :-)
http://orafaq.com/node/56
http://www.psoug.org/reference/rollup.html

Similar Messages

  • Is this a bug with grouping sets?

    Version is Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    SQL> with data as
      2  (select rownum r, 'X' x, 1 n from dual connect by rownum <= 10)
      3  select x
      4       , sum(n) s
      5       , (select sum(1) from dual where dummy = x) t
      6  from data
      7  group by grouping sets (x, ())
      8  /
    X          S          T
    X         10          1
              10
    SQL>  Shouldn't the superaggregate row have a 1 in the T column?

    Here's your QUERY with the addition of the GROUPING() function:
    SQL> WITH data AS
      2  (
      3     SELECT rownum r
      4          , 'X'    x
      5          , 1      n
      6     FROM   dual
      7     CONNECT BY rownum <= 10
      8  )
      9  SELECT GROUPING(x)
    10       , x
    11       , sum(n) s
    12       , (SELECT sum(1) FROM dual WHERE dummy = x) t
    13  FROM   data
    14  GROUP BY GROUPING SETS (x, ())
    15  ;
    GROUPING(X) X          S          T
              0 X         10          1
              1           10In this case the value for X is null because it's a subtotal row. If you look at the execution plan for this statement:
    PLAN_TABLE_OUTPUT
    SQL_ID  gumphwvgumqc4, child number 0
    WITH data AS (  SELECT rownum r       , 'X'    x       , 1      n  FROM
      dual  CONNECT BY rownum <= 10 ) SELECT /*+gather_plan_statistics*/ x
        , sum(n) s      , (SELECT sum(1) FROM dual WHERE dummy = x) t FROM
    data GROUP BY GROUPING SETS (x, ())
    Plan hash value: 1718326399
    | Id  | Operation                       | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
    |   0 | SELECT STATEMENT                |      |      1 |        |      2 |00:00:00.01 |       0 |    |  |          |
    |   1 |  SORT AGGREGATE                 |      |      2 |      1 |      2 |00:00:00.01 |       4 |    |  |          |
    |*  2 |   TABLE ACCESS FULL             | DUAL |      2 |      1 |      1 |00:00:00.01 |       4 |    |  |          |
    |   3 |  SORT GROUP BY ROLLUP           |      |      1 |      1 |      2 |00:00:00.01 |       0 |  2048 |  2048 | 2048  (0)|
    |   4 |   VIEW                          |      |      1 |      1 |     10 |00:00:00.01 |       0 |    |  |          |
    |   5 |    COUNT                        |      |      1 |        |     10 |00:00:00.01 |       0 |    |  |          |
    |   6 |     CONNECT BY WITHOUT FILTERING|      |      1 |        |     10 |00:00:00.01 |       0 |    |  |          |
    |   7 |      FAST DUAL                  |      |      1 |      1 |      1 |00:00:00.01 |       0 |    |  |          |
    Predicate Information (identified by operation id):
       2 - filter("DUMMY"=:B1)The value for X is being bound to the subquery. If X is null this won't return any rows (I'm sure you knew that :) ). I would say this is expected albeit confusing behavior. You get the same results if you use CUBE or ROLLUP as well.

  • Isse with using Grouping Sets in SQL Server 2008 R2

    Hi,
    I created a query in SQL Server 2012 using grouping sets that works fine.  When I try to use the same query in SQL Server 2008 I get an error ("Inccorrect syntax near SETS").
    I researched using grouping sets in 2008 and didn't see any issue with my query.  What is different in grouping sets 2008 vs 2012?
    SELECT tl.ClientRegionCd as [Client Region Code] , tl.ClientRegionDesc as [Region Name], count(tl.CompleteICN) as [Trauma Letters Sent]
    from TORT_Trauma_Letters tl
    Where CONVERT(VARCHAR(26), tl.SecondNoticeSent, 23) between '2014-06-12' and '2014-06-12'
    GROUP BY GROUPING SETS((tl.ClientRegionCd, tl.ClientRegionDesc), ())
    Stacie

    Check this blog post as how to deal with date ranges
    http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/16/bad-habits-to-kick-mishandling-date-range-queries.aspx
    For your result you can simply achieve your result with UNION ALL, e.g.
    select Client, Region, Letters
    from myTable 
    UNION ALL
    select NULL, NULL, SUM(letters) as Letters
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • OrgChart - Group setting not respected when default hierarchy clicked

    Does anyone else get this problem in OrgChart (staged)?
    If a group is defined and set to u201CGroupu201D via the settings panel by the end user, then clicking the default root icon then the resulting hierarchy starts at the default root but without members grouped. 
    If the settings panel is opened, the group setting is still u201CGroupu201D (as expected).  Simply clicking u201CApply settingsu201D does result in the members being grouped.
    Looks like a bug to me, anyone else able to replicate?

    Hey Stephen,
    This funcitonality should work as expected.
    First, please verify that youset up groups from the admin first? If not, you should do it before doing anything from settings.
    From the setting when you select u201CGroupu201D u2013 you will see the grouping, the nodes will be grouped; when you select u201CShow Group Membersu201D u2013 the group will be expanded; when you select u201CHide Group Membersu201D u2013 the box with this group will disappear from the OrgChart.
    Once this is setup, if you are still experiencing issues, verify the CDS log, as it may be a permissions issue (ie no write or modify access for user settings).
    Hope this helps
    Carl

  • My icloud email address is included in a friend's address group set up in Gmail.  When they send an email to the group, the email never gets through to my icloud address - why?

    My icloud email address is included in a friend's address group set up in Gmail.  When they send an email to the group, the email never gets through to my icloud address - why?

    They've always been able to send to me individually - just not when my email is part of a group or even when it's just included with several other addresses added one at a time.

  • How to sort specific column when using GROUPING SETS in SQL Server?

    If I remember correctly, in SQL Server, it is not recommended to use "ORDER BY" when there's already a GROUP BY GROUPING SETS.
    I have two columns: [UPC#] & [Description] both are varchar.
    My grouping sets is like this:
    GROUP BY
    GROUPING SETS
    ([UPC],[Description])
    I don't have 'ORDER BY' but it's automatically sorting the Description column.
    If I added a 3rd column, sum(Qty), then it doesn't sort by Description anymore. But if I added
    ORDER BY [Description]
    then the grand total of sum(Qty) will be at the first row instead of the last.
    Is there a way to sort the Description column and still let the grand total of sum(Qty) be at the last row instead?
    Thanks.

    Yes, it works.
    Thank you.
    Could you kindly explain the logic?
    I don't quite understand the "= 3 THEN 1 ELSE 0" part.
    Is the #3 mean the column index 3?
    GROUPING_ID returns a bit mask value depending on number of columns grouped in the result 
    So when you do GROUPING_ID(([UPC]),([Description]))
    it has two columns UPC and Description so bit mask will be as follows
    0 ie 00 for rows grouped by both UPC and Description fields
    1 ie 01 for rows grouped by UPC ie UPC subtotals
    2 ie 10 for rows grouped by Description ie Description subtotals
    3 ie 11 for rows not grouped by both columns ie Grand total
    see
    http://msdn.microsoft.com/en-IN/library/bb510624.aspx
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Group by Grouping sets in obiee11g query

    Hi Experts
    I have few unbalanced heirarchies in the rpd. I have created level beased heirarchy for those ragged and skipped hierarchy but while generating the report using those hierarchy generates a huge SQL involving 20-30 UNION ALLs . Instead it should have generated comparatively smaller query using Group by Grouping Sets but it is not.
    I have also tried enabling this parameter in DB properties in OBIEE11g ( changed the default DB from 9i to 11g in obiapps 7.9.6.3 rpd) but still no use .
    As a result of huge query geenration the performance is badly impacted.
    Any thought on this pls.

    I have run into the same problem. Any hints about what might be the reason or, even better, how to address it, will be more than welcome.
    thanks in advance

  • GROUP BY grouping sets

    Hello everybody,
    I have this query
    SELECT ALL
                M_PAGAMENTO||' '||D_TIPO MPAGA,
                grouping(F.KIND_F),
                DECODE (grouping(F.KIND_F), 0 , 'Totali IVA',F.KIND_F)  a1,
                 case
                 when grouping(F.KIND_F) = 1 then F.KIND_F
                 when grouping(F.KIND_F) = 0 then 'Totali IVA'
                 end  TIPOF,
               F.KIND_F  TIPOF,
               COUNT(*) NFATTURE,
                sum(F.TOT_FT) TFATTURA, sum(F.BOLLO) TBOLLO,
                K_IVA, SUM(FA.IPBFA) TIMP, SUM(FA.IPSFA) TIPS
    FROM A_FATTURE  F,A_PAZIENTI A,TIPOLOGIA_PAGAMENTI p  ,A_FATTURE_ALIQUOTE FA
    where (F.TIPORD = A.TIPORD AND F.K_CODE = A.K_CODE  AND F.ANNO = A.ANNO)
    AND   (FA.TIPORD = F.TIPORD AND FA.K_CODE = F.K_CODE AND FA.ANNO = F.ANNO AND FA.N_FATT_D = F.N_FATT_D)
    and p.ID_TIPO = f.M_PAGAMENTO
    &MYWHERE
    GROUP BY grouping sets (M_PAGAMENTO||' '||D_TIPO,F.KIND_F),K_IVA
    ORDER by  1
    Why I get this result(A1, tipof) = NULL but they would be = 'D' (F.KIND_F ='D'
    MPAGA                    GROUPING(F.KIND_F)     A1     TIPOF     TIPOF     NFATTURE     TFATTURA     TBOLL      K_IVA     TIMP          TIPS
    01 Contante               1                              5          3183,24          7,24     20     250          50
    01 Contante               1                              3          2342,43          5,43     FC     5,43          0
    01 Contante               1                              50          7024,61          9,91     ES     24458,91     0
    02 Assegno               1                              1          780,81          1,81     FC     1,81          0
    02 Assegno               1                              2          816,96          1,81     ES     755,15          0
    02 Assegno               1                              1          780,81          1,81     20     50          10
    04 Carta di credito          1                              1          780,81          1,81     FC     1,81          0
    04 Carta di credito          1                              1          780,81          1,81     20     50          10
    04 Carta di credito          1                              1          780,81          1,81     ES     719          0
                        0     Totali IVA     Totali IVA     D     53          8622,37          23,53     ES     25933,06     0
                        0     Totali IVA     Totali IVA     D     5          3904,05          9,05     FC     9,05          0
                        0     Totali IVA     Totali IVA     D     7          4744,86          10,86     20     350          70where I am wrong?
    thanks for any help

    Rosario,
    Those rows where grouping(f.kind_f) = 1 are superaggregate rows where you are grouping by M_PAGAMENTO||' '||D_TIPO,K_IVA. So f.kind_f doesn't come into play for these rows, and it shows a null value.
    You can of course hard code a 'D' value in there, but values of the f.kind_f column are always null at this grouping level.
    See this example:
    SQL> select deptno
      2       , job
      3       , extract(year from hiredate)
      4       , sum(sal)
      5       , grouping(job)
      6    from emp
      7   group by deptno
      8       , grouping sets (job,extract(year from hiredate))
      9  /
        DEPTNO JOB       EXTRACT(YEARFROMHIREDATE)   SUM(SAL) GROUPING(JOB)
            10 CLERK                                     1300             0
            10 MANAGER                                   2450             0
            10 PRESIDENT                                 5000             0
            20 CLERK                                     1900             0
            20 ANALYST                                   6000             0
            20 MANAGER                                   2975             0
            30 CLERK                                      950             0
            30 MANAGER                                   2850             0
            30 SALESMAN                                  5600             0
            10                                1981       7450             1
            10                                1982       1300             1
            20                                1980        800             1
            20                                1981       5975             1
            20                                1982       3000             1
            20                                1983       1100             1
            30                                1981       9400             1
    16 rijen zijn geselecteerd.
    SQL> select deptno
      2       , job
      3       , extract(year from hiredate)
      4       , sum(sal)
      5       , grouping(job)
      6       , decode(grouping(job),1,job)
      7    from emp
      8   group by deptno
      9       , grouping sets (job,extract(year from hiredate))
    10  /
        DEPTNO JOB       EXTRACT(YEARFROMHIREDATE)   SUM(SAL) GROUPING(JOB) DECODE(GR
            10 CLERK                                     1300             0
            10 MANAGER                                   2450             0
            10 PRESIDENT                                 5000             0
            20 CLERK                                     1900             0
            20 ANALYST                                   6000             0
            20 MANAGER                                   2975             0
            30 CLERK                                      950             0
            30 MANAGER                                   2850             0
            30 SALESMAN                                  5600             0
            10                                1981       7450             1
            10                                1982       1300             1
            20                                1980        800             1
            20                                1981       5975             1
            20                                1982       3000             1
            20                                1983       1100             1
            30                                1981       9400             1
    16 rijen zijn geselecteerd.Regards,
    Rob.

  • GROUP BY GROUPING SETS Clarification of Format

    Hello.
    I am trying to validate the answers to 2 questions I have listed below.
    Would it be possible for someone to assist me with clarifying these 2 questions and answer statements? Please clarify the portion of the answer that is in
    Bold Text and Italicized. Why is one answer enclosed in brackets and the other not?
    Basically I need to figure out how to determine the syntax of these parenthesis and why they differ from each question. Is it a format issue or is it based on how the question is asked.
    Question #1-
    You are a developer for a Microsoft SQL Server 2008 R2 database instance used to support a customer service application. 
    You create tables named complaint, customer, and product as follows: 
    CREATE TABLE [dbo].[complaint] ([ComplaintID] [int], [ProductID] [int], [CustomerID] [int], [ComplaintDate] [datetime]); 
    CREATE TABLE [dbo].[customer] ([CustomerID] [int], [CustomerName] [varchar](100), [Address] [varchar](200), [City] [varchar](100), [State] [varchar](50), [ZipCode] [varchar](5)); 
    CREATE TABLE [dbo].[product] ([ProductID] [int], [ProductName] [varchar](100), [SalePrice] [money], [ManufacturerName] [varchar](100)); 
    You need to write a query to sum the sales made to each customer who has made a complaint by the following entries:
    ·Each customer name
    ·Each product name
    ·The grand total of all sales 
    Which SQL query should you use?
    SELECT c.CustomerName, p.ProductName, SUM(p.SalePrice) AS Sales
    FROM product p 
    INNER JOIN complaint com ON p.ProductID = com.ProductID 
    INNER JOIN customer c ON com.CustomerID = c.CustomerID
    GROUP BY GROUPING SETS ((c.CustomerName), (p.ProductName), ());
    Question #2 - 
    You are a developer for a Microsoft SQL Server 2008 R2 database instance. 
    You create tables named order, customer, and product as follows: 
    CREATE TABLE [dbo].[order] ([OrderID] [int], [ProductID] [int], [CustomerID] [int],[OrderDate] [datetime]); 
    CREATE TABLE [dbo].[customer] ([CustomerID] [int], [CustomerName] [varchar](100),[Address] [varchar](200), [City] [varchar](100), [State] [varchar](50),
    [ZipCode] [varchar](5)); 
    CREATE TABLE [dbo].[product] ([ProductID] [int], [ProductName] [varchar](100), [SalePrice] [money], [ManufacturerName] [varchar](100)); 
    You need to write a query to sum the sales made to each customer by the following entries:
    ·The Customer name and product name
    ·The grand total of all sales
     Which SQL query should you use?
    SELECT c.CustomerName,p.ProductName,SUM(p.SalePrice) AS Sales
    FROM product p 
    INNER JOIN [order] o ON p.ProductID = o.ProductID 
    INNER JOIN customer c ON o.CustomerID = CustomerID
    GROUP BY GROUPING SETS ((c.CustomerName, p.ProductName), ());
    Your help would be highly appreciated. :)
    Thanks

    Its just to identify the GROUPING SET.When you enclose, the grouping would take for the combination.
    Ok, it would be better to see with an example as below:
    create table T1 (Col1 Varchar(100),Col2 int,Col3 int)
    Insert into T1 Select 'SQL',10,100
    Insert into T1 Select 'SQL',11,100
    Insert into T1 Select 'Oracle',20,500
    SELECT p.col1,p.col2,SUM(p.col3) AS Sales
    FROM T1 p
    GROUP BY GROUPING SETS ((p.Col1,p.Col2), ());/*Oracle 20 500
    SQL 10 100
    SQL 11 100
    NULL NULL 700*/
    SELECT p.col1,p.col2,SUM(p.col3) AS Sales
    FROM T1 p
    GROUP BY GROUPING SETS ((p.Col1),(p.Col2), ());
    /*NULL 10 100
    NULL 11 100
    NULL 20 500
    NULL NULL 700
    Oracle NULL 500
    SQL NULL 200*/
    Drop table T1

  • GROUP BY GROUPING SETS for a selected month and for year to date

    Below is a code example to demonstrate this question:
    declare @test table (ID int, Quantity int, Day date);
    insert into @test values
    (4, 500, '1/18/2014'),
    (4, 550, '1/28/2014'),
    (7, 600, '1/10/2014'),
    (7, 750, '1/11/2014'),
    (7, 800, '1/20/2014'),
    (1, 100, '1/2/2014'),
    (1, 125, '1/10/2014'),
    (8, 300, '1/7/2014'),
    (9, 200, '1/17/2014'),
    (9, 100, '1/22/2014'),
    (4, 900, '2/18/2014'),
    (4, 550, '2/28/2014'),
    (7, 600, '2/10/2014'),
    (7, 700, '2/11/2014'),
    (7, 800, '2/20/2014'),
    (1, 100, '2/2/2014'),
    (1, 150, '2/10/2014'),
    (8, 300, '2/7/2014'),
    (9, 200, '2/17/2014'),
    (9, 100, '2/22/2014'),
    (4, 500, '3/18/2014'),
    (4, 550, '3/28/2014'),
    (7, 600, '3/10/2014'),
    (7, 750, '3/11/2014'),
    (7, 800, '3/20/2014'),
    (1, 100, '3/2/2014'),
    (1, 325, '3/10/2014'),
    (8, 300, '3/7/2014'),
    (9, 200, '3/17/2014'),
    (9, 100, '3/22/2014'),
    (4, 500, '4/18/2014'),
    (4, 550, '4/28/2014'),
    (7, 100, '4/10/2014'),
    (7, 750, '4/11/2014'),
    (7, 800, '4/20/2014'),
    (1, 100, '4/2/2014'),
    (1, 325, '4/10/2014'),
    (8, 300, '4/7/2014'),
    (9, 200, '4/17/2014'),
    (9, 100, '4/22/2014'),
    (4, 500, '5/18/2014'),
    (4, 550, '5/28/2014'),
    (7, 600, '5/10/2014'),
    (7, 750, '5/11/2014'),
    (7, 50, '5/20/2014'),
    (1, 100, '5/2/2014'),
    (1, 325, '5/10/2014'),
    (8, 300, '5/7/2014'),
    (9, 200, '5/17/2014'),
    (9, 100, '5/22/2014');
    --detail
    select *
    from @test;
    --aggregation
    select
    TotalQuantity = sum(Quantity),
    [Month] = month(Day)
    from @test
    group by
    grouping sets
    (month(Day)),
    (year(Day))
    go
    This is the aggregation query result:
    However, the desired result is to return only two rows: one row for month 3 and the other row for year to date (in the picture above YTD is the row that appears with {null} in the Month column).  Is there a way to achieve this goal by modifying the
    sample code above?  The requirement is to only read the data once (do not want a solution that involves a UNION which implies reading the data twice).

    you can add required filters in having clause. Here is the query -
    select
    TotalQuantity = sum(Quantity),
    [Month] = month(Day)
    from @test
    group by
    grouping sets
    (month(Day)),
    (year(Day))
    having
    month(Day) = 3 or month(Day) is null;

  • Has anyone used grouping sets or perhaps rollup function

    I am trying to do a rollup query ....to sum up sales based on an
    account number you would think pretty simple.....so here is the
    query I wrote
    SELECT broker_number
    account_number , ACCOUNT_NAME ,
    to_char( SUM ( FACTORY_NET_AMT ) , '9,999,999,999') sales$
    FROM broker_account_expanded
    WHERE product_level = 'BRND'
    and product_level_Code not in ( select PROD_CATEGORY_CD from
    genesis.tFIN_BRAND )
    AND PRODUCT_LEVEL_CODE = '100515'
    group by GROUPING SETS (
    ( broker_number , account_number , account_name ))
    however I am getting the following error
    SQL> /
    group by GROUPING SETS (
    ERROR at line 9:
    ORA-00933: SQL command not properly ended
    Any ideas ?
    Thank's
    Sameer Handa

    I think correct would be:
    GROUP BY ROLLUP (broker_number , account_number , account_name )

  • Why is DPM 2012 R2 splitting this Co-located Protection Group Set across three different tapes?

    I have 10 protection groups that ran Saturday night.  I have put information about them in the following table:
    I extracted the data in the table using Get-DPMProtectionGroup CmdLet and the job information from the Monitoring page of the DPM admin console.  As you can see from the tape barcode, DPM wrote the first four PGs to one tape, then started a second
    tape, and finally failed when starting a third because it had run out of free tapes.
    All of the protection groups are assigned to the same "Co-Located Protection Group Set" in the "Tape Optimization Setup" dialog box.
    Prior to the jobs running, I painstakingly ensured that each of the Protection Group's "Select Long-Term Goals" and "Select Library and Tape Details" screens are identical as follows:
    Prior to running the jobs I had already disabled autoclean on the tape library.
    I have checked to see if the tape drive is reporting IO error 0x8007045D using the method described in this blog post:
    http://blogs.technet.com/b/dpm/archive/2012/05/14/things-you-can-do-to-help-data-protection-manager-utilize-your-tapes-full-capacity.aspx
    There seems to be no mention of 0x8007045D or -2147023779 in any of the MSDPM*.Errlog files.
    These are LTO6 tapes with a capacity of 2.5 TB each.  The total data for all the jobs put together is less than 300GB.
    Why is DPM 2012 R2 splitting these protection groups across three different tapes? 
    What else should I check for that might cause DPM to start a new tape?

    Hi Mike,
    Thanks for taking a look at the logs.  I just wanted to point out that there were actually two changes made just before collocation succeeded:
    Write Period and Expiry Tolerance were increase
    Full Verbose logging was enabled
    This technet article states that Full Verbose logging affects performance.  If the problem with collocation is caused by a race condition then it is
    possible that enabling logging actually caused collocation to succeed. At this point there doesn’t seem to be any evidence pointing to which of the two changes resulted in collocation succeeding.
    I propose I run the following tests to differentiate the two:
    Test #1 - Collocation Confirmation
    Repeat last weekend's test to confirm that successful collocation wasn't an anomaly.  Configuration:
    full verbose logging: Enabled
    Expiry Tolerance: 1 year
    Write Period: 1 year
    Test #2 - Logging Only
    Test to see whether enabling Full Verbose logging enabled collocation to succeed.  Configuration:
    full verbose logging: Enabled
    Expiry Tolerance: 7 days
    Write Period: 6 day
    Thanks for your help,
    Alex

  • Grouping Sets

    who can explain GROUPING SETS for me?

    Hello.
    GROUPING SETS is basically an extension to the GROUP BY clause allowing you to specify ROLLUP / CUBE information or
    to actually specify which groups (or set of groups) you want to see in the output, which can greatly simplify the SQL.
    For further information, see
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_10002.htm#sthref7251

  • Request group set error...

    I have created an request group set to upload Master Item in inventory.
    I am getting the following error when submitting Request Group request set from "India Local Inventory" responsibility in vision R12 instance :
    APP-FND-01564: ORACLE error -1116 in SUBMIT: others
    Cause: SUBMIT: others failed due to ORA-01116: error in opening database file 85
    ORA-01110: data file 11: '/d02/vis_db/VIS/db/apps_st/data/tx_idx17.dbf'
    ORA-27041: unable to open file
    Linux Error: 24: Too many open files
    Additional information: 3.
    The SQL statement being executed at the time of the error was: &SQLSTMT and was executed for the file &ERRFILE.
    How can I solve this problem??

    Please refer to the following notes:
    [Note: 566234.1 - Error: 24: Too many open files running initial request set|https://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=566234.1]
    [Note: 429760.1 - Ora-01110: Data File Error Opening Datafiles File|https://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=NOT&p_id=429760.1]

  • Using rollup or grouping sets in a complicated manner

    Dear all;
    I have the following select statements that display data in the following manner below
    select location, male, female from table_testthis gives me the results as following below
    location  Male   Female
    tx          1          2
    NM         0         1
    AK         1          2
    tx          2          4this is what i want though however
    location Male    Female
    Tx         3           6
               Genders   9
    AK        1            2
               Genders    3
    NM        0             1
                Gender      1see inserts statment below
    create table  table_test
    location varchar2(4000),
    male number(20),
    female number(10)
    insert into table_test values('Tx', 1, 2);
    insert into table_test values('NM', 0, 1);
    insert into table_test values('AK', 1, 2);
    insert into table_test values('Tx', 2, 4); all help is appreciated. thank you
    Edited by: user13328581 on Oct 19, 2012 10:03 AM

    select  case group_id()
              when 0 then location
              else null
            end location,
            case group_id()
              when 0 then to_char(sum(male))
              else 'Genders'
            end male,
            case group_id()
              when 0 then sum(female)
              else sum(male) + sum(female)
            end female
      from  table_test t
      group by grouping sets((location),(location))
      order by t.location,
               group_id()
    LOCATION             MALE           FEMALE
    AK                   1                   2
                         Genders             3
    NM                   0                   1
                         Genders             1
    Tx                   3                   6
                         Genders             9
    6 rows selected.
    SQL> SY.

Maybe you are looking for