Logical query help

Oracle 11g Release 2

Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.

CASE 1:


create table t

(key primary id number,)

supplier_id number,

number of supplier_desc_id

batch number,

date of dt_recv

)

/

Insert into t

values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')

/

Insert into t

values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')

/

Insert into t

values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')

/

Insert into t

values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')

/

Insert into t

values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')

/

Insert into t

values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')

/

Insert into t

values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')

/

commit;

Select * from t;

ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV

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

35405 605 3809 0 14 JUNE 2013

58543 605 3809 0 10 DECEMBER 2013

3809 605 136793 1 11 NOVEMBER 2014

96510 605 3809 1 10 JUNE 2014

94222 605 3809 1 9 MAY 2014

108229 605 3809 3 09 - SEP - 2014

114585 605 3809 2 28 OCTOBER 2014

RULE: when there are 2 or more records with batch = 1, return the two most

recent recordings with batch = 1 AND any recording (no matter the batch) that

has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)

96510 10 June 2014 (2nd most active record with batch = 1)

136793 11 November 2014 (more current label with batch = 1)

94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)

The results should be:

ID         SUPPLIER_ID SUPPLIER_DESC_ID         STATUS_ID         DT_RECV
---------- ----------- ------------------------ ----------------- -------------
96510      605                     3809                 1          10-JUN-2014
136793     605                     3809                 1          11-NOV-2014
114585     605                     3809                 2          28-OCT-2014 >= 10-JUN-2014
108229     605                     3809                 3          09-SEP-2014 >= 10-JUN-2014


This query returns the correct results:


WITH    got_r_num    AS
(
    SELECT  id, supplier_id, supplier_desc_id, status_id, dt_recv
    ,       ROW_NUMBER () OVER ( PARTITION BY  supplier_id,supplier_desc_id,status_id
                                 ORDER BY      dt_recv  DESC
                               )   AS r_num
    FROM    t
)
,    got_dt_cutoff    AS
(
    SELECT  id, supplier_id, supplier_desc_id, status_id, dt_recv
    ,       MIN ( CASE
                      WHEN  status_id  = 1
                      AND   r_num      <= 2
                      THEN  dt_recv
                  END
                ) OVER (PARTITIN BY supplier_id,supplier_desc_id)   AS dt_cutoff
    FROM    got_r_num
)
SELECT    id, supplier_id, supplier_desc_id, status_id, dt_recv
FROM      got_dt_cutoff
WHERE     dt_recv  >= dt_cutoff
ORDER BY  dt_recv
;


NOTE: records are grouped by supplier_id/supplier_desc_id








CASE 2:

truncate table t;
insert into table t
values(45401,801300,4466,0,TO_DATE('21-AUG-2013','DD-MON-YYYY')
/


insert into table t
values(44414,801300,4466,0,TO_DATE('08-AUG-2013','DD-MON-YYYY')
/


commit ;


select * from t;








CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
---------- ----------- ------------------------ ----------------- ------------
     451      801300                     4466                 0 21-AUG-2013 
     44414      801300                     4466                 0 08-AUG-2013









 801300                     4466                 0 08-AUG-2013 

RULE: when there is no batch = 1, then return all rows

The query above does not work for this case.

CASE 3:

truncate table t;


insert into table t
values(29887,609051,1781,0,TO_DATE('19-APR-2013','DD-MON-YYYY')
/


insert into table t
values(33623,609051,1781,0,TO_DATE('24-MAY-2013','DD-MON-YYYY')
/


insert into table t
values(45477,609051,1781,0,TO_DATE('22-AUG-2013','DD-MON-YYYY')
/


insert into table t
values(54013,609051,1781,1,TO_DATE('22-OCT-2013','DD-MON-YYYY')
/


commit;


select * from t;


CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
---------- ----------- ------------------------ ----------------- -------------
     29887      609051                     1781                 0 19-APR-2013
     33623      609051                     1781                 0 24-MAY-2013
     45477      609051                     1781                 0 22-AUG-2013 
     54013      609051                     1781                 1 22-OCT-2013
















RULE: When there is only to record with batch = 1, return all rows

The query above does not work for this case.

Hello

orclrunner wrote:

Oracle 11g Release 2

Frank Kulash was able to help on this issue yesterday. But I got additional requirements. Details below.

CASE 1:

create table t

(key primary id number,)

supplier_id number,

number of supplier_desc_id

batch number,

date of dt_recv

)

/

Insert into t

values (35405,605,3809,0,TO_DATE('14-JUN-2013','DD-MON-yyyy')

/

Insert into t

values (58543,605,3809,0,TO_DATE('10-DEC-2013','DD-MON-yyyy')

/

Insert into t

values (136793,605,3809,1,TO_DATE('11-NOV-2014','DD-MON-yyyy')

/

Insert into t

values (96510,605,3809,1,TO_DATE('11-JUN-2014','DD-MON-yyyy')

/

Insert into t

values (94222,605,3809,1,TO_DATE('09-MAY-2014','DD-MON-yyyy')

/

Insert into t

values (108229,605,3809,3,TO_DATE('09-SEP-2014','DD-MON-yyyy')

/

Insert into t

values (114585,605,3809,2,TO_DATE('28-OCT-2014','DD-MON-yyyy')

/

commit;

Select * from t;

ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV

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

35405 605 3809 0 14 JUNE 2013

58543 605 3809 0 10 DECEMBER 2013

3809 605 136793 1 11 NOVEMBER 2014

96510 605 3809 1 10 JUNE 2014

94222 605 3809 1 9 MAY 2014

108229 605 3809 3 09 - SEP - 2014

114585 605 3809 2 28 OCTOBER 2014

RULE: when there are 2 or more records with batch = 1, return the two most

recent recordings with batch = 1 AND any recording (no matter the batch) that

has a DT_RECV > = only the DT_RECV of the 2nd record most of batch = 1 (June 10, 2014)

96510 10 June 2014 (2nd most active record with batch = 1)

136793 11 November 2014 (more current label with batch = 1)

94222 may 9, 2014 (not interested in this matter, since it is greater than 2 versions)

The results should be:

  1. ID SUPPLIER_ID SUPPLIER_DESC_ID BATCH DT_RECV
  2. ---------- ----------- ------------------------ ----------------- -------------
  3. 96510 605 3809 1 10 JUNE 2014
  4. 3809 605 136793 1 11 NOVEMBER 2014
  5. 114585 605 3809 2 28 OCTOBER 2014 > = JUNE 10, 2014
  6. 108229 605 3809 3 09 - SEP - 2014 > = JUNE 10, 2014
  7. This query returns the correct results:
  8. WITH got_r_num AS
  9. (
  10. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
  11. , ROW_NUMBER () OVER (PARTITION BY supplier_id, supplier_desc_id, batch)
  12. ORDER BY dt_recv DESC
  13. ) AS r_num
  14. T
  15. )
  16. got_dt_cutoff AS
  17. (
  18. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
  19. MIN (CASE
  20. WHEN batch = 1
  21. AND r_num<=>
  22. THEN dt_recv
  23. END
  24. ) ON (PARTITIN BY supplier_id, supplier_desc_id) AS dt_cutoff
  25. OF got_r_num
  26. )
  27. SELECT id, supplier_id, supplier_desc_id, batch, dt_recv
  28. OF got_dt_cutoff
  29. WHERE dt_recv > = dt_cutoff
  30. ORDER BY dt_recv
  31. ;
  32. NOTE: the records are grouped by supplier_id/supplier_desc_id

CASE 2:

  1. truncate table t;
  2. insert into table t
  3. values (45401,801300,4466,0,to_date('21-Aug-2013','DD-mon-YYYY')
  4. /
  5. insert into table t
  6. values (44414,801300,4466,0,to_date('08-Aug-2013','DD-mon-YYYY')
  7. /
  8. commit;
  9. Select * from t;
  10. CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
  11. ---------- ----------- ------------------------ ----------------- ------------
  12. 451 801300 4466 0 21 AUGUST 2013
  13. 44414 801300 4466 0 AUGUST 8, 2013
  14. 801300 4466 0 AUGUST 8, 2013

RULE: when there is no batch = 1, then return all rows

The query above does not work for this case.

CASE 3:

  1. truncate table t;
  2. insert into table t
  3. values (29887,609051,1781,0,to_date('19-Apr-2013','DD-mon-YYYY')
  4. /
  5. insert into table t
  6. values (33623,609051,1781,0,to_date('24-May-2013','DD-mon-YYYY')
  7. /
  8. insert into table t
  9. values (45477,609051,1781,0,to_date('22-Aug-2013','DD-mon-YYYY')
  10. /
  11. insert into table t
  12. values (54013,609051,1781,1,to_date('22-Oct-2013','DD-mon-YYYY')
  13. /
  14. commit;
  15. Select * from t;
  16. CONTENT_ID SUPPLIER_ID SUPPLIER_CONTENT_DESC_ID CONTENT_STATUS_ID RECEIVE_DATE
  17. ---------- ----------- ------------------------ ----------------- -------------
  18. 29887 609051 1781 0 19 APRIL 2013
  19. 33623 609051 1781 0 24 MAY 2013
  20. 45477 609051 1781 0 22 AUGUST 2013
  21. 54013 609051 1781 1 22 OCTOBER 2013

RULE: When there is only to record with batch = 1, return all rows

The query above does not work for this case.

Want to get answers that work, or is it possible to get responses that cause errors?

Make sure that the INSERT statements you post too much work.  Test (and, if necessary, correct) them before posting.  All the instructions insert above have errors.

The query above (once you correct the spelling of PARTITION) returns all the lines after a date limit.   It's always what you want, only the details of how calculated this date limit changed.  In accordance with the new requirements, the closing date must be earlier than the actual lines dt_recv in there are not 2 (or more) with batch = 1 for any combination of (supplier_id, supplier_desc_id).  All you have to do is change "r_num".<= 2"="" to="" "r_num="2" ,"="" when="" computing="" dt_cutoff,="" and="" return="" an="" impossiblly="" early="" date="" if="" there="" is="" no="" such="" row. ="" (i="" assume="" that="" dt_recv="" can="" not="" be="">

WITH got_r_num AS

(

SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

ROW_NUMBER () OVER (PARTITION BY supplier_id

supplier_desc_id

batch

ORDER BY dt_recv DESC

) AS r_num

T

)

got_dt_cutoff AS

(

SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

, NVL ( MIN (CASE)

WHEN batch = 1

AND = 2 r_num - not <=, as="">

THEN dt_recv

END

) OVER (PARTITION BY supplier_id

supplier_desc_id

)

, TO_DATE ('1', 'J') - first DATE in Oracle

( ) AS dt_cutoff

OF got_r_num

)

SELECT id, supplier_id, supplier_desc_id, batch, dt_recv

OF got_dt_cutoff

WHERE dt_recv > = dt_cutoff

ORDER BY supplier_id

supplier_desc_id

dt_recv

;

If dt_recv can be NULL, it is a bit more complicated, but only a little.  Post instructions INSERT (work) and outcomes if you would like to help with this scenario.

Tags: Database

Similar Questions

  • Logical query:

    Hi experts solve it please this sense thanks in advance

    Logical query:


    (1) in real time for these three paintings, there is huge amount of data can be in lakhs.
    (2) for each record in the table for a mobno upgradation there (of five accno or less) to the table in the omni.
    (3) how can I get all those (five account number or less) Omni table with the only mobno that is commen in upgradation tables and omni.
    (4) how columns == logid (log tables and upgradation) and upgradid (tables upgradation and omni).
    (5) I want all columns of all tables


    Please find the beolowsample create and create   scripts :
    
    
    create table log(id number,logid number,name varchar2(10));
    insert into log (id,logid,name) values (1,1231,'ramu');
    insert into log (id,logid,name) values (2,1235,'venu');
    insert into log (id,logid,name) values (3,1236,'sita');
    select * from log;
    
    
    
    
    
    drop table upgradation purge;
    select * from upgradation;
    create table upgradation(logid number,mobno varchar2(10),accno varchar2(10),upgradid varchar2(10));
    =================================================================================================
    
    insert into upgradation (logid,mobno,accno,upgradid) values (1231,'9025934234','2523555','ajknfd');
    
    insert into upgradation (logid,mobno,accno,upgradid) values (1235,'9025936535','2523674','ajknfd');
    
    commit;
    
    drop table omni purge;
    select * from omni;
    create table omni(mobno varchar2(10),accno varchar2(10),upgradid varchar2(10));
    ==============================================================================
    
    insert into omni (mobno,accno,upgradid) values ('9025934234','2523555','ajknfd');
    
    insert into omni (mobno,accno,upgradid) values ('9025934234','3523555','ajknfd');
    
    insert into omni (mobno,accno,upgradid) values ('9025934234','4523555','ajknfd');
    
    insert into omni (mobno,accno,upgradid) values ('9025934234','5523555','ajknfd');
    
    
    required output 
    ===============
    
    accno,mobno,upgradid,logid
    ---------------------------
    9025934234,2523555,ajknfd,1231
    9025934234,3523555,ajknfd,1231
    9025934234,4523555,ajknfd,1231
    9025934234,5523555,ajknfd,1231
    select o.*
    from  omni o, upgradation u, log l
    where l.logid = u.logid
    and u.mobno = o.mobno
    ;
    
  • SQL query - help with join

    Dear friends,

    Version of DB - 11.1.0.7... , I'm stuck with SQL basics today... need your help...

    The slot SQL tells me "cache them locks library" in the database that I will put up as a proactive measure.

    I'll be it works via shell script and include the table gv instance_name $ instance ... I'm a little confused as to how a 3rd table "gv$ instance ' can be introduced into the query in order to make the instance_name in the result set...

    SELECT * FROM)

    SELECT / * + LEADING (a) USE_HASH (u) * /.

    instance_name, INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 2, 'INSERT') as "order."

    Event, mod(P1,16) p1, p2, p3

    COUNT (*) totalseconds

    , SUM (CASE WHEN wait_class = 'Application' THEN 1 ELSE 0 END) 'Application '.

    Of

    (SELECT

    a.*

    , TO_CHAR (CASE WHEN session_state = 'WAITING' THEN ELSE null END p1, '0XXXXXXXXXXXXXXX') p1hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN p2 ELSE null END, '0XXXXXXXXXXXXXXX') p2hex

    , TO_CHAR (CASE WHEN session_state = "PENDING" THEN ELSE null END p3, '0XXXXXXXXXXXXXXX') p3hex

    SGS $ active_session_history one) a

    u dba_users

    WHERE

    a.user_id = u.user_id

    AND sample_time BETWEEN sysdate-90 /(24*60) AND sysdate

    - AND a test of ('library cache lock', 'library cache pin")

    AND event like '% library '.

    GROUP BY

    INST_ID select, blocking_inst_id, blocking_session, username, session_id, sql_id, current_obj #,.

    DECODE (sql_opcode, 1, 'CREATE TABLE', 'INSERT', 2),

    event, mod (p1, 16), p2, p3

    Having count (*) > 5

    ORDER BY

    TotalSeconds DESC

    , INST_ID select, blocking_session, username, session_id, sql_id, current_obj #, 'Order', event

    )

    WHERE

    ROWNUM < = 20

    /

    replace

    instance_name

    by

    (select instance_name gv$ instance where INST_ID select = a.inst_id) instance_name

    or select... in... a, u, gv$ instance where... and gv$ instance.inst_id (+) = a.inst_id...

  • Pivot query help

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                     3

    COMP2                    ABC                     5

    COMP1                    CAD                     3

    COMP3                    CAD                     5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                     ABC                     3

    -                     CAD                     2

    -                     TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.


    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    Hello

    Subhash C-Oracle wrote:

    need help on creating pivot query

    SELECT * FROM TEST1

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

    VALUE OF PERSON COMPUTERNAME

    COMP1                    ABC                    3

    COMP2                    ABC                    5

    COMP1                    CAD                    3

    COMP3                    CAD                    5

    COMP2                    TES                      1

    COMP1                    TES                      5

    COMP3                    ABC                      2

    myQuery

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

    Select the link null, label, value1 COUNT (VALUE)

    from 'test1 '.

    CONTROL group PER PERSON

    Results

    ---------

    Link label value1

    -                    ABC                    3

    -                    CAD                    2

    -                    TES                      2

    My requirement

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

    can we have something like that out using the concept of pivot? If so can you share an example query pls.

    Link label value1

    -ABC ORDI1, COMP2, COMP3

    -CAD COMP1, COMP2

    -YOUR ORDI1, COMP3

    This sounds like a job for LISTAGG:

    SELECT NULL AS link

    label

    LISTAGG (comp_name, ',')

    THE Group (ORDER BY ComputerName) AS value1

    OF test1

    GROUP BY label

    ;

    If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.

    Are you sure that the results you posted are what you want from data provided?

    Is of the order of the elements in a significant list?  In other words, when you say you want to get the results:

    COMP1, COMP2

    you'd be just as happy with

    ORDI1, COMP2

    ?  If the order is important, explains what this order.

  • SQL Query Help (not working not properly)

    Hello everyone,

    I use JDeveloper 12.1.2.0.0. I do a two-way communication using 3 tables, with links between them (and using schema HR).

    In my example, I have something like:

    Departments, employees, and the SalaryByJobs (I created this table where it shows a departmentd id, employee id, salary).

    Whenever I click on one OR more departments, the employees up-to-date table by putting on the table, employees who belong to the selected department and the salaryByjob to put the jobs of the employees selected on the employees table.

    So it's something like this:

    The departments selected-> employees selected-> salarybyjbobs of these employees.

    My query used (in the view) has the following code:

    SELECT Employees.COMMISSION_PCT,

    Employees.DEPARTMENT_ID,

    Employees.EMAIL,

    Employees.EMPLOYEE_ID,

    Employees.FIRST_NAME,

    Employees.HIRE_DATE,

    Employees.JOB_ID,

    Employees.LAST_NAME,

    Employees.MANAGER_ID,

    Employees.PHONE_NUMBER,

    Employees.SALARY

    Employees EMPLOYEES

    WHERE (department_id IN (select * from THE (select cast (in_list(:variavel3) as mytableType) double) a))

    Since I use the links, the employees table does not show anything at the beginning, so I added this to my query used to go: OR nvl(:variavel3,0) = 0

    But now, whenever I try to select multiple lines, it gives me the invalid numbers and I don't understand why...

    It's only one line of code and it is not in the bean.

    Can someone help me?

    PS - The bean will Department by Department, adds the departments with ',' and for each Department, gets employees who belongs to them.

    My best regards,

    Frederico Barracha.

    The expression NVL (: variavel3, 0) = 0 is not correct. The data type of the return value of the NVL function is considered to be equal to the data type of the argument of 1 (that is, the data type of the variable binding: variavel3). You said that this variable contained a list separated by commas to ID, so the data type of the variable is VARCHAR2. As long as you compare the NVL expression of a number, you get 'Invalid number' exception, because Oracle expects a numeric data type on the left side of the comparison operator.

    To avoid the exception "Invalid number", you can modify the expression by using one of the following options:

    : variavel3 IS NULL

    NVL (: variavel3, ' *') = ' *'

    NVL (: variavel3, ' 0') = '0'

    Option 1, so the simplest and clearest.

    Dimitar

  • simple query help :)

    I have table with date, customer number, salary... Now, I want to extract the total number of client whose salary is between 2000 and 3000 and deposited consistantly for the last 6 months.
    Can someone help me with this...

    user12183668 wrote:
    I used your query... its working well... but there are a few County for the date (NTC) which I am less than 6... .and I want to exclude these documents at the level of the query.

    How can I do this

    By encapsulating the query as an interior view, something in this way:

    select *
      from (
    select
     empno
    ,count(distinct trunc(hiredate,'MM')) cnt
    from emp
    where
    sal between 2000 and 3000
    and
    hiredate >= add_months (trunc(sysdate, 'MM'), -5 )
    group by empno
    ) a
    where a.cnt = 6;
    
  • Grouping Query Help

    I need help in writing a query. The data that I now contains three columns. The values in the first column have duplicate, and the 2nd column entries. The third is unique. Here is an example of the data that I have today:

    Column1 Column2 Column3
    Tier1 Group1 1
    Tier1 Group1 2
    Group level 1 2 3
    Tier1 Group2 4
    Group level 1 3 5
    Level 1 Group 3 6


    Expected result:

    Column1 Column2 Column3
    Tier1 Group1 1
    2
    2 3 Group
    4
    3 5 group
    6

    Thanks for your help

    Hello

    Your front end that can probably not for you.
    In SQL * Plus, for example:

    BREAK  ON column1    ON column2
    
    SELECT    column1, column2, column3
    FROM      table_x
    ORDER BY  column3
    ;
    

    If you havd do in SQL, here's one way:

    SELECT       CASE
             WHEN ROW_NUMBER () OVER ( PARTITION BY  column1
                              ORDER BY         column3
                            ) = 1
             THEN  column1
    
           END     AS col1
    ,       CASE
             WHEN ROW_NUMBER () OVER ( PARTITION BY  column2
                              ORDER BY         column3
                            ) = 1
             THEN  column1
    ,       END     AS col2
    ,       column3
    FROM       table_x
    ORDER BY  column3
    ;
    

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Highlight a few places where the queries above are getting incorrect results and explain, using specific examples, how you get the results of the data provided in these places.
    Always tell what version of Oracle you are using.

    Published by: Frank Kulash, March 21, 2012 16:15

  • calculate the value of the query help

    Hi all
    Following the script nicely:
    CREATE TABLE ACCOUNT_LOOCKUP
    (
      SERIAL_ID     NUMBER,
      ACCOUNT_ID    NUMBER,
      ACCOUNT_RATE  NUMBER,
      ACCOUNT_MAX   NUMBER
    )
    
    ALTER TABLE ACCOUNT_LOOCKUP ADD (
      CONSTRAINT PK_ACCOUNT_LOOCKUP
     PRIMARY KEY
     (SERIAL_ID, ACCOUNT_ID));
    
    
    
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (1, 1, 10, 200);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (2, 1, 12, 150);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (3, 1, 8, 400);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (1, 2, 7, 100);
    Insert into ACCOUNT_LOOCKUP
       (SERIAL_ID, ACCOUNT_ID, ACCOUNT_RATE, ACCOUNT_MAX)
     Values
       (2, 2, 5, 200);
    COMMIT;
    
    
    SELECT * FROM ACCOUNT_LOOCKUP
    
     SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX
    ---------- ---------- ------------ -----------
             1          1           10         200
             2          1           12         150
             3          1            8         400
             1          2            7         100
             2          2            5         200
             
             
    CREATE TABLE ACCOUNT_AMOUNT(
    ACCOUNT_ID NUMBER(10),
    ACCOUNT_AMNT NUMBER(10))
    
    
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (1, 9);
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (1, 35);
    COMMIT;
     
    SELECT * FROM ACCOUNT_AMOUNT
     
    ACCOUNT_ID ACCOUNT_AMNT
    ---------- ---------------
             1               9
             1              35
           
    I want by select query calculte ACCOUNT_TOTAL every ACCOUNT_ID exists in table ACCOUNT_AMOUNT as below ,
    
    1. every ACCOUNT_ID have SERIAL_ID and ACCOUNT_RATE and ACCOUNT_MAX in table ACCOUNT_LOOCKUP,
    2. to calculte ACCOUNT_TOTAL every ACCOUNT_ID : 
       
       - order ascending SERIAL_ID exists in table ACCOUNT_LOOCKUP to every ACCOUNT_ID exists in table ACCOUNT_AMOUNT ,
       - make sum ACCOUNT_AMNT every ACCOUNT_ID in table ACCOUNT_AMOUNT,
       - then, copmare result (sum ACCOUNT_AMNT) with first record after ascending SERIAL_ID,
       - product (sum ACCOUNT_AMNT) * ACCOUNT_RATE  result as ACCOUNT_TOTAL,(in ex: sum ACCOUNT_AMNT  = 44   ) ,
       - if ACCOUNT_TOTAL > ACCOUNT_MAX then 
             * ACCOUNT_TOTAL = ACCOUNT_MAX for first record(in ex SERIAL_ID = 1 ) ,
             * Goto the second record by ascending (in ex SERIAL_ID = 2 ) ,
               make ( ACCOUNT_TOTAL / ACCOUNT_RATE ) for first record ,
               ( 200 / 10 ) result 20 >>
               
             * calculate Remainder ACCOUNT_AMNT =  the sum ACCOUNT_AMNT 44 - 20 = 24 
             
                *Goto the second record by ascending (in ex SERIAL_ID = 2 ) ,   
                   Remainder ACCOUNT_AMNT 24 * (12) ACCOUNT_RATE for second record = 288 as ACCOUNT_TOTAL 
                   -if ACCOUNT_TOTAL > ACCOUNT_MAX then 
                   * ACCOUNT_TOTAL = ACCOUNT_MAX for second record(in ex SERIAL_ID = 2 ) ,
                      * Goto the third record by ascending (in ex SERIAL_ID = 3 ) ,
                      make ( ACCOUNT_TOTAL / ACCOUNT_RATE ) for second record ,
                       ( 150 / 12 ) result 12.5
                   
                        * calculate Remainder ACCOUNT_AMNT =  the sum ACCOUNT_AMNT 24 - 12.5 = 11.5 
                        Remainder ACCOUNT_AMNT 9.5 * (12) ACCOUNT_RATE for third record = 92 result as ACCOUNT_TOTAL 
                        if result <= ACCOUNT_MAX then 
                            ACCOUNT_TOTAL = 92 
    except result
    ------------
    
    SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX   ACCOUNT_TOTAL          ** explain ** 
    ---------- ---------- ------------ -----------  -------------  *****  sum ACCOUNT_AMNT  = 44 for ACCOUNT_ID = 1from table ACCOUNT_AMOUNT ******
             1          1           10         200          200  >> (44 * 10 ) = 440 >>  200 /10 rate = 20 >> 44 - 20 = 24 Remainder ACCOUNT_AMNT
             2          1           12         150          150  >> (22 * 12 ) = 288 >>  150 /12 rate = 12.5 >> 24 - 12.5 = 11.5 Remainder ACCOUNT_AMNT
             3          1            8         400           92  >> (11.5 * 8)  = 92  And so on ....
        
    another insert 
    Insert into ACCOUNT_AMOUNT
       (ACCOUNT_ID, ACCOUNT_AMNT)
     Values
       (2, 10);         
       
       
    SELECT * FROM ACCOUNT_AMOUNT
     
    ACCOUNT_ID ACCOUNT_AMNT
    ---------- ---------------
             1               9
             1              35
             2              10    
             
    
    except result
    ------------
      
    
    SERIAL_ID ACCOUNT_ID ACCOUNT_RATE ACCOUNT_MAX   ACCOUNT_TOTAL          ** explain ** 
    ---------- ---------- ------------ -----------  -------------  *****  sum ACCOUNT_AMNT  = 10 for ACCOUNT_ID = 2 from table ACCOUNT_AMOUNT ******
             1          1           10         200          200  
             2          1           12         150          150  
             3          1            8         400           92           
             1          2            7         100           70    10 * 7 = 70 
    Help me please
    Thanks in advance

    Published by: 900510 on December 5, 2011 08:05

    900510 wrote:
    Hi all

    First I want to apologize for my English, this isn't flunet.

    Following the script nicely:

    CREATE OR REPLACE VIEW V_ACCOUNT (ACCOUNT_ID,ID,ACCOUNT_RATE,ACCOUNT_MAX,ACCOUNT_TOTAL)
    AS
    SELECT  1, 2, 10 ,200 , 0 FROM DUAL
    UNION
    SELECT  1, 5, 12 ,150 , 0 FROM DUAL
    UNION
    SELECT  1, 9, 8  ,400 , 0 FROM DUAL
    UNION
    SELECT  2 ,1, 7  ,100 , 0 FROM DUAL
    UNION
    SELECT  2 ,3 ,5  ,200 , 0 FROM DUAL
    ORDER BY 1,2
    
    ACCOUNT_ID         ID ACCOUNT_RATE ACCOUNT_MAX ACCOUNT_TOTAL
    ---------- ---------- ------------ ----------- -------------
    1          2           10         200             0
    1          5           12         150             0
    1          9            8         400             0
    2          1            7         100             0
    2          3            5         200             0
    

    I must be missing something... in your opinion, by definition does only selected literals 5 double. How you expect what you do for any other table to change those returned by the view?

    Published by: EdStevens on December 5, 2011 08:51

  • join query help

    Hello everyone, I kindly need help with a query that I'm writing. I think it's supposed to be some kind of join, but I'm a little uncertain. Here is an example:

    Select a.person_id, a.company, b.name, e.element, f.value
    of a, b, e, f
    where a.person_id = b.person_id
    and e.el_id = f.el_id
    -e.t.c

    Lets say this returns

    person_id, company, name, element value
    ------------------------------------------------------
    1 vol., krog, breakfast, 34
    2, mols, flog, munch, 24

    The problem is now the table e. I want to get all the e table values that meet certain criteria. As in:

    Select e.element
    where e.name = "RATED."

    Lets say this returns

    element
    -----------
    food
    lunch
    Munch

    And combine it with the query at the top of the page. But I also want to show all the other values, a.person_id, a.company, b.name for each line.
    So my goal is to finally have:

    person_id, company, name, element value
    ------------------------------------------------------
    1 vol., krog, breakfast, 34
    1 vol., krog, food, 0
    1 vol., krog, munch, 0
    2, mols, flog, munch, 24
    2, mols, flog, food, 0
    2, mols, flog, 0

    It's to have a default value of zero, where no join does exist for the value and do not duplicate anything even if I could always use separate.
    Can anyone help with this?
    with t1 as (
                select a.person_id, a.company, b.name, e.element, f.value
                  from a, b, e, f
                  where a.person_id = b.person_id
                  and e.el_id = f.el_id
                  -- e.t.c
               ),
         t2 as (
                select e.element
                 where e.name = 'EVALUE'
               )
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
      order by person_id,
               company,
               name,
               t2.element
    /
    

    For example:

    with t1 as (
                select 1 person_id, 'Vols' company, 'krog' name, 'lunch' element, 34 value from dual union all
                select 2, 'Mols', 'flog', 'munch', 24 from dual
               ),
         t2 as (
                select 'food' element from dual union all
                select 'lunch' from dual union all
                select 'munch' from dual
               )
    select  person_id,
            company,
            name,
            t2.element,
            sum(
                case t1.element
                  when t2.element then value
                  else 0
                end
               ) value
      from  t1,
            t2
      group by person_id,
               company,
               name,
               t2.element
      order by person_id,
               company,
               name,
               t2.element
    /
    
     PERSON_ID COMP NAME ELEME      VALUE
    ---------- ---- ---- ----- ----------
             1 Vols krog food           0
             1 Vols krog lunch         34
             1 Vols krog munch          0
             2 Mols flog food           0
             2 Mols flog lunch          0
             2 Mols flog munch         24
    
    6 rows selected.
    
    SQL> 
    

    SY.

  • The join SQL query help

    I'm just having a bit of troubel get a correct join query - I thought it was an Inner Join, but I don't get the results I expect.

    My table structure is:

    Table: lodges

    LodgeID (PK)

    Lodge

    etc.

    Table: implemented application

    NominationID (PK)

    Category

    LodgeID

    Year

    So I try to use this structure to replicate this page:

    http://www.safariawards.com/nominees12/

    That is to say a list of boxes for each category, they are appointed on.

    The query I've tried looks like this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. NominationID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

    But this product:

    http://www.safariawards.com/nominees12/southernafrica.php

    Its the right number of results, but not the list on the right of the boxes - for example British Airwways is not LodgeID 786

    If anyone could help with the SQL right for what would be well appreciated.

    That you join on the wrong column. Try this:

    SELECT appointments. LodgeID, lodges. Lodge, applications. NominationID, applications. Lodges INNER JOIN applications category IT lodges. LodgeID = nominated. LodgeID WHERE category = "Best property of Safari in southern Africa" ORDER BY Lodge

  • Decode the query help

    Hello
    I'm new to the development of Oracle.
    Oracle 10 g 2

    My original query:

    SELECT APP, count (*)
    TRANSACTION
    WHERE TYPE in ('ShipmentConfirmPR', 'ShipmentConfirm', 'ShipConfirm')
    and the APP ("SAPPI", "SAPPI", "SAPR3") and INTERVENE ('9320 ', '9332','1208 ')
    GROUP BY APP TYPE
    order of the APP

    the result of this query:
    SAPPI 100
    SAPPI 600
    SAPR3 440

    My requirement
    And I want to have something like output

    LDCS 100
    TSW 600
    PDC 440

    IE.the APPP and STEP combinations. Must return the specified values
    SAPPI & 9320-> LOC (here SAPPI IE APP is the same for both... but it's a coincidence IE APP can be sliced also)
    SAPPI & 9332-> tsw
    SAPR3 & 1208-> pdc

    Options, I tried:
    Query provided by one of the Forum members...
    SELECT THE CHECK BOX
    WHEN APP = "SAPP1" THEN DECODE (step, '9320', 'LSW', '9332', "TSW")
    WHEN APP = "SAPR3" step = '1208' AND 'PDC '.
    END app
    COUNT (*)
    TRANSACTION
    WHERE TYPE in ('ShipmentConfirmPR', 'ShipmentConfirm', 'ShipConfirm')
    AND THE APP ("SAPPI", "SAPPI", "SAPR3")
    AND STEP IN ('9320', '9332', ' 1208')
    GROUP BY APP, STEP
    ORDER OF THE APP.

    EXECUTION PLAN

    | ID | Operation | Name |
    ------------------------------------------------------------------------
    | 0 | SELECT STATEMENT |
    | 1. GROUP SORT BY |
    | 2. INLIST ITERATOR.
    | 3. TABLE ACCESS BY INDEX ROWID | TRANSACTION |
    | 4. INDEX RANGE SCAN | TRANSACTION_IDX |


    The output of the query (as above) must partially match the following query (a particular combination of CLO)

    SELECT count (1)
    TIBCO. TRANSACTION_HISTORY
    WHERE TYPE = 'ShipmentConfirm '.
    and APP in ("SAPPI") and INTERVENE ('9332')


    My Questions:

    1.*There are indexes on all 3 APP passes it's IE, STEP and TYPE *. I don't want a FULL table Scan (as one would use the index). Can change us the query / use of indices, etc. to make it faster?

    2. is the right to approach? Would the use of the concat operator in the function decode work better for my needs?
    Something like

    Select decode (APP |) STEP, 'SAPP9332', 'X') of TRANSACTION_HISTORY where < COND >

    If Yes can you please provide the query?

    3. ANY other approach / request for my requirement.

    Thanks in advance.

    Hello

    user13517642 wrote:
    ... EXECUTION PLAN

    | ID | Operation | Name |
    ------------------------------------------------------------------------
    | 0 | SELECT STATEMENT |
    | 1. GROUP SORT BY |
    | 2. INLIST ITERATOR.
    | 3. TABLE ACCESS BY INDEX ROWID | TRANSACTION |
    | 4. INDEX RANGE SCAN | TRANSACTION_IDX |

    The output of the query (as above) must partially match the following query (a particular combination of CLO)

    SELECT count (1)
    TIBCO. TRANSACTION_HISTORY
    WHERE TYPE = 'ShipmentConfirm '.
    and APP in ("SAPPI") and INTERVENE ('9332')

    My Questions:

    1.*There are indexes on all 3 APP passes it's IE, STEP and TYPE *. I don't want a FULL table Scan (as one would use the index). Can change us the query / use of indices, etc. to make it faster?

    A full table scan might be the fastest way to get results. Do you have any reason to think that it would be faster to go through the index? How selective are the clues? In other words, what is the percentage of rows in the table correspond to each of the values in the WHERE clause?

    2. is the right to approach?

    It depends on what you're trying to do, which is not at all clear to me.

    Would the use of the concat operator in the function decode work better for my needs?
    Something like

    Select decode (APP |) STEP, 'SAPP9332', 'X') of TRANSACTION_HISTORY where

    If you use this approach, look out for the problem Ab asse crevit . For example, if you have these 4 rows and 2 columns:

    str1     str2
    ----     ----
    (NULL)     FOO
    F     OO
    FO     O
    FOO     (NULL)
    

    There are 4 values of distict of str1 (counting NULL) and 4 separate values of str2, str1 but | str2 is the same for each of them. In the above example, it there is no way to know, just by looking at the concatenated string, including str1 and str2 ends begins. Maybe it's not the case for your specific data (for example, if the application is still exactly 5 characters long). otherwise, you may need to add some kind of delimiter, like this

    app || '+' || step
    

    where you know that '+' never occurs in one of these columns.

    3. ANY other approach / request for my requirement.

    CASES, as I mentioned in your previous message:
    Decode the help function
    and as you have used above.

    In this thread, you said "I have to use the decode function. Why? It is a competition of school Etudieeo DECODE is explicitly required? Why you don't want in the best way, what that turns out to be?

    Your WHERE clause:

    AND APP IN ('SAPPI', 'SAPPI', 'SAPR3')
    AND STEP IN ('9320', '9332', '1208')
    

    admits 6 possible combinations of APA and step:

    app     step
    -----     ----
    SAPP1     9320
    SAPP1     9332
    SAPP1     1208
    SAPP3     9320
    SAPP3     9332
    SAPP3     1208
    

    but you are looking for only 3 of these combinations in DECODE it or the expression BOX. (Have 2 copies of 'SAPP1' e list won't do any good, but it does hurt real, either.)
    By the way, is "SAPPI" app with the letter 'I' at the end, or "SAPP1", with the number '1' at the end?

    Published by: Frank Kulash, March 24, 2011 19:44

  • Group Query Help

    Oracle 10G version | R/2

    This is a Customers table and we will have to find they are elgible for product produced B or both or none.

    But if you're serviceble by product B, it is certain that you will be good product a. (imagine the product A is greater coverage of Prouct and be produced is the smallest)

    What I have done becomes the product of A B and get the remains that are usable for the product and not product B.

    < pre >
    CREATE TABLE CUST_TEST
    (
    Number of cust_id,
    Column_serviceable_A varchar2 (2),
    Column_Serviceable_B varchar2 (2),
    Column_Color_A varchar2 (2),
    Column_Color_B varchar2 (2),
    Area_OO varchar2 (2),
    Area_H varchar2 (2),
    Varchar2 (10) STATE
    );

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(1,'Y','Y','G','G','Y','Y','HYD');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(2,'Y','Y','G','G','Y','Y','HYD');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(3,'Y',,'G',,'Y','Y','HYD');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(4,'Y',,'R',,'Y','Y','BANG');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(5,,,,,'Y','Y','BANG');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(6,'Y','Y','G','G','Y','Y','CHEN');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(7,'Y',,'G',,'Y','Y','CHEN');

    insert into cust_test (Cust_ID, Column_serviceable_A, Column_Serviceable_B, Column_Color_A, Column_Color_B, Area_OO, Area_H, STATE)
    values(8,'Y',,'G',,'Y','Y','CHEN');
    < / pre >

    < pre >
    Select
    (
    (
    Select sum (case when area_oo = 'Y' or area_h = 'Y' then 1 else 0 end) of CUST_TEST one
    where column_serviceable_A = 'Y '.
    and Column_Color_A ('g', 'A')
    and status = 'HYD.
    )
    -
    (
    Select sum (case when area_oo = 'Y' or area_h = 'Y' then 1 else 0 end) of CUST_TEST one
    where Column_Serviceable_B = 'Y '.
    and Column_Color_B ('g', 'A')
    and status = 'HYD.
    )
    )
    Double;
    < / pre >


    But I need this 15 times as there are 15 States, I want to do in a single request via the Group of. CUST_TESTis also a view and each query takes once NGOs as customers there 15 million.

    Please let me know if you need more information.

    Desirable output;

    COUNT (*) STATE
    HYD 1
    BANG null
    CHEN 2

    Thank you and best regards,
    Cracky

    Published by: CrackerJack 22 November 2010 16:51

    Hello

    Thanks for posting the sample data in a useful form. That really helps.

    So you want a query that produces a line of output for each value in the column state.
    That's exactly what "State GROUP BY" don't:

    SELECT       state
    ,       COUNT ( CASE
                      WHEN  column_serviceable_a  = 'Y'
                    AND   column_color_a         IN ('G', 'A')
                    THEN  1
                  END
                )
         - COUNT ( CASE
                      WHEN  column_serviceable_b  = 'Y'
                    AND   column_color_b         IN ('G', 'A')
                    THEN  1
                  END
                )     AS cnt
    FROM       cust_test
    WHERE       area_oo     = 'Y'
    AND       area_h     = 'Y'
    GROUP BY  state
    ORDER BY  state
    ;
    

    In fact, this becomes 0 as the county when no line meets the criteria of "has".
    If you prefer that NULL, as you posted, then replace the first indictment by AMOUNT:

    SELECT       state
    ,       SUM   ( CASE
                      WHEN  column_serviceable_a  = 'Y'
                    AND   column_color_a         IN ('G', 'A')
                    THEN  1
                  END
                )
         - COUNT ( CASE
                      WHEN  column_serviceable_b  = 'Y'
                    AND   column_color_b         IN ('G', 'A')
                    THEN  1
                  END
                )     AS cnt
    FROM       cust_test
    WHERE       area_oo     = 'Y'
    AND       area_h     = 'Y'
    GROUP BY  state
    ORDER BY  state
    ;
    

    Output:

    STATE             CNT
    ---------- ----------
    BANG
    CHEN                2
    HYD                 1
    

    Note that we do not need duplicate in this problem. Whenever you are tempted to use double, ask yourself why. See if the information you want to be taken from another table that you use when even.

    This should be much faster than the original request. Instead of making 2 passes through the table for each status value (2 * 15 = 30 past total), just make a pass through the table.

    Published by: Frank Kulash, 22 November 2010 20:14
    Simplified 2nd query (for number of NULL).

  • mySQL Query Help

    Is there an easy way to find all records where he is an ID in a list? For example if I have the following:

    TABLE:
    resources

    FIELDS:
    resourceID. listOfIDs | title

    RECORDS:
    1. 1,7,9 | Point 1
    2. 2,3,7,8 | Point 2
    5: 8.17. Point 3
    4. 2.7 | Point 4

    I want to query the table for all records where listOfIDs contains 7. That would be 1, 2, and 3. OR I can want to find where listOfIDs contains 2 AND 8. That would be 2, 3, and 4.

    Normally I use another table and have each of the elements of the list as a record that links to the resource ID, but I have 6 columns of type different 'listOfIDs', so I just wanted to know if there is a better way.

    Thanks for any help.

    dbldutch wrote:

    > Normally I use another table and have each of the elements of the list as a
    > record that links to the resource ID, but I'll have 6 different
    > 'listOfIDs' type columns so I just wanted to know if there is a better way.
    >

    The second related table * IS * the best way. That is why all
    Ph.d. thesis of database are written on the standardization of data. Trust
    them, six related tables will be much easier then six columns each
    containing a list of numbers.

    Why, because a list of numbers is not a list of numbers in a database
    It is a string. And the only way to search is inefficient string
    functions. Your where clause will have to consider something like this:

    ' WHERE listOfIDs LIKE ' 7, % ' OR listOfIDs LIKE '%, 7% ' OR listOfIDs like '%, 7' '.

    And that is just to search for a possible value in the list, that
    progresses of exponential complexity with each additional number
    that research.

  • A hierarchical query help

    I have two tables:

    The FOLDERS where the areas concerned are: folderId, parentFolderId, folderQuota
    and DOCUMENTS where the areas concerned are: folderId, size

    FILES is hierarchical - parentFolderId of the file of the child has the folderId of the parent value
    folderQuota the value null (undefined quota)


    Now, I need to run a query with the following recursive logic

    < i > function calcQuota (folderIdpar, isFirstpar) {}

    If (not isFirstpar) and (folderQuota of the folder with folderIdpar is not null) return folderQuota;
    Returns the size of all documents where folderId = folderIdpar + calcQuota (folderId of all children, false);

    } < /i >

    (I hope the pseudo-code is understandable - the query is executed as < i > calcQuota (originalFolderId, true) < /i >).

    Now, my question is if I can accomplish this with a single hierarchical query, or if I should implement as a recursive procedure stored.

    Thank you!

    P.S. I use Oracle XE (10g)

    Hello

    If you want to ignore the quotas at the level = 1:

    WITH     tree     AS
    (
         SELECT     folder_id
         ,     CASE
                   WHEN  LEVEL > 1
                   THEN  quota
              END                         AS effective_quota
         FROM     folders          f
         START WITH     folder_id          = :start_folder_id
         CONNECT BY     parent_folder_id     = PRIOR folder_id
              AND     (   PRIOR quota          IS NULL
                   OR  LEVEL          = 2
                   )
    )
    SELECT     :start_folder_id
    ,     SUM ( NVL ( t.effective_quota
                , d.document_size
                )
             )          AS total_size
    FROM     tree               t
    LEFT OUTER JOIN documents     d     ON     t.folder_id          = d.folder_id
                             AND     t.effective_quota     IS NULL
    ;
    
  • The ROWNUM query help

    SELECT * FROM (SELECT A.SSN, C.CLAIM_SEQ, A.FIRST, A.MID, A.LAST, C.CLAIM_DT, C.INSERT_DT, C.CLAIM_STAT,
    RN C.ISSUE_CDE, B.UCX_UCFE, B.TRAPOSS, B.DUAPOSS, C.AGENT_ID, C.CONFNUM, ROWNUM
    THE APPLICANT HAS, CLMTELIG B, UICLAIMS C
    WHERE A.CLMT_SEQ = B.CLMT_SEQ AND B.CLMT_SEQ = C.CLMT_SEQ) HAS
    WHERE A.RN > = 100 AND A.RN < = 200

    If you need high-end of for example: 100 records I get them...

    But when I add the date column insert in this query to get the same range

    SELECT * FROM (SELECT A.SSN, C.CLAIM_SEQ, A.FIRST, A.MID, A.LAST, C.CLAIM_DT, C.INSERT_DT, C.CLAIM_STAT,
    RN C.ISSUE_CDE, B.UCX_UCFE, B.TRAPOSS, B.DUAPOSS, C.AGENT_ID, C.CONFNUM, ROWNUM
    THE APPLICANT HAS, CLMTELIG B, UICLAIMS C
    WHERE A.CLMT_SEQ = B.CLMT_SEQ AND B.CLMT_SEQ = C.CLMT_SEQ) HAS
    WHEN TRUNC (INSERT_DT) BETWEEN ' 01-AUG-09' AND '' 02-SEP-09
    AND A.RN > = 100 AND A.RN < = 200

    This 100 and 200 are actually the bind variable: p_range1 and: p_range2
    rownum > =: p_range1 AND rownum < =: p_range2

    The user can enter any beach, as he has 300 to 500 should get rownum between 300 and 500 records.

    Any help is appreciated...

    Thanks in advance...

    Hello

    Use the analytical ROW_NUMBER function instead of ROWNUM.

    For example:

    SELECT  *
    FROM     (   SELECT  A.SSN, C.CLAIM_SEQ, A.FIRST, A.MID, A.LAST
             ,         C.CLAIM_DT, C.INSERT_DT, C.CLAIM_STAT
             ,         C.ISSUE_CDE, B.UCX_UCFE, B.TRAPOSS, B.DUAPOSS, C.AGENT_ID
             ,         C.CONFNUM
             ,         ROW_NUMBER () OVER (ORDER BY ...)     -- Use whatever column or columns you want
                                         AS rn
             FROM    CLAIMANT   A
             ,         CLMTELIG   B
             ,         UICLAIMS   C
             WHERE   A.CLMT_SEQ = B.CLMT_SEQ
             AND         B.CLMT_SEQ = C.CLMT_SEQ
         ) A
    WHERE      TRUNC (INSERT_DT) BETWEEN '01-SEP-09'
                           AND       '02-SEP-09'
    AND     A.RN             BETWEEN  :p_range1
                     AND        :p_range2
    ;
    

    In the ORDER BY clause Analytics, put expressions that determine the order. You can use ASC or DESC after each expression as an ORDER BY in the query clause.
    In the query that you have posted, rn has been affected in no particulare order. If that's what you really want, use a constant in analytics ORDER BY clause, like this:

    ...         ,         ROW_NUMBER () OVER (ORDER BY  0)
    

    You must use an ORDER byclause with ROW_NUMBER.

    If you really want to use ROWNUM, assign ROWNUM in a subquery (give it an alias like r_num), then to a query of great, have the State "WHERE the r_num BETWEEN: p_range1 AND: p_range2.

    Note that you query can produce rewer 101 lines, before even the last page. In fact, it can produce no line. The condition in the main query "WHERE TRUNC (INSERT_DT) BETWEEN" is applied after that rn is assigned, so, although the 101 lines meet the requirement on the rn, they can all be excluded by the condition on insert_date. Move condition on insert_date in the subquery if you want to change this.

    Moreover, it is a Request of Pagination .

    Published by: Frank Kulash, 21 May 2010 14:39
    Added example.

Maybe you are looking for