Using Subquery in Select with Rollup (or some way to get grand total)

I am trying to do the following (simplified the query):
SELECT s.SYSTEM,
(SELECT COUNT(cp1.cid) FROM cfrs cp1, systems sp1, subsystems ssp1 where cp1.priority = 'P1' AND cp1.subsystem = ssp1.subsystem AND sp1.system = ssp1.system AND sp1.system = s.system) AS P1
FROM cfrs c, systems s, subsystems ss
WHERE c.subsystem = ss.subsystem
AND s.system = ss.system
GROUP BY ROLLUP(s.SYSTEM)
But the result of the Rollup on the P1 column is always 0 (it is not summing it up)
How can I accomplish a Rollup (or get a grand total) of the subquery result?
Thanks,

How about this then:
SQL > WITH SYSTEMS AS
  2  (
  3     SELECT 'A' AS SYSTEM FROM DUAL UNION ALL
  4     SELECT 'B' AS SYSTEM FROM DUAL UNION ALL
  5     SELECT 'C' AS SYSTEM FROM DUAL
  6  ),
  7  SUBSYSTEMS AS
  8  (
  9     SELECT 'A' AS SYSTEM, '1' AS SUBSYSTEM FROM DUAL UNION ALL
10     SELECT 'A' AS SYSTEM, '2' AS SUBSYSTEM FROM DUAL UNION ALL
11     SELECT 'A' AS SYSTEM, '3' AS SUBSYSTEM FROM DUAL UNION ALL
12     SELECT 'B' AS SYSTEM, '4' AS SUBSYSTEM FROM DUAL UNION ALL
13     SELECT 'B' AS SYSTEM, '5' AS SUBSYSTEM FROM DUAL UNION ALL
14     SELECT 'B' AS SYSTEM, '6' AS SUBSYSTEM FROM DUAL UNION ALL
15     SELECT 'C' AS SYSTEM, '7' AS SUBSYSTEM FROM DUAL UNION ALL
16     SELECT 'C' AS SYSTEM, '8' AS SUBSYSTEM FROM DUAL UNION ALL
17     SELECT 'C' AS SYSTEM, '9' AS SUBSYSTEM FROM DUAL
18  ),
19  CFRS AS
20  (
21     SELECT 10 AS CID, '1' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
22     SELECT 11 AS CID, '1' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
23     SELECT 12 AS CID, '1' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
24     SELECT 13 AS CID, '2' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
25     SELECT 14 AS CID, '2' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
26     SELECT 15 AS CID, '2' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
27     SELECT 16 AS CID, '3' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
28     SELECT 17 AS CID, '3' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
29     SELECT 18 AS CID, '4' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
30     SELECT 19 AS CID, '5' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
31     SELECT 20 AS CID, '5' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
32     SELECT 21 AS CID, '6' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
33     SELECT 31 AS CID, '7' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
34     SELECT 41 AS CID, '8' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
35     SELECT 51 AS CID, '9' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
36     SELECT 61 AS CID, '9' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL
37  )
38  SELECT DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM) AS SYSTEM
39  ,  MAX(DECODE(PRIORITY,'LOW',CNT,0))       AS LOW
40  ,  MAX(DECODE(PRIORITY,'MED',CNT,0))       AS MED
41  ,  MAX(DECODE(PRIORITY,'HIGH',CNT,0))      AS HIGH
42  ,  MAX(DECODE(PRIORITY,'ALL_PRIOR',CNT,0)) AS SYSTEM_TOTAL
43  FROM
44  (
45     SELECT DECODE(GROUPING(SYSTEMS.SYSTEM),1,'ALL_SYSTEMS',SYSTEMS.SYSTEM) AS SYSTEM
46     , DECODE(GROUPING(CFRS.PRIORITY),1,'ALL_PRIOR',CFRS.PRIORITY) AS PRIORITY
47     , COUNT(*) AS CNT
48     FROM SYSTEMS
49     JOIN SUBSYSTEMS ON SYSTEMS.SYSTEM = SUBSYSTEMS.SYSTEM
50     JOIN CFRS ON SUBSYSTEMS.SUBSYSTEM = CFRS.SUBSYSTEM
51     GROUP BY CUBE(SYSTEMS.SYSTEM,CFRS.PRIORITY)
52     ORDER BY SYSTEMS.SYSTEM, CFRS.PRIORITY
53  )
54  GROUP BY DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM)
55  ORDER BY DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM)
56  /
SYSTEM             LOW        MED       HIGH SYSTEM_TOTAL
A                    2          2          4            8
B                    1          1          2            4
C                    1          2          1            4
Grand Total          4          5          7           16Oh, BTW, use "code" wrapped in "{}" on either side to post nicely formatted code.

Similar Messages

  • Delete statement that uses a sub-select with the statement in the cursor

    Hi all,
    How to write write a delete statement that uses a sub-select with the statement in the cursor?
    CURSOR excluded_dates IS         
           SELECT TO_TIMESTAMP(report_parameter_value, in_date_format_mask)
          INTO my_current_date_time
          FROM report_parameters
         WHERE report_parameters.report_parameter_id    = in_report_parameter_id
           AND report_parameters.report_parameter_group = 'DATE_TIME'
           AND report_parameters.report_parameter_name  = 'EXCLUDED_DATE';
    OPEN excluded_dates;
      LOOP
        FETCH excluded_dates INTO my_excluded_date;
        EXIT WHEN excluded_dates%NOTFOUND;
        DELETE FROM edr_rpt_tmp_inclusion_table
        WHERE TO_CHAR(date_time, 'mm/dd/yyyy') = TO_CHAR(my_excluded_date, 'mm/dd/yyyy');
      END LOOP;
      CLOSE excluded_dates;Thanks

    Hi,
    In such case I think is better to create a view an perform the delete using it. Example (using HR schema):
    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    SQL> create or replace view v_employees as select * from employees where first_name like 'J%';
    View created
    SQL> select * from v_employees;
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE   JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
            110 John                 Chen                      JCHEN                     515.124.4269         28/09/1997  FI_ACCOUNT    8200,00                       108           100
            112 Jose Manuel          Urman                     JMURMAN                   515.124.4469         07/03/1998  FI_ACCOUNT    7800,00                       108           100
            125 Julia                Nayer                     JNAYER                    650.124.1214         16/07/1997  ST_CLERK      3200,00                       120            50
            127 James                Landry                    JLANDRY                   650.124.1334         14/01/1999  ST_CLERK      2400,00                       120            50
            131 James                Marlow                    JAMRLOW                   650.124.7234         16/02/1997  ST_CLERK      2500,00                       121            50
            133 Jason                Mallin                    JMALLIN                   650.127.1934         14/06/1996  ST_CLERK      3300,00                       122            50
            139 John                 Seo                       JSEO                      650.121.2019         12/02/1998  ST_CLERK      2700,00                       123            50
            140 Joshua               Patel                     JPATEL                    650.121.1834         06/04/1998  ST_CLERK      2500,00                       123            50
            145 John                 Russell                   JRUSSEL                   011.44.1344.429268   01/10/1996  SA_MAN       14000,00           0,40        100            80
            156 Janette              King                      JKING                     011.44.1345.429268   30/01/1996  SA_REP       10000,00           0,35        146            80
            176 Jonathon             Taylor                    JTAYLOR                   011.44.1644.429265   24/03/1998  SA_REP        8600,00           0,20        149            80
            177 Jack                 Livingston                JLIVINGS                  011.44.1644.429264   23/04/1998  SA_REP        8400,00           0,20        149            80
            181 Jean                 Fleaur                    JFLEAUR                   650.507.9877         23/02/1998  SH_CLERK      3100,00                       120            50
            186 Julia                Dellinger                 JDELLING                  650.509.3876         24/06/1998  SH_CLERK      3400,00                       121            50
            189 Jennifer             Dilly                     JDILLY                    650.505.2876         13/08/1997  SH_CLERK      3600,00                       122            50
            200 Jennifer             Whalen                    JWHALEN                   515.123.4444         17/09/1987  AD_ASST       4400,00                       101            10
    16 rows selected
    SQL> delete from v_employees where hire_date >= to_date('01/06/1998', 'dd/mm/yyyy');
    2 rows deleted
    SQL> regards,

  • I have purchased (veranda magazines) for my IPHONE which I would like to use on my macbook air. Do I have to repurchase these app or is there some way to get them in my macbook air with no charge?

    i have purchased (veranda magazines) for my IPHONE which I would like to use on my macbook air. Do I have to repurchase these app or is there some way to get them in my macbook air with no charge?

    Veranda Magazine is a free app available for iOS devices in the iOS App Store.  You then purchase a Veranda Magazine subscription from within the app.  In other words, the app itself is free, but the contents are not.
    There is no way to duplicate or import this to a Mac.  Your Mac runs a totally different operating system.
    iBooks can now import to Mac since OS X Mavericks came out about a month ago.  But that does not apply to magazines or iOS Newsstand publications.

  • HT5312 i need help with the security questions is there some way to get you to remind me what they were from e-mail or other wise

    I need help with the security Questions is there some way to get you to remember them by e-mail of other wise

    Read the HT5312 page that you posted from, it has instructions for how to reset them i.e. if you have a rescue email address set up on your account then steps 1 to 5 half-way down that page should give you a reset link.
    If you don't have a rescue email address then you will need to contact iTunes Support / Apple in your country to get the questions reset.
    Contacting Apple about account security : http://support.apple.com/kb/HT5699
    When they've been reset (and if you don't already have a rescue email address) you can then use the steps half-way down the HT5312 page that you posted from to add a rescue email address for potential future use

  • When using an external (ergonomic mouse) is there any way to get the wheel to scroll one full page at a time?

    When using an external (ergonomic mouse) is there any way to get the wheel to scroll one full page at a time? Much appreciated.

    I can't use the keyboard to scroll. I need to keep my hand on the mouse, otherwise I'd never get anything done. Since I have no problem getting Firefox to scroll one page at a time, and I'm constantly jumping between 3 browsers, it would be nice to be able to scroll a full page at a time in Safari, just like I can in Firefox. Thanks.

  • HT201272 I want to download a previously purchased album that had an "error" and isn't complete. I have an older Mac and don't have iCloud. Surely there is some way to get the music I paid for!

    I want to download a previously purchased album that had an "error" and isn't complete. I have an older Mac and don't have iCloud. Surely there is some way to get the music I paid for!

    Hi,
    See this http://support.apple.com/kb/ht2519
    Jim

  • I don't want to do the recovery thing to my iPhone4, but my iPhone is at the stage in the update where it says to plug it into iTunes, is there some way to get out of this or save all of my data somehow?

    i don't want to do the recovery thing to my iPhone4, but my iPhone is at the stage in the update where it says to plug it into iTunes, is there some way to get out of this or save all of my data somehow?

    What do you mean the "update" says plug it into iTunes? If you have the iTunes logo/USB cord on the screen, it's in recovery mode or needs to be activated. Anything that was on the phone is already gone. You have no choice but to plug it into the computer. Hopefully, you backed it up before you started the update.
    If that's not what you mean, then please explain.
    Apple may or may not be able to fix the lock button for you without replacing the phone entirely. You'll have to take it in and ask.

  • Ios7 Maps have no voice, is there some way to get voice back?

    Since updating to ios7 my Maps have no voice, is there some way to get voice back?  I'm in the UK which is supported by ios7 and I had voice on ios6...HELP!

    Since updating to ios7 my Maps have no voice, is there some way to get voice back?  I'm in the UK which is supported by ios7 and I had voice on ios6...HELP!

  • In interactive how to use AT LINE-SELECTION WITH SET PF-STATUS

    Hi all,
             I am developing an interactive report in which i am using gui status (pf-status ) for AT USER-COMMAND and AT LINE-SELECTION  event .But when i used at line-selection event is  not working with pf-status even though i use PICK function code .
    plz help me to work with both the event i.e.
    AT USER-COMMAND.
    AT LINE-SELECTION.
    Thanks a lot in Advance .
    Regards,
    Tarak

    hi,
      In PBO of Module Pool, Please Write in this way
    MODULE status_9001 OUTPUT.
    Set the PF-STATUS
      SET PF-STATUS '9001'.
    To Set the Title of Screen
      SET TITLEBAR 'TITLE'.
    ENDMODULE
    Setting PF status to the (SAP) system default
    set pf-status 'BASIC'.
    How to check for pf-status
    AT USER-COMMAND.
    CASE SY-UCOMM.
    WHEN 'ART'.
    PERFORM STYR_ARTSKONTI.
    WHEN 'PST'.
    PERFORM STYR_POSTER.
    WHEN 'BIL'.
    PERFORM VIS_BILAG.
    ENDCASE..

  • Can not use 'AT LINE-SELECTION' with SET PF-STATUS

    Hi experts,
    my code is like below:
    PARAMATERS: .....
    SET PF-STATUS 'st_nam'.
    Perform Load_data . ' For list display
    START-OF-SELECTION.
    AT LINE-SELECTION.
      Perform list_detail.
    AT USER-COMMAND.
      CALL SCREEN '1001'.
    END-OF-SELECTION.
    When I execute the report , the basic was displayed , but when then i double click on the list to call the next screen '1001'. It did not work , but if i omit the SET PF-STATUS m AT LINE-SELECTION worked week , so in this case what has happened i how can i solve this problem,
    Thaks,
    PS: Reward immediately

    Hi,
    Thank you very much for your helpful answers, I have copied the report downto my system and test it , When i used SET PF-STATUS , i can not use AT LINE-SELECTION to capture events ...May be we can not use these 2 statements in parallel,...:D
    Thanks,

  • Use of dsp:select with some map or list???

    Dear Friends,
    I want to use dsp:select by using map or list as the option of the given combobox using repository. Can anybody help me out to resolve my problem. Thanks to in advance.
    Regards,
    Girish Mishra

    Hi,
    You can use the below snippet.
    <dsp:select bean="YourBean.value">
    <dsp:droplet name="ForEach">
    <dsp:param name="array" param="YourList"/>
    <dsp:oparam name="output">
    <dsp:option><dsp:valueof param="element"/></dsp:option>
    </dsp:oparam>
    </dsp:droplet>
    </dsp:select>
    You have to set the list from the repository before you render this page.
    Hope this helps.
    Keep posting the updates / questions.
    Thanks,
    Gopinath Ramasamy

  • I am using the "Menu Selection With Events.vi" example alongside other while loops, When I select exit from the new menu it stops the other while loops but not the menu/event while loop

    The event driven menu example works fine and I've integrated it into my application. I have built in simple local vars to link menu selections to existing GUI buttons. The problem is that I cannot get the File-Exit menu option to switch off/terminate the application. The [1] "Stop": Value Change panel of the event structure simply switches off the other while loops but leaves the event while loop running. Add a while loop to the axample using a local var from the event while loop to terminate 2nd while loop to get the effect.
    Help appreciated, Chris

    Attached you will find a modified version of your VI.
    I documented what I did on the block diagram.You were right on you just needed to add a variable with a true wired to it.
    Have fun with it.
    Joe
    Joe.
    "NOTHING IS EVER EASY"
    Attachments:
    Menu_Selection_with_Events_%26_while_loop[1].vi ‏76 KB

  • USING SUBQUERY IN SELECT STATEMENT

    I am looking at reusing the results of a first query in a second select statement.
    I have a first table "ACTIVE_FIELDS" in which I maintain the columns I want to query.
    Table name ACTIVE_FIELDS
    Field - Status
    CODE - ACTIVE
    DESCRIPTION - ACTIVE
    NAME - INACTIVE
    So I get the list of active fields by doing :
    SELECT FIELD FROM ACTIVE_FIELDS WHERE STATUS=ACTIVE
    It gives me : Code and Description.
    I have then a second table MATERIAL with 3 columns CODE, DESCRIPTION and NAME.
    I am looking at doing a query against MATERIAL table as follows
    SELECT (SELECT FIELD FROM ACTIVE_FIELDS WHERE STATUS=ACTIVE) from MATERIAL and I expect to only get columns CODE and DESCRIPTION in the result.
    If I want to add the NAME field in the result of my query, I just have to change status of NAME field in the eh ACTIVE_FIELDS table..
    Can anybody help me with the exact syntax, I must miss a function to convert the result of the first query..

    that data model looks suspiciously like an attribute-value generic model, thus the need for that funky sql.
    read this thread.
    Re: SIMPLE Database Design Problem !

  • Using an inner select with an Update

    I am using Oracle 10g and I'm trying to do the following which I got from a post.
    UPDATE Items
    SET
    Items.itemNumber,
    Items.revision
    ) = (SELECT
    XmlItems.itemNumber,
    XmlItems.revision,
    FROM XmlItems
    WHERE XmlItems.id = Items.id);
    Basically, XmlItems is a global temporary table that contains the data from an XML CLOB that was passed in. I have verified the data does exist in that temporary table before this update is called and that the item does exist in Items.
    However, I get the following error when this executes:
    cannot update ("WP_A"."ITEMS"."ITEMNUMBER") to NULL
    This happens because the there is a NOT NULL constraint on that column. The question is why is it trying to set null? Is the syntax above not correct?

    Hi,
    user8692759 wrote:
    Basically, XmlItems is a global temporary table that contains the data from an XML CLOB that was passed in. I have verified the data does exist in that temporary table before this update is called and that the item does exist in Items.Have you verified this for every row in items? Since the main UPDATE statement does not have a WHERE clause, you're trying to update every row in items.
    However, I get the following error when this executes:
    cannot update ("WP_A"."ITEMS"."ITEMNUMBER") to NULL
    This happens because the there is a NOT NULL constraint on that column. The question is why is it trying to set null? Is the syntax above not correct?You may need to add a WHERE clause to the UPDATE statement:
    UPDATE  Items
    SET             (Items.itemNumber,        Items.revision) =
    (     SELECT      XmlItems.itemNumber,     XmlItems.revision     -- No , after revision
         FROM      XmlItems
         WHERE      XmlItems.id = Items.id
    WHERE   id  IN ( SELECT  id
                      FROM      XmlItems
                );or use MERGE instead of UPDATE.

  • A website that i use quite frequently will not work with the newer versions of firefox. Is there a compatability mode of some way to get the newer version to work?

    All the website says is this browser is not supported. Only version 3.5 is. I can list the url because it is a work url

    You can install the portable Firefox 3.6.x version to access websites that do not work with Firefox 5+.
    *http://portableapps.com/apps/internet/firefox_portable#legacy
    *http://portableapps.com/apps/internet/firefox_portable/localization#legacy36

Maybe you are looking for

  • Java.lang.NoClassDefFoundError:Couldnot initialize class com.sap.mw.jco.JCO

    Hi, I am having an issue with SAP adapter configuration. It throws below error in log files : java.lang.ExceptionInInitializerError: JCO.classInitialize(): Could not load middleware layer 'com.sap.mw.jco.rfc.MiddlewareRFC' JCO.nativeInit(): Could not

  • Advice on buying a 13" MacBook Pro

    Hey everybody! I am currently getting ready to buy my wife and I our first Mac. But I need some advice in making sure I'm picking the right one. I'll give you the rundown: I'm looking at the 13" MacBook Pro because I'm double majoring in communicatio

  • CProjects and DMS

    Hi, We're in the BBP phase and wondering if we are going to use DMS for structuring documents in relation to cProject. However as I can read this is more or less an object link. Is there possibilities to automatically copy the cProjects structure int

  • ¿COMO COMPRAR ADOBE PHOTOSHOP CS5 EXTENDED

    TENGO LA VERSIÓN DE PRUEBA DE ADOBE PHOTOSHOP CS5 EXTENDED Y QUIERO COMPRAR LA LICENCIA, NO QUIERO OTRO POSTERIOR PORQUE NO LO SE USAR, ES MÁS NO QUIERO SUSCRIPCIONES ANUALES, QUIERO COMPRAR LA LICENCIA Y QUE ME VALGA PARA SIEMPRE, QUE LLEGA UN MOMEN

  • So many errors for one 30-day-ish trial. Lovely.

    I'm constantly having quite a bunch of problems and errors all in an attempt to use a 30 day trial of Photoshop. This is all happening with the most recent version of the software, freshly downloaded from the Adobe website. I'll explain what I've bee