Group of subquery

Hello

I need help in the following

DB version: 11 GR 2

several times that I need to find the duplicate rows exist in a table.where I do it in two parts

1 write group and get duplicates

select customer_id , count(order_id) from customers c , orders o where c.customer_id=o.customer_id group by customer_id having count(order_id ) >1;


2. more than lines returned in the second request for information

{code}

Select c., s.*

customers c, order_status s

where c.customer_id = s.customer_id

and c.customer_id (values from the query above);

{code}

instead of plugging the details, I can complete the foregoing in a stmt

Please notify

Thank you for your time

Hello mydba

Try this:

SELECT *.

FROM (SELECT c., s.*)

, COUNT (c.customer_id) OVER (PARTITION BY c.customer_id) AS CNT_ID

CUSTOMERS c

s order_status

where c.customer_id = s.customer_id

)

WHERE cnt_id > 1

;

In this case, you should get all the visitors that they arrested more than 1.

I hope it helps.

Tags: Database

Similar Questions

  • Cannot return number of orders by the hour in the request please help

    Here's the query I run currently. It allows you to select the Date and time on a form and returns a total number of commands between the Date and the selected hours. What I need to break further down, at the time, the number of orders. So it should take the 05:00 06/08/2012-06/08/2012 11:00 show me the number of orders at the time. Where currently it totals orders for the chosen duration

    This query currently works but does not divide it by time unless I select the 05:00 06/08/2012-06/08/2012 06:00 and then repeat for each hour. Which is very time consuming.

    Any help would be amazing as I have been at it for weeks.

    SELECT
    ENTITIES OF ROUND (SUM (COUNTWEIGHT)), AREA OF Z.NAME
    Of
    PPSDBA. ARCH_RX RX,
    PPSDBA. ARCH_VIAL_CONTENTS STROKE,
    PPSDBA. ZONE Z,
    (SELECT 1 / COUNT (RX.) TOTE_ORDER_ID COUNTWEIGHT), RX. TOTE_ORDER_ID ORDER NO.
    OF PPSDBA. ARCH_RX RX,
    PPSDBA. ARCH_VIAL_CONTENTS STROKE,
    PPSDBA. ARCH_VIAL AV
    WHERE AV RX_ID = RX. RX_ID AND
    STROKE. VIAL_ID = BC VIAL_ID AND
    RX. FILLED_TYPE NOT IN (19.20) and
    STROKE. FILL_DATE BETWEEN TO_DATE('#DATE_FROM#', 'MM/DD/YYYY hh24:mi') AND TO_DATE (' #DATE_TO # "," MM/DD/YYYY HH24 ')
    RX GROUP. SUBQUERY TOTE_ORDER_ID),
    PPSDBA. ARCH_VIAL AV
    WHERE
    SUBQUERY. ORDERID = RX. TOTE_ORDER_ID AND
    AV. RX_ID = RX. RX_ID AND
    RX. FILLED_TYPE = Z.ZONE_ID AND
    STROKE. VIAL_ID = BC VIAL_ID AND
    RX. FILLED_TYPE NOT IN (19.20) and
    STROKE. FILL_DATE BETWEEN TO_DATE('#DATE_FROM#', 'MM/DD/YYYY hh24:mi') AND TO_DATE (' #DATE_TO # "," MM/DD/YYYY HH24 ')
    Z.NAME GROUP

    This will probably work. I can't test it because I don't have the tables:

    SELECT
    TRUNC (STROKE. FILL_DATE, 'HH') FILL_DATE_HOUR,.
    ENTITIES OF ROUND (SUM (COUNTWEIGHT)), AREA OF Z.NAME
    Of
    PPSDBA. ARCH_RX RX,
    PPSDBA. ARCH_VIAL_CONTENTS STROKE,
    PPSDBA. ZONE Z,
    (SELECT 1 / COUNT (RX.) TOTE_ORDER_ID COUNTWEIGHT), RX. TOTE_ORDER_ID ORDER NO.
    OF PPSDBA. ARCH_RX RX,
    PPSDBA. ARCH_VIAL_CONTENTS STROKE,
    PPSDBA. ARCH_VIAL AV
    WHERE AV RX_ID = RX. RX_ID AND
    STROKE. VIAL_ID = BC VIAL_ID AND
    RX. FILLED_TYPE NOT IN (19.20) and
    STROKE. FILL_DATE BETWEEN TO_DATE('#DATE_FROM#', 'MM/DD/YYYY hh24:mi') AND TO_DATE (' #DATE_TO # "," MM/DD/YYYY HH24 ')
    RX GROUP. SUBQUERY TOTE_ORDER_ID),
    PPSDBA. ARCH_VIAL AV
    WHERE
    SUBQUERY. ORDERID = RX. TOTE_ORDER_ID AND
    AV. RX_ID = RX. RX_ID AND
    RX. FILLED_TYPE = Z.ZONE_ID AND
    STROKE. VIAL_ID = BC VIAL_ID AND
    RX. FILLED_TYPE NOT IN (19.20) and
    STROKE. FILL_DATE BETWEEN TO_DATE('#DATE_FROM#', 'MM/DD/YYYY hh24:mi') AND TO_DATE (' #DATE_TO # "," MM/DD/YYYY HH24 ')
    Z.NAME GROUP, , TRUNC (STROKE. FILL_DATE, 'HH')

  • A query containing a subquery and Group By all-in-one

    Hi people,
    Any interesting fact but nerve racking situation I have here. I have a question that I have to do counts and amounts and I also need to have a Sub inside query. When I run my application, I get an error like this:

    Error in the command line column: 4:36
    Error report:
    SQL error: ORA-00979: not a GROUP BY expression
    00979 00000 - "not a GROUP BY expression"
    * Cause:
    * Action:

    Here's my query:
    select distinct to_char(c.start_date,'YYYY') start_date,
             substr(acc.account_num,1,12) account_number,
           ( select acc2.account_desc from student_account  acc2
             where  acc2.account_num = acc.account_num 
             and    acc2.account_type = 'A'
           ),
           COUNT(distinct(s.student_id))
    from student s,
           student_account acc,
           course   c
    where s.course_id = c.course_id
    and     s.account_num = acc.account_num
    and    to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31'
    group by to_char(c.start_date,'YYYY'), substr(acc.account_num,1,12)
    order by 1;
    If I replace it with the acc.account_num (subquery) with a number that is in the database, then my request works well.

    Published by: Roxyrollers on March 29, 2011 16:45

    Hello

    We don't know what you want. It would really help if you posted some examples of data (CREATE TABLE and INSERT statements) and the results desired from these data.

    Are you sure that the scalar subquery will return ever more than one line? If so, you can wrap in a MIN or MAX aggregate function, as I've suggested before:

     select distinct to_char(c.start_date,'YYYY') start_date,
             substr(acc.account_num,1,12) account_number,
       MIN ( ( select acc2.account_desc from student_account  acc2     -- Changed
             where  acc2.account_num = acc.account_num
             and    acc2.account_type = 'A'
           ) ),                                   -- Changed
           COUNT(distinct(s.student_id))
    from student s,
           student_account acc,
           course   c
    where s.course_id = c.course_id
    and     s.account_num = acc.account_num
    and    to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31'
    group by to_char(c.start_date,'YYYY'), substr(acc.acct_acc_num,1,12)
    order by 1;
    

    It seems that you could also simplify the query and make more fast, eliminating the scalar subquery. The table there is already part of the main query, you can do something like this:

    SELECT    TO_CHAR (c.start_date, 'YYYY')     AS start_date
    ,         SUBSTR (acc.account_num, 1, 12)      AS account_number
    ,       MIN ( CASE
                        WHEN  acc.account_type = 'A'
                  THEN  account_desc
                    END
               )                         AS a_desc
    ,         COUNT (DISTINCT s.student_id)          AS student_id_cnt
    FROM       student            s
    ,       student_account     acc
    ,       course          c
    WHERE       s.course_id          = c.course_id
    AND       s.account_num          = acc.account_num
    AND       c.start_date          BETWEEN DATE '2004-01-01'
                          AND     DATE '2009-12-31'
    GROUP BY  TO_CHAR (c.start_date, 'YYYY')
    ,            SUBSTR (acc.acct_acc_num, 1, 12)
    ORDER BY  start_date;
    

    I guess that course.start_date is a DATE. Instead of converting each start_date to string (so you can see if it is in the right range), it is more efficient and less prone to error, compared to other DATEs, as shown above.

    It is very strange that you

    substr(acc.account_num,1,12)
    

    in the select clause, but

    substr(acc.acct_acc_num,1,12)
    

    in the GROUP BY clause. Did you mean having the same column in both places?

    Yet once, without seeing some examples of data (CREATE TABLE and INSERT statements) and the results desired from this data, I can't do much.

  • Subquery in GROUP BY CLAUSE

    Is it possible to place a subquery in the GROUP BY CLAUSE? If this is the case, can anyone show me an example? Thank you

    Hello

    This is a GROUP BY clause that uses an IN subquery:

    SELECT    COUNT (*)     AS cnt
    FROM       scott.emp
    GROUP BY  CASE
              WHEN deptno  IN (
                             SELECT     deptno
                             FROM     scott.dept
                             WHERE     loc     = 'NEW YORK'
                        )
              THEN  1
              ELSE  2
           END
    ;
    

    I've never seen a need for it (and I was at a state fair, a rodeo and a picnic!)

    Interestingly, if I copy the expression in the GROUP BY clause from the SELECT clause:

    SELECT    COUNT (*)     AS cnt
    ,       CASE
              WHEN deptno  IN (
                             SELECT     deptno
                             FROM     scott.dept
                             WHERE     loc     = 'NEW YORK'
                        )
              THEN  1
              ELSE  2
           END          AS new_york
    FROM       scott.emp
    GROUP BY  CASE
              WHEN deptno  IN (
                             SELECT     deptno
                             FROM     scott.dept
                             WHERE     loc     = 'NEW YORK'
                        )
              THEN  1
              ELSE  2
           END
    ;
    

    I get this error:

    ...             WHEN deptno  IN (
                         *
    ERROR at line 3:
    ORA-00979: not a GROUP BY expression
    

    Despite what this book says you can use a subquery-IN like this in an ORDER BY clause:

    SELECT       deptno
    FROM       scott.emp
    ORDER BY  CASE
              WHEN deptno  IN (
                             SELECT     deptno
                             FROM     scott.dept
                             WHERE     loc     = 'DALLAS'
                        )
              THEN  1
              ELSE  2
           END
    ;
    

    (New York is Department 10, the first anyway, is not as clear to use the same exact example.)

  • Grouping of SQL with a subquery

    Hi all

    I had to get an output by combining the same field by using 2 different condition. I tried to use the subquery, but the output is not getting properly completed.

    example of the query will be something like:

    Select count (*), _ (select count (*) from TableX where = 1 condition) from TableX Grouper by FieldA.

    The output is considered the first condition + (Count + have 2 folders and subquery (.. .or condition = 1) with also 2 records.)

    But the real result for the subquery if I run the query separately is only 1 card. The query has filled the second row with the same performance as a result of the first condition with 2 records to return. Anyway I put a condition to bring together both the condition?

    Thank you.

    Something like that?

    select sum(case when  then 1 else 0 end ) first_count, sum(1) total_count from TableX group by FieldA;
    

    Arun-

    Not tested

  • Subquery in the select clause and not "a group by expression" error.

    Hi all
    I hope someone out there can help me out with this one. I have a sql query that performs a subquery in the select case combined with an aggregate function and gives me a "not a group by expression.

    I created a simplified example
     SELECT COUNT(*)                     ,
      ai.invoice_num invoice_number      ,
      ai.description invoice_description ,
      (SELECT poh.po_header_id
           FROM po_headers poh ,
          po_distributions pod
          WHERE pod.po_distribution_id = aid.po_distribution_id
        AND pod.po_header_id           = poh.po_header_id
        ) po_number
         FROM ap_invoice_distributions aid ,
        ap_invoices ai
        WHERE ai.invoice_id = 1476932
      AND aid.project_id    = 8608   
      AND aid.task_id       = 462202  
      AND ai.invoice_id     = aid.invoice_id
     GROUP BY ai.invoice_num,
                    ai.description;
    I'm sure he expected me to add the po_number to the group by clause, but this does not work and gives the error "invalid identifier".

    Can someone suggest a way for a new concert the query so that I can add po_number to my education by clause.

    Kris

    You should try either:

    SELECT COUNT (*),
    AI.invoice_num invoice_number,
    have. Description invoice_description,
    (SELECT poh.po_header_id
    OF po_headers poh,.
    pod po_distributions
    WHERE pod.po_distribution_id = aid.po_distribution_id
    AND pod.po_header_id = poh.po_header_id
    ) po_number
    HELP ap_invoice_distributions.
    ap_invoices AI
    WHERE ai.invoice_id = 1476932
    AND aid.project_id = 8608
    AND aid.task_id = 462202
    AND ai.invoice_id = aid.invoice_id
    Ai.invoice_num GROUP,
    have. Description, aid.po_distribution_id;

    or

    SELECT COUNT (*),
    AI.invoice_num invoice_number,
    have. Description invoice_description,
    MAX ((SELECT poh.po_header_id
    OF po_headers poh,.
    pod po_distributions
    WHERE pod.po_distribution_id = aid.po_distribution_id
    AND pod.po_header_id = poh.po_header_id
    po_number))
    HELP ap_invoice_distributions.
    ap_invoices AI
    WHERE ai.invoice_id = 1476932
    AND aid.project_id = 8608
    AND aid.task_id = 462202
    AND ai.invoice_id = aid.invoice_id
    Ai.invoice_num GROUP,
    have. Description;

  • Analytical and group functions of

    Oracle 11g Release 2 Server

    SQL, SQLPLUS not

    I need to 'break' when a column value changes and insert a line with only a dash.

    CREATE TABLE tab
    (   id            number
      , part_num      number
      , part_type_id  number
      , part_type_txt varchar2(50)
      , CONSTRAINT tab_pk PRIMARY KEY(part_num,part_type_id)
    )
    /
    
    insert into tab values(1,10,100,'hose') ;
    insert into tab values(1,10,110,'hose clamp') ;
    insert into tab values(1,20,200,'plastic value') ;
    insert into tab values(1,20,210,'brass value') ;
    insert into tab values(1,30,300,'headlamp') ;
    insert into tab values(1,30,310,'lamp misc') ;
    commit ;
    
    select part_num,part_type_id,part_type_txt, count(part_num) CNT
    from   tab
    where  id = 1
    group by part_num,part_type_id,part_type_txt
    order by part_num,part_type_id ;
    
      PART_NUM PART_TYPE_ID PART_TYPE_TXT       CNT
    ---------- ------------ ------------------- ----------
            10          100 hose                         1
            10          110 hose clamp                   1
            20          200 plastic value                1
            20          210 brass value                  1
            30          300 headlamp                     1
            30          310 lamp misc                    1
    

    My query:

    WITH   data
    AS
    (
       select   case when nvl( lag( part_num ) over( order by rownum ), ' ' ) != part_num
                     then part_num
                end part_num
             ,  part_type_id, part_type_txt, count(part_num) CNT
       from
                tab
       where    id = 1 
       group by    case when nvl( lag( part_num ) over( order by rownum ), ' ' ) != part_num
                        then part_num
                   end 
                 , part_type_id
                 , part_type_txt
       order by part_num,part_type_id 
    )
    SELECT rownum, d.part_num,d.part_type_id, d.part_type_txt
    FROM   data   d ;
    
    ERROR at line 11:
    ORA-30483: window  functions are not allowed here
    
    
    

    I guess that analytical functions are not allowed in a group by clause.

    Output desired, he had to 'break' on when PART_NUM changes and insertion of a line with a dash (-):

      PART_NUM PART_TYPE_ID PART_TYPE_TXT       CNT
    ---------- ------------ ------------------- ----------
            10          100 hose                         1
            10          110 hose clamp                   1
            -
            20          200 plastic value                1
            20          210 brass value                  1
            -
            30          300 headlamp                     1
            30          310 lamp misc                    1
    

    Any help appreciated.

    Hello

    I see: XYZ123 is linked only to a part_type_id, 7777, so you do not want any output to XYZ123.

    Similarly, XYZ456 is only linked to a part_type_id, 8888, so you do not want XYZ456.

    One thing you can do is to start with the query in response to #4 above.  The results of this query, you can count part_type_ids h: different lots, each part_num is bound to and then only display the part_nums who have more than 1 part_type_id.  For example:

    WITH got_aggregates AS

    (

    SELECT THE CHECK BOX

    WHEN you GROUP (part_type_id) = 0

    THEN TO_CHAR (part_num)

    ELSE '-'

    END                               AS txt

    part_type_id, part_type_txt

    CASE

    WHEN you GROUP (part_type_id) = 0

    THEN COUNT (part_num)

    END                               AS cnt

    part_num,

    COUNT (DISTINCT part_type_id)

    COURSES (PARTITION BY part_num) AS part_type_id_cnt

    TAB

    ID WHERE = 1

    GROUP BY part_num, ROLLUP (part_type_id, part_type_txt))

    )

    SELECT txt, part_type_id, part_type_txt, cnt

    OF got_aggregates

    WHERE part_type_id_cnt > 1

    ORDER BY part_num, part_type_id

    ;

    Note that the subquery got_aggregates is almost identical to the response query #4.  The only differences are that the subquery is not an ORDERBY clause (ORDER BY is almost always useless to subqueries) and includes two additional columns, which will be needed in the WHERE and ORDER BY the main query clauses.

  • Help in the treatment of "subquery returns multiple rows.

    Appreciate your help on this.

    I have under sql query fails with the error "ORA-01427: einreihig subquery returns multiple lines"

    WITH t
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL
             UNION
             SELECT 'E', 'F', 'G' FROM DUAL
             UNION
             SELECT 'A', 'T', 'U' FROM DUAL),
         t1
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'D1', 'E1', 'F1' FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL
             UNION
             SELECT 'S', 'V', 'W' FROM DUAL),
         t2
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'A', 'E1', 'F1' FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL)
    SELECT t.col1,
           (SELECT distinct col2
              FROM t2
             WHERE t2.col1 = t.col1) RND_COL
      FROM t, t1
    WHERE t.col1 = t1.col1
    
    WhenI checked the count using below query
    
    WITH t
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL
             UNION
             SELECT 'E', 'F', 'G' FROM DUAL
             UNION
             SELECT 'A', 'T', 'U' FROM DUAL),
         t1
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'D1', 'E1', 'F1' FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL
             UNION
             SELECT 'S', 'V', 'W' FROM DUAL),
         t2
         AS (SELECT 'A' Col1, 'B' Col2, 'C' Col3 FROM DUAL
             UNION
             SELECT 'A', 'E1', 'F1' FROM DUAL
             UNION
             SELECT 'D', 'E', 'F' FROM DUAL)
    SELECT t.col1,
           (SELECT count(col2)
              FROM t2
             WHERE t2.col1 = t.col1) RND_COL
      FROM t, t1
    WHERE t.col1 = t1.col1 (+)
    
    it shows as below
    -----------------
    COL1    RND_COL
    A    2
    A    2
    D    1
    E    0
    
    My Requirement is for all the values in the column 'RND_COL' > 1 it should display text as 'Multiple', for 'RND_COL' = 1 display actual value returned by sub query and 'RND_COL' = 0 display it as NULL.
    
    expected output
    -------------------
    COL1    RND_COL
    A       Multiple
    A       Multiple
    D       E
    E       NULL
    
    
    
    

    Hello

    Here's another way.  It's like the solution in response #1, but it does not use a subquery:

    SELECT t.col1

    CASE

    WHEN COUNT (DISTINCT t2.col2) > 1

    THEN "Multiple."

    For ANOTHER MIN (t2.col2)

    END AS rnd_col

    T

    LEFT OUTER JOIN t2 ON t2.col1 = t.col1

    GROUP OF t.col1, t.col2, t.col3

    ORDER BY t.col1

    ;

    This assumes that in table t (col1, col2, col3) combination is unique.  If theat is not the case, change the GROUP BY clause, but you need to ensure that all lines of t becomes a separate group.

  • last album back in a subquery

    Hi, I have the following table:


    Table_a

    record_id def_id Date
    111June 7
    134June 1
    155May 5
    234June 4
    211May 8

    Table_A_DEF

    def_id def_name
    11Examine
    34definitionF
    50DEFINITIONH
    55definitionN

    Table_B

    record_id record_name
    1Record1
    2Record2
    3record Record3

    I need to check if there are records from Table_A 'examine' or 'definitionN' as their last entry.

    So, in that the above example, I would expect only record 1 to return as his last entry in the table def_id = 11.

    That's what I have, but obviously does not work as it throws an error on the max function, can someone please help with it?

    select * from Table_B b
    where exists (
      select * from Table_A a
                where a.record_id in (b.record_id) and max(a.def_id) in (
                select ad.def_id from Table_A_DEF ad where ad.def_name in ('definitionA', 'definitionN')
      )
    
    

    Here's a way (using either exist or subquery in the list)...

    with Table_A (record_id, def_id, date_A) as)

    Select 1, 11, date ' 2015-06-07 the Union double all the

    Select day 1, 34, '' 2015-06-01 Union double all the

    Select 1, 55, date ' 2015 - 05 / 05' Union double all the

    Select option 2, 34, day ' 2015-06-04 Union double all the

    Select option 2, 11, date ' double' 2015-05-08

    )

    Table_A_DEF (def_id, def_name) as)

    Select 11, 'examine' from dual union all

    Select 34, 'definitionF' from dual union all

    Select 50, 'DEFINITIONH' from dual union all

    Select 55, "definitionN' from dual

    )

    Table_B (record_id, record_name) as)

    Select 1, "record1" dual union all

    Select 2, 'record2"dual union all

    Select 3, 'registration' record3 of the double

    )

    Select record_id, record_name

    of table_B

    When there is)

    Select null

    from table_A

    Join table_A_DEF

    using (def_id)

    where table_A.record_id = table_B.record_id

    Record_id group

    having max (def_name) to keep (last dense_rank command by date_A)

    in ("examine", "definitionN")

    )

    ;

    -or-

    Select record_id, record_name

    of table_B

    where record_id in)

    Select record_id

    from table_A

    Join table_A_DEF

    using (def_id)

    Record_id group

    having max (def_name) to keep (last dense_rank command by date_A)

    in ("examine", "definitionN")

    )

    ;

    If you post in the forum SQL you will get probably more answers, however.

    Kind regards

    Bob

  • create id by group

    Hello

    I have a requirement that seems simple, but I'm unable to create the sql for it.

    We have a table on a database 10.2.0.4 that looks like this:

    create the table groupid_example

    (

    Identification number not null,

    VARCHAR2 (32) F1.

    F2 varchar2 (32),

    F3 varchar2 (32),

    number of GroupId,

    form sdo_geometry

    );

    And contains the records:

    insert into GROUPID_EXAMPLE (ID, F1, F2, F3, GROUPID) values (1, 'A', 'A', 'A', null);

    insert into GROUPID_EXAMPLE (ID, GROUPID, F1, F2, F3) values (2, 'A', 'A', 'A', null);

    insert into GROUPID_EXAMPLE (ID, GROUPID, F1, F2, F3) values (3, 'B', 'B', 'B', null);

    insert into GROUPID_EXAMPLE (ID, F1, F2, F3, GROUPID) values (4, 'B', 'B', 'C', null);

    insert into GROUPID_EXAMPLE (ID, F1, F2, F3, GROUPID) values (5, 'C', 'C', 'C', null);

    insert into GROUPID_EXAMPLE (ID, F1, F2, F3, GROUPID) values (6, 'C', 'C', 'C', null);

    The sdo_geometry is really irrelevant to the requirement, but it prevents the use of the merge statement.

    The condition is that all records with the same f1, f2 and f3 values must obtain the same value to the groupid. In addition, the groupid must be numbered in the order increasing by f1, f2, f3.

    The power required is:

    ID F1 F2 F3 GROUPID
    1AAA1
    2AAA1
    3BBB2
    4BBC3
    5CCC4
    6CCC4

    I tried using row_number() like this:

    Update groupid_example g

    Set groupid = (select groupid

    from (select row_number() over (order by f1, f2, f3) groupid, f1, f2, f3)

    Group groupid_example by f1, f2, f3) s

    where g.f1 = s.f1

    and g.f2 = s.f2

    and g.f3 = s.f3);

    This works fine on a small set, but takes forever on the 1 million rows in the table.

    There must be a simpler and more elegant way to do...

    Thanks in advance!

    Rob

    Pleiadians wrote:

    From a theoretical point of view, it just bothers me that a pl/sql cursor loop that updates the records online is much faster than a pure SQL.

    It is as you have already said pointing to the plan of the explain command.

    In this case no unnesting of the subquery is possible and that is why there is a hughe work performed for each line.

    You can move it with simple sql if you are using views fusion or updateble.

    None of this applies in this case, if im my opinion you have discovered the best approach for yourself.

  • How to determine the number of records in a subquery

    I am fairly new to Oracle SQL, and would like to know if anyone has examples of how to perform the following operations:

    1. I would return the number of rows that exist in the following SQL statement below. This SQL will be part of a larger SQL statement and I will be passing in the EMPLID identifier in the subquery. The result must be a value showing the total number of records that correspond to the SQL below. "xyz" will be finally passing through the extension number of the employee by using a reference to the parent SQL... ie: jbe. POSITION_NBR
    2. My SQL:

    SELECT Cjbe. EMPLID OF PS_JOB cjbe
    WHERE cjbe. POSITION_NBR = "xyz".
    GROUP BY cjbe. EMPLID

    I would like to know how I can change the value of poarent to POSITION_NBR (I think I use jbe.) POSITION_NBR) total calls the EMPLID found in this subquery subquery and then return.

    Thanks for your help.

    Maybe I lack something, but isn't this just

    SELECT count (distinct empl_id) from ps_job where position_nbr =: some_position_nbr

  • Single - row subquery returns more than one line.

    Hi Experts

    I am faced with error

    ORA-01427: single - row subquery returns more than one line.

    MyQuery is:

    select
       TO_CHAR(T.MR_REG_DATE,'DD')                     "DATE"
       ,CASE  
         WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) between 0 and 1 THEN ' 01'||'  - ('||'0 - 1 Month)'
         WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 2 AND 12 THEN ' 02'||'  - ('||'2 - 12 Months)'
         WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 13 AND 60 THEN ' 03'||'  - ('||'1 - 5 Years)'
         WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 61 AND 120 THEN ' 04'||'  - ('||'5 - 10 Years)'
         WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) > 120 then ' 05'||'  - ('||'> 10 Years)'
        END age
      ,count(T.Mr_Code) No_of_Patient
      ,(  SELECT count(x.mr_code) mr_code
             FROM HMIS_MRINFO X
             where X.mr_reg_date between &FRM_DATE AND &TO_DATE
               and X.mr_code NOT in (select Y.mr_code from hmis_pat_add_dis_detail Y
                                      WHERE Y.mr_reg_date between &FRM_DATE AND &TO_DATE
                                    )
            GROUP BY CASE  
                       WHEN trunc((MONTHS_BETWEEN(X.MR_REG_DATE,X.MR_DOB))) between 0 and 1 THEN ' 01'||'  - ('||'0 - 1 Month)'
                       WHEN trunc((MONTHS_BETWEEN(X.MR_REG_DATE,X.MR_DOB))) BETWEEN 2 AND 12 THEN ' 02'||'  - ('||'2 - 12 Months)'
                       WHEN trunc((MONTHS_BETWEEN(X.MR_REG_DATE,X.MR_DOB))) BETWEEN 13 AND 60 THEN ' 03'||'  - ('||'1 - 5 Years)'
                       WHEN trunc((MONTHS_BETWEEN(X.MR_REG_DATE,X.MR_DOB))) BETWEEN 61 AND 120 THEN ' 04'||'  - ('||'5 - 10 Years)'
                       WHEN trunc((MONTHS_BETWEEN(X.MR_REG_DATE,X.MR_DOB))) > 120 then ' 05'||'  - ('||'> 10 Years)'
                      END 
      ) Missing_MR
    from hmis_mrinfo T,hmis_pat_add_dis_detail M
    where T.mr_code = M.mr_code(+)
      and T.mr_reg_date between &FRM_DATE AND &TO_DATE
      &AGE_GRP
    GROUP BY T.MR_REG_DATE
             ,CASE  
               WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) between 0 and 1 THEN ' 01'||'  - ('||'0 - 1 Month)'
               WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 2 AND 12 THEN ' 02'||'  - ('||'2 - 12 Months)'
               WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 13 AND 60 THEN ' 03'||'  - ('||'1 - 5 Years)'
               WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) BETWEEN 61 AND 120 THEN ' 04'||'  - ('||'5 - 10 Years)'
               WHEN trunc((MONTHS_BETWEEN(T.MR_REG_DATE,T.MR_DOB))) > 120 then ' 05'||'  - ('||'> 10 Years)'
              END 
    ORDER BY T.MR_REG_DATE;
    

    Please give some advice / solution.

    I think this might do it for you

    Select

    TO_CHAR (T.MR_REG_DATE, 'DD') "DATE."

    CASE

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) between 0 and 1 THEN ' 01' |'.  - ('||' 0-1 month)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 2 AND 12 THEN ' 02' |'.  - ('||' 2-12 months)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 13 AND 60 THEN ' 03' |'.  - ('||' 1-5 years)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 61 AND 120 THEN ' 04' |'.  - ('||' 5-10 years)'

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) > 120 then ' 05' |'.  ' - ('| ' > 10 years).

    Age of the END

    count (T.Mr_Code) No_of_Patient

    , count (case when t.mr_code NOT in (select Y.mr_code from hmis_pat_add_dis_detail Y))

    WHERE Y.mr_reg_date between & FRM_DATE AND & TO_DATE)

    then t.mr_code

    (end) Missing_MR

    of hmis_mrinfo T, hmis_pat_add_dis_detail M

    where T.mr_code = M.mr_code (+)

    and between T.mr_reg_date & FRM_DATE AND & TO_DATE

    & AGE_GRP

    T.MR_REG_DATE GROUP

    CASE

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) between 0 and 1 THEN ' 01' |'.  - ('||' 0-1 month)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 2 AND 12 THEN ' 02' |'.  - ('||' 2-12 months)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 13 AND 60 THEN ' 03' |'.  - ('||' 1-5 years)"

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) BETWEEN 61 AND 120 THEN ' 04' |'.  - ('||' 5-10 years)'

    WHEN trunc ((MONTHS_BETWEEN (T.MR_REG_DATE, T.MR_DOB))) > 120 then ' 05' |'.  ' - ('| ' > 10 years).

    END

    ORDER BY T.MR_REG_DATE;

  • Recursive subquery factoring: calculate aggregates

    Table T represents a tree. Each record is a node, and each node has only one parent. This query calculates the SUM() of each branch for each node.

    WITH T AS
            (SELECT  1 ID, NULL parent_id, NULL VALUE FROM dual UNION ALL
             SELECT 10 ID,    1 parent_id, 1000 VALUE FROM dual UNION ALL
             SELECT 20 ID,    1 parent_id, 2000 VALUE FROM dual UNION ALL
             SELECT 30 ID,   10 parent_id, 3000 VALUE FROM dual UNION ALL
             SELECT 40 ID,   10 parent_id, 4000 VALUE FROM dual UNION ALL
             SELECT 50 ID,   20 parent_id, 5000 VALUE FROM dual UNION ALL
             SELECT 60 ID,    1 parent_id, 6000 VALUE FROM dual UNION ALL
             SELECT 70 ID,   60 parent_id, 7000 VALUE FROM dual UNION ALL
             SELECT 80 ID,   70 parent_id, 8000 VALUE FROM dual
        ) SELECT CAST(LPAD(' ', (LEVEL-1)*4) || ID AS VARCHAR2(20))  id
            ,VALUE                                                   self_value
          , (SELECT SUM (VALUE)
             FROM   T t2
             CONNECT BY 
               PRIOR t2.ID = t2.parent_id
               START WITH ID = T.ID)                                 branch_value
        FROM   T
        CONNECT BY PRIOR t.id = t.parent_id
        START WITH t.parent_id IS NULL
        ORDER SIBLINGS BY t.id;
    
    ID                   SELF_VALUE BRANCH_VALUE
    -------------------- ---------- ------------
    1                                      36000
        10                     1000         8000
            30                 3000         3000
            40                 4000         4000
        20                     2000         7000
            50                 5000         5000
        60                     6000        21000
            70                 7000        15000
                80             8000         8000
    
    9 rows selected.
    

    I tried to reach the same result of this query using the new syntax for subquery factoring. Any help would be really appreciated!

    Hello

    I think it's one of those things that CONNECT BY is better.

    Here's a way to do it using a recursive clause (AND not CONNECT BY):

    WITH recursive_results (ancestor_id, descendant_id, value, lvl, lineage) AS

    (

    SELECT id AS ancestor_id

    id LIKE descendant_id

    value

    ,       1                     AS lvl

    , TO_CHAR (id, "9999") AS line

    T

    UNION ALL

    SELECT r.ancestor_id

    t.id AS descendant_id

    t.valeur

    r.lvl + 1 AS lvl

    r.lineage | ' /'

    || To_char (t.id, '9999') AS line

    T

    JOIN recursive_results r WE t.parent_id = r.descendant_id

    )

    SELECT LPAD (' ' ')

    , 4 * (

    SELECT MAX (lvl) - 1

    OF recursive_results

    WHERE descendant_id = m.ancestor_id

    )

    ) || ancestor_id AS indented_id

    SUM (CASE WHEN ancestor_id = descendant_id THEN value END) AS self_value

    The amount (value) AS branch_value

    OF recursive_results m

    GROUP BY ancestor_id

    ORDER BY)

    SELECT MAX (lineage) DUNGEON (DENSE_RANK LAST ORDER BY lvl)

    OF recursive_results

    WHERE descendant_id = m.ancestor_id

    )

    ;

    Output (even you have):

    INDENTED_ID SELF_VALUE BRANCH_VALUE

    -------------------- ---------- ------------

    1                                      36000

    10-1000-8000

    30 3000 3000

    40 4000 4000

    20-2000-7000

    50 5000 5000

    60 6000 21000

    70-7000-15000

    80-8000-8000

  • A subquery in the From Clause

    How can I model the RPD with the sub query that has the subquery in the from Clause.

    SELECT

    o948938. CONSOLIDATED_NAME,

    (SUM (o948992. YTD_COMPLETED)) / (SUM (TOTAL_OCC_AP)) AS C_1,.

    SUM (TOTAL_OCC_AP) AS TOTAL_OCC_AP,

    Of

    ORG_DIM o948938,

    TIME_MONTHLY_DIM o948963,

    INSPECTION_FACT o948992,

    (SELECT TDS_NUM,

    MONTH_ID,

    SUM (TOTAL_APTS) TOTAL_AP,

    OF SUMMARY_FACT

    TDS_NUM GROUP,

    MONTH_ID

    ) O949126

    WHERE (o949126. MONTH_ID = o948992. MONTH_ID (+)

    AND o949126. TDS_NUM = o948992. TDS_NUM (+)

    AND (o948938. TDS_NUM = o949126. TDS_NUM)

    AND (O948963. MONTH_ID = O949126. MONTH_ID))

    Group

    O948938. NEW_BOROUGH_GROUPING

    Hello

    You can do this via an opaque view.

    You can also do this by modeling the cumulative value as a calculation LOGIC in the group by aggregation "pinned" to a specific dimension hierarchy that reflects consolidation in the online posting.

    Hope this helps,

    Robert.

  • grouping of XML

    looking for help generate xml. Thanks to all who can help. im sure I do something wrong with the group. This inner query must also generate in a clob, so that the final result is 1 plug clob so my finished result is a clob.

    It works fine when I do by itself

    ------------

    SELECT

    XMLELEMENT

    (

    'OBJECT ',.

    XMLATTRIBUTES)

    b.OBJECT AS 'NAME '.

    ), XMLAGG

    (

    XMLELEMENT

    (

    "WELL,"

    XMLELEMENT

    (

    'NAME' PROPERTY

    ),

    XMLELEMENT

    (

    "DESCRIPTION", DESCRIPTION

    )

    )

    )

    )

    OF mysecondtable b

    GROUP BY b.OBJECT

    ------------

    but when I paste this into my other request, that it will fail with this error.

    ORA-01427: einreihig subquery returns multiple rows

    ----

    SELECT

    xmlserialize

    (

    content

    (

    XMLELEMENT

    (

    "trials."

    XMLATTRIBUTES)

    SYSTIMESTAMP AS 'DATE'),

    XMLELEMENT ("FUNCTIONS",

    (select XMLAGG

    (

    XMLELEMENT

    (

    "FUNCTION."

    XMLELEMENT

    (

    "U_ID", a.U_ID

    ) ,

    XMLELEMENT

    (

    'Function_name', a.FUNC_NAME

    ) ,

    XMLELEMENT

    (

    'PATH', a.PATH

    ) ,

    XMLELEMENT

    (

    "INCLUDE", a.INCLUDE

    ) ,

    XMLELEMENT

    (

    'DESC', a.DESC

    ) ,

    XMLELEMENT

    (

    'EX', a.EX

    ) ,

    XMLELEMENT

    (

    'DOC', a.DOC

    ) ,

    XMLELEMENT

    (

    'STATUS', a.STATUS

    )  ,

    XMLELEMENT

    (

    "PROP."

    (

    SELECT

    XMLELEMENT

    (

    'OBJECT ',.

    XMLATTRIBUTES)

    b.OBJECT AS 'NAME '.

    ),

    XMLAGG)

    XMLELEMENT

    (

    "WELL,"

    XMLELEMENT

    (

    'NAME' PROPERTY

    ),

    XMLELEMENT

    (

    "DESCRIPTION", DESCRIPTION

    )

    )

    )

    )

    OF mysecondtable b

    WHERE b.R_ID = a.U_ID

    GROUP BY b.OBJECT

    )

    )

    )

    )

    OF myfirsttable one

    )

    )

    )

    )

    as CLOB

    ) testxml

    OF THE DOUBLE

    OK, so you must group these 4 rows in a single XML using XMLAgg fragment:

    ...

    XMLELEMENT

    (

    "PROP."

    (

    SELECT XMLAGG)

    XMLELEMENT

    (

    'OBJECT ',.

    XMLATTRIBUTES)

    b.OBJECT AS 'NAME '.

    )

    ...

Maybe you are looking for

  • Can I add a drop-down list for the bookmarks toolbar?

    I would like to have a series of categories of bookmarks in the bookmarks toolbar. Let's say a button called "Household" could have a menu drop down menu links to my sites popular for "Home repair," 'Kitchen', 'Furniture finish', etc. And maybe anoth

  • Satellite 5105-S501 keyboard problem

    After that I replaced my 40 GB hard drive with a 100 GB hard drive (the old frozen drive), I had a lot of problems with my keyboard.When I type, I find that the letters 'i' with tab back until a strange moment, the letter 'u' can tab forward, other l

  • SQLite3 connection string?

    Hello I have a SQLite3 database file. I read with labivew. I installed the ODBC driver since http://www.ch-werner.de/sqliteodbc/. I created the DSN. I would like to read different with their paths .db files. If I use directly the DSN 'temperatura' is

  • Ridge SVFA and the Relative Phase amplitude must be reentrant

    I think I found an obscure problem with one of the "Baseband FFT" s VI on the 'Frequency analysis' subpalette of the Sound and Vibration Measurement Suite. " The "Peak Amplitude and Relative Phase SVFA" is set to no reentrant run, but I think it must

  • my HP Z600 (dual CPU Q5550 / Windows 7 64 bit) freezes 'stotters' with multitreading on

    Hello I have a HP Z600 (dual CPU Q5550 / Windows 7 64 bit / 260gtx /NVIDIA 6x4gb). When I activate multithreading in my bios (version 4 786 v01.17) my windows (using the application / web browsing) to become very unstable. It will freeze (stop respon