Help on sql query rewriting

Thank you

Hello

Whenever you have a problem, please post a small example data (CREATE TABLE and only relevant columns, INSERT statements) of all the tables involved, so that people who want to help you can recreate the problem and test their ideas.

Also post the exact results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.

Always say what version of Oracle you are using (for example, 11.2.0.2.0).

See the FAQ forum: Re: 2. How can I ask a question on the forums?

Something not clear here:

... WHEN date_dim.date_key - CASE< 20150101="">

If date_key is a DATE, so don't try to compare it to a NUMBER.

If date_key isn't a DATE, then you have an even more serious problem.

Tags: Database

Similar Questions

  • need help with sql query dates

    Hello

    I have a sql query I need to extract some info between data dates. Where clause in this query is:

    WHERE CPD_BUS_UNIT =: ESI_PRM_1
    AND CPD_VOUCHER_DATE > =: P_DATE_FROM
    AND CPD_VOUCHER_DATE < (: P_DATE_TO + 1).

    When I run the query into a toad, I can view the data, but not the execution plan. It gives an error ORA-00932 inconsistent Datatypes.
    But when I remove (+ 1): P_DATE_TO, I can c the execution plan and data. The data will be different from the previous.

    Please suggest how to rewrite the query.

    Can you please give it a try?

    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :to_date(P_DATE_FROM)
    AND CPD_VOUCHER_DATE < (to_date(:P_DATE_TO)+1) 
    

    Concerning

  • Need help with sql query

    Dear all,

    I have a sql like query below

    SELECT min (G.TRANSACTION_DATE), D.REQUEST_NUMBER

    MTL_TXN_REQUEST_HEADERS D,.

    MTL_TXN_REQUEST_LINES, E.

    MTL_MATERIAL_TRANSACTIONS G,.

    Mtl_Transaction_Types I have

    WHERE D.HEADER_ID = E.HEADER_ID

    AND G.TRANSACTION_TYPE_ID = I.TRANSACTION_TYPE_ID

    AND Upper (I.Transaction_Type_Name) = Upper ('TEC outcome')

    AND E.LINE_ID = G.MOVE_ORDER_LINE_ID

    AND D.MOVE_ORDER_TYPE = 5

    TO_DATE (G.TRANSACTION_DATE) BETWEEN TO_DATE('01-JAN-2014') AND TO_DATE('31-DEC-2014')

    D.REQUEST_NUMBER GROUP

    I need to get the first number of the application and finally ask for number based on the date of the transaction, how can I get the number of name application and based on the date of the transaction for the same query, please help me.

    above query is back under results

    results.jpg

    If the query should return a line with the number of application like 2383 based on minimum transaction date and another column based on the maximum transaction date, please help me.

    so in the example above, it must return

    FIRST REQUEST NUMBER LAST NUMBER

    2383                                      1886

    Thank you

    select min(request_number) keep(dense_rank first order by transaction_date asc ) request_number_min
         , min(request_number) keep(dense_rank first order by transaction_date desc) request_number_max
      from (
            select min(g.transaction_date) transaction_date
                 , d.request_number
              from mtl_txn_request_headers d
                 , mtl_txn_request_lines e
                 , mtl_material_transactions g
                 , mtl_transaction_types i
             where d.header_id                    = e.header_id
               and g.transaction_type_id          = i.transaction_type_id
               and upper(i.transaction_type_name) = upper('WIP Issue')
               and e.line_id                      = g.move_order_line_id
               and d.move_order_type              = 5
               and to_date(g.transaction_date) between to_date('01-jan-2014') and to_date('31-dec-2014')
             group
                by d.request_number
           )
    
  • Need help with sql query performance

    Dear all,

    I have a sql like query below, I need to give the following query please help me identify which statement I should tune to have better performanece.

    Select rownum LINE_NUM,

    A.LINE_ID,

    TO_CHAR (A.INVITMID),

    TO_NUMBER (A.PICKQTY),

    UNLOADINGPNT NULL,

    RRNUM NULL,

    WORKORDNUM NULL,

    WORKORDDESC NULL,

    A.PONUM,

    DTR_DUMB NULL,

    A.DESCRIPTION,

    FROM_SUB NULL,

    TO_SUB NULL,

    NO SOURCE,

    ASSET_NUMBER NULL,

    A.RECEIPTNUM,

    MOVEORD NULL,

    FROM_LOC NULL,

    TO_LOC NULL,

    MSD_NUM NULL,

    CONTAIN_LINE NULL,

    A.UOM,

    A.PO_RELEASE

    de)

    Select headerid Po.Po_Header_Id,

    rcv1. Po_Line_Id LINE_ID,

    rcv1.item_id INVITMID,

    (NVL(Rcv1.Transact_Qty,0)-NVL(rcv2.transact_qty,0)) PICKQTY,

    Po.Segment1 PONUM,

    Rcv1.Receipt_Num RECEIPTNUM,

    Rcv1.Item_Desc DESCRIPTION,

    Rcv1.Transact_Uom GLU,

    Rcv1.Po_release

    Po_Headers_All in.,.

    (Select rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, SUM (rcv3. Transact_Qty) Transact_Qty, rcv3. PO_RELEASE OF)

    SELECT A.Po_Header_Id,

    C.RECEIPT_NUM receipt_num,

    A.Po_Line_Id,

    A.Destination_Type_Code,

    B.Item_Id,

    B.item_description Item_Desc,

    A.UNIT_OF_MEASURE Transact_Uom,

    A.QUANTITY Transact_Qty,

    D.RELEASE_NUM PO_RELEASE

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND UPPER (A.Transaction_Type) = "to DELIVER".

    AND higher (A.Destination_Type_Code) = "EXPENSES".

    AND D.PO_RELEASE_ID = A.PO_RELEASE_ID

    UNION ALL

    SELECT A.Po_Header_Id,

    C.RECEIPT_NUM receipt_num,

    A.Po_Line_Id,

    A.Destination_Type_Code,

    B.Item_Id,

    B.item_description Item_Desc,

    A.UNIT_OF_MEASURE Transact_Uom,

    A.QUANTITY Transact_Qty,

    D.RELEASE_NUM PO_RELEASE

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND B.ITEM_ID IS NULL

    AND UPPER (A.Transaction_Type) = "to DELIVER".

    AND higher (A.Destination_Type_Code) = "WORKSHOP".

    D.PO_RELEASE_ID AND = A.PO_RELEASE_ID) rcv3

    GROUP BY rcv3. Po_Header_Id, RCV3.receipt_num, rcv3. Po_Line_Id, rcv3. Destination_Type_Code, rcv3. Item_Id, rcv3. Item_Desc, rcv3. Transact_Uom, rcv3. Rcv1 PO_RELEASE),

    (SELECT A.PO_LINE_ID,

    Sum (A.Quantity) transact_qty,

    A.PO_HEADER_ID,

    C.RECEIPT_NUM

    OF RCV_TRANSACTIONS,.

    RCV_SHIPMENT_HEADERS C.

    B RCV_SHIPMENT_LINES,

    PO_RELEASES_ALL D

    WHERE C.SHIPMENT_HEADER_ID = A.SHIPMENT_HEADER_ID

    AND B.SHIPMENT_LINE_ID = A.SHIPMENT_LINE_ID

    AND UPPER (A.Transaction_Type) = "RETURN to the RECEPTION"

    AND D.PO_RELEASE_ID = A.PO_RELEASE_ID

    A.PO_LINE_ID, A.PO_HEADER_ID, C.RECEIPT_NUM GROUP) Rcv2

    Where Po.Po_Header_Id = Rcv1.Po_Header_Id (+)

    And Rcv1.Po_Line_Id = Rcv2.Po_Line_Id (+)

    And Rcv1.Receipt_Num = Rcv2.Receipt_Num (+)

    And Rcv1.Transact_Qty <>Nvl(Rcv2.Transact_Qty,999999999)

    Group of po.po_header_id, rcv1.po_line_id, po.segment1, rcv1.receipt_num, rcv1.item_id, Rcv1.Item_Desc, rcv1. TRANSACT_UOM, rcv1. PO_RELEASE, (NVL(Rcv1.Transact_Qty,0)-NVL(RCV2.transact_qty,0))) has

    Is my version of the database: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    Please find the attached PLAN to EXPLAIN.

    EXPLAINPLAN.jpg

    Thank you

    May be essentially the same join (between a, b, c and d) twice instead of three times

    sounds like it could be done with a single join (between a, b, c, and d), but you do not some columns than the columns of rcv1 rcv2

    We can work on what we can see only

    Select rownum line_num,

    rcv1.po_line_id line_id,

    TO_CHAR (rcv1.item_id) invitmid,

    NVL(rcv1.transact_qty,0) - nvl(rcv2.transact_qty,0) pickqty,

    unloadingpnt null,

    rrnum null,

    workordnum null,

    workorddesc null,

    Po. Ponum Segment1,

    dtr_dumb null,

    description of the rcv1.item_desc,

    from_sub null,

    to_sub null,

    No source,

    asset_number null,

    rcv1.receipt_num receiptnum,

    moveord null,

    from_loc null,

    to_loc null,

    msd_num null,

    contain_line null,

    Glu rcv1.transact_uom,

    rcv1.po_release

    of po_headers_all in.

    left outer join

    (select a.po_header_id,

    c.receipt_num,

    a.po_line_id,

    a.destination_type_code,

    b.item_id,

    b.item_description item_desc,

    a.unit_of_measure transact_uom,

    Sum (a.Quantity) transact_qty,

    d.release_num po_release

    from (select shipment_header_id,

    shipment_line_id,

    po_release_id,

    po_header_id,

    po_line_id,

    destination_type_code,

    unit_of_measure,

    quantity

    of rcv_transactions

    where upper (a.transaction_type) = "to DELIVER".

    and upper (a.destination_type_code) ('charge', 'WORKSHOP')

    ) a

    inner join

    rcv_shipment_lines b

    On a.shipment_line_id = b.shipment_line_id

    inner join

    c rcv_shipment_headers

    On a.shipment_header_id = c.shipment_header_id

    left outer join

    po_releases_all d

    On a.po_release_id = d.po_release_id

    where upper (a.destination_type_code) = "EXPENSES".

    or (upper (a.destination_type_code) = 'WORKSHOP'

    and b.item_id is null

    )

    A.po_header_id group,

    c.receipt_num,

    a.po_line_id,

    a.destination_type_code,

    b.item_id,

    b.item_description,

    a.unit_of_measure,

    d.release_num

    ) rcv1

    On po.po_header_id = rcv1.po_header_id

    left outer join

    (select a.po_line_id,

    Sum (a.Quantity) transact_qty,

    a.po_header_id,

    c.receipt_num

    from (select shipment_header_id,

    shipment_line_id,

    po_release_id,

    po_header_id,

    quantity

    of rcv_transactions

    where upper (a.transaction_type) = "RETURN to THE RECIPIENTS.

    ) a

    inner join

    rcv_shipment_lines b

    On a.shipment_line_id = b.shipment_line_id

    inner join

    c rcv_shipment_headers

    On a.shipment_header_id = c.shipment_header_id

    left outer join

    po_releases_all d

    On a.po_release_id = d.po_release_id

    A.po_line_id group,

    a.po_header_id,

    c.receipt_num

    ) rcv2

    On rcv1.po_line_id = rcv2.po_line_id

    and rcv1.receipt_num = rcv2.receipt_num

    where rcv1.transact_qty! = nvl(rcv2.transact_qty,999999999)

    Concerning

    Etbin

  • Help with SQL Query (Subselects)

    Hello community,

    IAM a new Member in this forum. The first excuse my English, my native language is German.
    In my workplace, we have a great Orcle Database 11 g with 30 different tables for production control issues.
    I try to get a couple of different information from the database, so I started with SQL of the query, but for this problem, I wasn't able to write a query to work.

    In this case, I have 2 tables:
    Table 1:
    ID; ORDER_NR; DESCRIPTION; CREATE_DATE
    1; A500236; CLEAN HOUSE; 20/02/2012
    2; A623555; REPAIR CAR; 10/01/2012
    3; A866944; MAINTAIN EQUIPMENT; 11/02/2012

    Table 2:
    ID; ORDER_NR; WO_STEP; STEP_DATE; EMPLOYEE
    1; A500236; A; 21/02/2012; W0010
    2; A500239; F; 21/02/2012; W0010
    3; A500239; S; 22/02/2012; W0027
    4; A500239; R; 23/02/2012; H0034
    5; A500239; U; 25/02/2012; L0099
    6; A263555; A; 15/01/2012; G0009
    7; A263555; C; 17/01/2012; S0039
    5 V A263555; R; 18/01/2012; K0059
    9; A263555; U; 19/01/2012; A0048
    10; A866944; A; 13/02/2012; H0034
    11; A866944; B; 13/02/2012; L0035
    12; A866944; G; 17/02/2012; D0084
    13; A866944; U; 23/02/2012; S0039

    And the result of my query should look like this:
    ORDER_NR; DESCRIPTION; CREATE_DATE; A_STAT_AGE; R_STAT_AGE; U_STAT_AGE
    A500236; OWN HOME; 20/02/2012; 5; 3; 1
    A623555; REPAIR CAR; 01/10/2012; 42; 39; 38
    A866944; MAINTAIN EQUIPMENT; 11/02/2012; 15; 4; 3

    The age of my query result should be calculated from the date of the creation of the order.
    I would like to know 2 things, one is how old was the order when they reached this status, R and U.
    The second, that is, how long did you order remaining on A stat, R and U (and if possible all the others too)
    It can happen that not every order reaches every State, so he ca go directly from A you in this case I want to display a generic character in this row/column

    I hope you all know what I mean and what result to expect.

    Thanks for your help.

    Reinhard W.

    Hi, Reinhard,

    990524 wrote:
    Hi Frank,.

    I thank you for your professional response and excuse my non-professional way to clarify my question.
    I have now already read and understand how to ask good questions, but is there an easy an inexpensive way to run a database on my computer at home?

    You can download the Oracle database from this site. The Express edition is easier to install. It lacks a few features that the Enterprise edition (for example, partitioning table and safety of level line), but most of the things work in all editions of Oracle.
    All editions are free if you do not use them for Production applications. Of course, at this price (or lack thereof), you get Oracle support.

    I have already noticed that the syntax SQL for Oracle differ from other DBMSS.

    Yes and some features are different in different products.

    Your query works perfectly, thank you.

    Now I want to refine my (your) query, is there a way to display a range of day within 1 day? As 0.5 day to half a day between two State timestamp.

    Of course, you can do it. After the exact output desired of the sample data that I posted. If the sample data do not show what you need to do, then after a few different examples of data that makes.

    My table contains the Date and time in a row, I forgot to mention that.

    After a CREATE TABLE statement. I was guessing a lot of things that may be important. For example, I assumed you were using a DATE for date and time column. So it is only reasonable to do so, but maybe it's not what looks like at your table.

    I tried something like this:

    SYSDATE - MIN ( CASE
                        WHEN  t2.wo_step = 'A'
                  THEN  t2.step_date
              END
               ) AS Age_A
    

    But it gives me an error of arithmetic overflow and not die difference in the Date and time or days with decimal friction.

    There are only about 5.4 million possible days with DATEs of Oracle. Is only 7 digits and Oracle can treat approximately 39 numbers without overflow errors, so I don't see how this can happen if t2.step_date is really a DATE. This is an example of why you need to publish a CREATE TABLE statement.

  • Need help with SQL Query - change of name history of audit table.

    I need your help to find the result in the following way...


    Emp No    New_name    Old_Name
    -----------------------------------------------
    1           Name3        Name2
    1           Name2        Name1
    create table emp(emp_id number(10),
    emp_name varchar(50),
    constraints emp_pk primary key(emp_id) );
    
    
    
    create table emp_audit(
    audit_id number(10),
    emp_id number(10),
    emp_name varchar(50),
    audit_date date,
    constraints emp_audit_pk primary key (audit_id),
    constraints  emp_audit_emp_fk foreign key(emp_id)  references emp(emp_id));
    
    insert into emp values(1,'Name3');
    
    
    insert into EMP_AUDIT (audit_id, emp_id, emp_name, audit_date)
    values (1, 1, 'Name1', to_date('14-08-2011', 'dd-mm-yyyy'));
    insert into EMP_AUDIT (audit_id, emp_id, emp_name, audit_date)
    values (2, 1, 'Name2', to_date('15-08-2011', 'dd-mm-yyyy'));
    commit;
    Thank you...

    Dipabkar Bédard (DB) wrote:

    We write is the query without using "partition by" in oracle...?

    with t as (
               select  audit_id,
                       emp_id,
                       emp_name,
                       row_number() over(order by audit_id) rn
                 from  emp_audit
                 order by emp_id,
                          audit_id
              )
    select  a.audit_id,
            a.emp_id,
            a.emp_name old_name,
            nvl(b.emp_name,(select c.emp_name from emp c where c.emp_id = a.emp_id)) new_name
      from  t a left join t b
            on (
                    b.emp_id = a.emp_id
                and
                    b.rn = a.rn + 1
               )
    /
    
      AUDIT_ID     EMP_ID OLD_NAME                                           NEW_NAME
    ---------- ---------- -------------------------------------------------- ------------
             1          1 Name1                                              Name2
             2          1 Name2                                              Name3
    
    SQL> 
    

    And without analytical functions:

    with t1 as (
                select  audit_id,
                        emp_id,
                        emp_name
                  from  emp_audit
                  order by emp_id,
                           audit_id
               ),
         t2 as (
                select  audit_id,
                        emp_id,
                        emp_name,
                        rownum rn
                  from  t1
               )
    select  a.audit_id,
            a.emp_id,
            a.emp_name old_name,
            nvl(b.emp_name,(select c.emp_name from emp c where c.emp_id = a.emp_id)) new_name
      from  t2 a left join t2 b
            on (
                    b.emp_id = a.emp_id
                and
                    b.rn = a.rn + 1
               )
    /
    
      AUDIT_ID     EMP_ID OLD_NAME                                           NEW_NAME
    ---------- ---------- -------------------------------------------------- ----------
             1          1 Name1                                              Name2
             2          1 Name2                                              Name3
    
    SQL> 
    

    SY.

  • Urgent help of SQL query

    Hello

    I need assistance with SQL... My version of the database is Oracle 10 g Release 1.2.

    I have two tables with the same exact structure as follows...

    create table t1 (cust_id, number (5), zone_number number (2), part_number varchar2 (10))
    create table t2 (cust_id, number (5), zone_number number (2), part_number varchar2 (10))


    I need a query that would give me a two count indictment by pairs of customer between t1 and t2...

    1. part_number County matches between the same areas in table 1 and table 2 for the pairs of customers

    for customer 1 and 10 for zone 1 this total shoulb be 2 because there are two matches part A numbers between 1 and 10 for Zone 1 cust and B.

    for customer 1 and 10 for zone 2 this total shoulb be 0 because between 1 and 10 for Zone 2 cust, there is no part number match

    for customer 1 and 20 for the zone 1 this total shoulb be 0 because between 1 and 20 for Zone 1 cust, there is no part number match

    for 1 client and client 20 zone 2 this total shoulb be 0 because between 1 and 20 to Zone 2 cust is a part number, it's 'F'

    for 1 client and client 20 zone 3 of this total shoulb be 0 because there is no zone 3 for client 1

    same way
    .. Cust 2 and cust 10 zone2 and Zone 1 figures account
    .. 2 and cust 20 zones 1, 2 and 3 counts


    2. the unique total part nmbers between the same areas for customers in table 1 pairs and the table2

    for example

    part numbers only between 1 and 10 for zone 1 cust are A, B and C so the count should be 3
    part numbers only between 1 and 10 for zone 2 cust are B, F, D, and E the county should be 3
    unique piece between 1 and 20 for the zone 1 cust numbers A, B, C and G so the count should be 4
    and so on so forth




    insert into t1 values (1, 1, 'A')
    insert into t1 values (1, 1, 'B')
    insert into t1 values (1, 1, "C")
    insert into t1 values (1, 2, 'B')
    insert into t1 values (1, 2, 'F')
    insert into t1 values (1, 2, has ')
    insert into t1 values (1, 2, 'E')
    insert into t1 values (2, 1, 'F')
    insert into t1 values (2, 2, 'G')
    insert into t1 values (2, 2, 'H')

    insert into t2 values (10, 1, 'A')
    insert into t2 values (10, 1, 'B')
    insert into t2 values (10, 2, null)
    insert into t2 values (20, 1, 'G')
    insert into t2 values (20, 2, 'F')
    insert into t2 values (20, 2, 'H')
    insert into t2 values (20, 3, 'I')
    insert into t2 values (20, 3, 'J')

    My query result should be as follows...

    cust1 cust2 area pn_match_count total_unique_pn_count

    1 10 1 2 3
    1 10 2 0 4
    1 20 1 0 4
    1 20 2 1 5
    1 20 3 0 2
    2 10 1 0 3
    2 10 2 0 2
    2 20 1 0 2
    2 20 2 1 3
    2 20 3 0 2

    I would appreciate your help in the drafting of this query.

    Thanks in advance

    Or leave the 'where' clause, so we get each combination of customer:

    SQL> with c as
      2    (select distinct t1.cust_id cust1, t2.cust_id cust2, t1.zone_number zone, t1.part_number p1, t2.part_number p2
      3       from t1, t2
      4       where t1.zone_number=t2.zone_number
      5         and t1.part_number = t2.part_number
      6     union
      7     select distinct t1.cust_id cust1, t2.cust_id cust2, t1.zone_number zone, t1.part_number p1, null p2
      8       from t1, t2
      9       where not exists (select 1 from t2 t02 where t02.zone_number=t1.zone_number
     10                           and t02.cust_id = t2.cust_id
     11                           and t02.part_number = t1.part_number)
     12     union
     13     select distinct t1.cust_id cust1, t2.cust_id cust2, t2.zone_number zone, null p1, t2.part_number p2
     14       from t1, t2
     15       where not exists (select 1 from t1 t01 where t01.zone_number=t2.zone_number
     16                           and t01.cust_id = t1.cust_id
     17                           and part_number = t2.part_number))
     18  select cust1, cust2, zone, sum(decode(p1,p2,1,0)) pn_match_count, count(*) unique_pn_count
     19    from c
     20    group by cust1, cust2,zone;
    
         CUST1      CUST2       ZONE PN_MATCH_COUNT UNIQUE_PN_COUNT
    ---------- ---------- ---------- -------------- ---------------
             1         10          1              2               3
             1         10          2              1               5
             1         20          1              0               4
             1         20          2              1               5
             1         20          3              0               2
             2         10          1              0               3
             2         10          2              1               3
             2         20          1              0               2
             2         20          2              1               3
             2         20          3              0               2
    
    10 rows selected.
    
  • Help with SQL query

    Please help in the slot provided.

    I have two columns A and B in a table with data as below,

    A AND B

    1 INACTIVE

    ACTIVE 1

    2 ACTIVE

    DEFAULT 2

    2 DEGENERATE

    2 INACTIVE

    3 PASSED

    3 DEFAULT

    3 DEGENERATE

    3 DEGENERATE

    INACTIVE 3

    4 INACTIVE

    DEFAULT 4

    5 BY DEFAULT

    5 ACTIVE

    I want to write a query that will give the values in column A where column B must not have value as 'ACTIVE' after group the data in column A.

    That is to say of the above example data that the outcome should be 3 and 4 since only 3 and 4 was not ACTIVE as a value in column B.

    My database is version 11g 11.2.0.3 on OS linux.


    nothing... just use

    SELECT A

    FROM THE DATA

    Group by one

    having MAX (CASE b WHEN "ACTIVE", THEN b ELSE NULL END) is null;

    because you have a table named somehow (icies data) and you have columns in it with a name, I just don't want to create tables, insert data, and remove the tables afterwards - it's the reason why the with.

    HTH

  • Help with sql query / a subquery to perform the COUNT

    Hello everyone,

    Co-worker colleague asked me to post a request here in hopes of getting more support.  Here's the question:

    There are 3 tables associated with this request.  A table of the application, which displays a number of application open for a particular request, an audit table that shows you all employees who worked on this application and one employee who shows you the details of the employee table.  My colleague is trying to understand what, how to see only applications that have been published by a wizard, but not a Manager.  There are a few applications that worked on both, but he won't see this request in its results.

    Here are the tables

    EMP

    ID FULL_NAME
    1234John Doe
    5467Jane Doe
    2345Clark Kent
    5432June Cleaver

    Unfortunately, this table does not have a title column (which was created provider, so we cannot change the internal structure).  My colleague knows who is the Assistant and the Director, then in this case, the first two of the id:

    1234 and 5467 are managers and the other 2 are assistants

    T_APPLICATION

    app_id app_number date_opened app_type
    901854778JANUARY 10, 2014NETWORK
    901954779JANUARY 11, 2014DATABASE
    901055000MARCH 12, 2014MATERIAL

    T_APP_AUDIT

    APP_ID PROCESSED_BY
    90181234
    90182345
    90185432
    90192345
    90195432
    90105432

    So, here is the actual query, I was given: it is actually to check the number of times that each application has been published per person:

    Select a.app_id, c.full_name, count (*) that controls

    t_application a, t_app_audit b, c of the emp

    where a.app_id = b.app_id and b.processed_by = c.id and

    a.app_type in ('NETWORK', "DATABASE")

    and b.processed not in ('1234, ' 5467')

    GROUP OF A.APP_ID, c.full_name;

    IF I won't see 9018 in my results, since at one time it was edited by a Manager (1234).  Unfortunately, this request does not eliminate the app_id 9018, it eliminates only the name of handlers appear in the query.  I hope I've explained this properly, and any help to point us in the right direction is welcomed.  Thanks in advance.

    Select

    a.app_id

    c.full_name

    , count (*) as the controls

    of t_application one

    t_app_audit b

    c of the emp

    where a.app_id = b.app_id

    and b.processed_by = c.id

    and a.app_type in ('NETWORK', "DATABASE")

    and b.app_id by (not in

    Select app_id

    of t_app_audit

    where processed_by in ('1234, ' 5467')

    )

    GROUP OF A.APP_ID, c.full_name;

  • Help with SQL query for SQL experts!

    Hi all

    For the very expert on SQL.

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

    create table t1 (STEP number);

    Insert into t1 select rownum from dba_tables where rownum < 11;

    commit;

    create table t2 (the STEP number, name varchar2 (4), varchar2 (1)) of the State;

    insert into t2 values (1, 'TOTO', ' A');

    insert into t2 values (3, 'TOTO', 'C');

    insert into t2 values (4, 'TOTO', ');

    insert into t2 values (1, 'FIFI', ' A');

    insert into t2 values (6, 'FIFI', 'B');

    commit;

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

    My goal is to list the STATE in the NAME of all the STEPS, even if measures do not exist for a NAME.

    In other words, my query should return a result like this: (query on t1 and t2, of course)

    STEP   1      2             3            4               5            6

    TOTO A C D NO NO NO

    FIFI HAS NO NO NO NO D

    Thank you in advance.

    with the data as)
    Select
    T1. Step step
    name
    State
    Of
    T1, t2
    where
    T1. Step = T2. Step (+)
    )

    Select
    *
    from the data
    Pivot (max (state))
    for step (1,2,3,4,5,6,7,8,9,10)
    )
    where the name is not null

    NAME 1 2 3 4 5 6 7 8 9 10
    TOTO A - C D - - - - - -
    FIFI A - - - - B - - - -

    If the column name must be the stage instead of the name

    with the data as)
    Select
    Step 1 T1. Step
    name as step
    State
    Of
    T1, t2
    where
    T1. Step = T2. Step (+)
    )

    Select
    *
    from the data
    Pivot (max (state))
    for step 1 in (1,2,3,4,5,6,7,8,9,10)
    )
    where the step is not null

  • Help with SQL query definition

    Hello

    Could someone please help me with the sub condition.

    On the example of the EMP table, I want a report that looks like:
    Manager Tot_Joiniees_Q1 Tot_Joiniees_Q2 Tot_Joiniees_Q3 Tot_Joiniees_Q4
    Please see the application for the relevant columns and the definitions of the watch below:
    SELECT   mgr,
             CASE
                WHEN TO_CHAR (hiredate, 'MON') IN ('JUN', 'JUL', 'AUG')
                   THEN 'Q1'
                WHEN TO_CHAR (hiredate, 'MON') IN ('SEP', 'OCT', 'NOV')
                   THEN 'Q2'
                WHEN TO_CHAR (hiredate, 'MON') IN ('DEC', 'JAN', 'FEB')
                   THEN 'Q3'
                WHEN TO_CHAR (hiredate, 'MON') IN ('MAR', 'APR', 'MAY')
                   THEN 'Q4'
             END qtd,
             COUNT (DISTINCT ename)
        FROM emp
    GROUP BY mgr,
             CASE
                WHEN TO_CHAR (hiredate, 'MON') IN ('JUN', 'JUL', 'AUG')
                   THEN 'Q1'
                WHEN TO_CHAR (hiredate, 'MON') IN ('SEP', 'OCT', 'NOV')
                   THEN 'Q2'
                WHEN TO_CHAR (hiredate, 'MON') IN ('DEC', 'JAN', 'FEB')
                   THEN 'Q3'
                WHEN TO_CHAR (hiredate, 'MON') IN ('MAR', 'APR', 'MAY')
                   THEN 'Q4'
             END
    ORDER BY 1, 2
    Thank you
    CJM
    SELECT   mgr Manager,
             COUNT(CASE WHEN TO_CHAR(hiredate,'Q') = '1' then 1 end) Tot_Joiniees_Q1,
             COUNT(CASE WHEN TO_CHAR(hiredate,'Q') = '2' then 1 end) Tot_Joiniees_Q2,
             COUNT(CASE WHEN TO_CHAR(hiredate,'Q') = '3' then 1 end) Tot_Joiniees_Q3,
             COUNT(CASE WHEN TO_CHAR(hiredate,'Q') = '4' then 1 end) Tot_Joiniees_Q4
        FROM emp
        GROUP BY mgr,
                 TO_CHAR(hiredate,'Q')
    /
    
       MANAGER TOT_JOINIEES_Q1 TOT_JOINIEES_Q2 TOT_JOINIEES_Q3 TOT_JOINIEES_Q4
    ---------- --------------- --------------- --------------- ---------------
          7566               0               0               0               1
          7839               0               3               0               0
          7698               0               0               2               0
          7788               0               1               0               0
          7782               1               0               0               0
          7566               0               1               0               0
          7902               0               0               0               1
          7698               2               0               0               0
                             0               0               0               1
          7698               0               0               0               1
    
    10 rows selected.
    
    SQL> 
    

    SY.

  • Need help with sql query involving distinct and County

    I have 2 tables and I want to get the number of specific names. Find the details below. It's hard to explain but I will try to provide as much detail as I can.

    Table A:
    ID of the SR
    1001 1
    1002 2
    2 1003
    1004 3


    Table B:
    Name of the key SrNew
    1 David 1001
    2 James 1002
    3 James 1002
    4 James 1003
    5 James 1004
    6 Mike 1004

    Result: I'm looking:
    Count names such as if the name appears for the same ID of Table A two times, and then only count 1.

    Name of County
    David 1
    James 2 (1002 and 1003 for the same ID (ID #2) so count as 1 for this and then 1 more for 1004 and ID 3)
    1 Mike


    I have following question:
    SELECT distinct (b.Name), a.ID
    FROM TableA, TableB b
    Where a.Sr = b.SrNew Group By b.Name, a.ID

    and as a result I get:
    ID name
    David 1
    James 2
    3 James
    3 Mike


    Now, I want to just the number of each name with the result, but don't know how I can do this with a sql?

    Thanks in advance.

    Who help me?

    SELECT b.Name, count(distinct a.ID)
    FROM TableA a, TableB b
    Where a.Sr = b.SrNew
    Group By b.Name
    

    Nicolas.

  • Need help with SQL query (10 years since I have SQL)

    Hi guys,.

    I tried to remove some rust SQL. I did not go to SQL within a certain time.

    Here are the tables (% = ent $ = varcchar)
    -----------------
    RESULT
    -----------------
    % RESULT_ID-$ RC-% YEAR_ID - PERIOD_ID-% CONTRACT_NO-% value %
    20841---1439---83---37---010427102---1
    20842---1439---83---41---010427102---2
    20843---1439---84---37---010427102---3
    20844---1439---84---41---010427102---4
    20845---1439---83---34---010427102---5

    -----------------
    YEARS
    -----------------
    YEAR_ID - YEAR_DESC
    83 2010-2011
    84 2011-2012

    -----------------
    PERIOD
    -----------------
    PERIOD_ID - PERIOD_DESC
    34 14
    37 02
    41 03

    What I want is to get the last period of each year in the RESULT table. (I'll work the rest of the WHERE statement myself, because I have omitted some news, or columns for simplicity)
    Note that I can't rely on the ID of the period, as the chronological order is not respected.

    In the scenario above, I should GET
    % RESULT_ID-$ RC - YEAR_ID - PERIOD_ID-% CONTRACT_NO-% % value %
    20844---1439---84---41---010427102---4
    20845---1439---83---34---010427102---5



    I have the following, but it only returns values if the year is the last time!

    SELECT DISTINCT RESULTS. RESULT_ID, RESULT. RC, RESULT. PERIOD_ID, RESULT. YEAR_ID, RESULT. CONTRACT_NO, YEARS. YEAR_DESC
    RESULT, YEARS
    WHERE TO TRAIN. YEAR_ID = YEARS. YEAR_ID AND - it is my problem, I want the last period for each year.
    RESULT. PERIOD_ID = (SELECT PERIOD_ID FROM PERIOD WHERE CAST (PERIOD_DESC AS INT) = (Select MAX (CAST (PERIOD_DESC AS INT)) of the period)) - I think it works fine I'm able to get the last period
    ORDER BY YEARS. YEAR_DESC;

    Published by: 935079 on May 17, 2012 11:32
    Add space not formatted tabs

    Published by: 935079 on May 17, 2012 11:35

    Your way to present the data that makes it very tedious to create test data. Next time please simply provide CREATE TABLE and INSERT statements so that we can all share the same test data.

    Without test data, I think you want Frank solution with a very minor change: order value instead of id, then the number of line 1 is the highest value.

    with GOT_R_NUM as (
      SELECT r.* -- or list the columns you want
      , ROW_NUMBER() over(partition by R.YEAR_ID, R.CONTRACT_NO
        order by
    --    P.PERIOD_ID
        P.PERIOD_VALUE
        DESC NULLS LAST) AS r_num
      FROM result r
      JOIN period p ON r.period_id = p.period_id
      )
    SELECT * -- or list all columns except r_num
    FROM got_r_num
    WHERE r_num = 1;
    
  • Please help build a sql query

    Hello

    Please help build a sql query


    My Table Test2015 has given below

    Header_id Line_id Ordered_item       

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887292 SH-POS-NO-BR POS-INS

    199926 723887293 MAIN2

    199927 723887294 IC-ENV-NON-BR-ENV-PXY

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199927 723887298 PRCSS SH-FAIRY-ELEC DISTR.

    199927 723887299 SH-FAIRY-SUM PRO-DE-CONS-HOUSE

    I am trying to query my Test2015 table to obtain the records with ordered_item containing 'MAIN1' and 'MAIN2' only. I tried to write a query as below

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1', 'MAIN2');

    But it gives me all the data with the MAIN2 records found but MAIN1 is absent, I want to retrieve only records to both 'MAIN1' and 'MAIN2' present for Header_id.

    While the result below shows me header_id - 199926 and 199929 that he should assume back. I want to fetch documents only with 'MAIN1' and 'MAIN2' both present.

    Header_id Line_id Ordered_item            

    723887290 199925 MAIN1

    199925 723887291 MAIN2

    199926 723887293 MAIN2

    199927 723887295 MAIN1

    199927 723887297 MAIN2

    199929 723887299 MAIN1

    Please suggest.

    Thank you and best regards,

    Prasad.

    Hello

    Try like this...

    SELECT * FROM test2015 WHERE ORDERED_ITEM in ('MAIN1") and in header_id (select test2015 WHERE ORDERED_ITEM in ('MAIN2') header_id)

  • Need help with a SQL query

    Hello

    I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)

    Primary_ID raj_id Raj_number Raj_format

    1                            raj                 rajvend                      1

    2                            raj                 rajvend                      1

    3                            raj                 rajvendor1                 2

    4                            raj                 rajvendor1                 2

    5                            raj                 rajvendor1                 2

    6                            raj                 rajvendor2                 3

    I used under SQL to get query output as below, but has not achieved the required result:

    Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)

    from raj_table by sl_no asc

    SL_NO raj_id raj_number raj_format primary_id

    1                   1                   raj              rajvendor                 1

    1                   2                  raj              rajvendor                 1

    2                   3                   raj              rajvendor1                2

    2                   4                   raj              rajvendor1                2

    2                   5                  raj               rajvendor1                2

    3                   6                    raj              rajvendor2                3

    I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.

    with t as)

    Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the

    Select option 2, 'raj', 'rajvend', 1 double Union all

    Select 3, 'raj', 'rajvendor1', 2 double Union all

    Select 4, 'raj', 'rajvendor1', 2 double Union all

    Select 5, 'raj', 'rajvendor1', 2 double Union all

    Select 6, 'raj', 'rajvendor2', 3 double

    )

    Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,

    t.*

    t

    order by primary_id

    /

    PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
    ---------- ---------- --- ---------- ----------
    1 1 raj rajvend 1
    1 2 raj rajvend 1
    2 3 raj rajvendor1 2
    2 4 raj rajvendor1 2
    2 5 raj rajvendor1 2
    3 6 raj rajvendor2 3

    6 selected lines.

    SQL >

    SY.

Maybe you are looking for

  • How to empty the lower part of the e-mail screen

    I would like the bottom of my screen (under the emails listed) email left blank. It now contains information user of Thunderbird, I don't need to see. How do I "blank" it?

  • iCloud 9.3.1 iOS does not

    Hello after update to iOS 9.3.1 I'm not able to backup to iCloud with iPhone is some suggestions? TKS

  • Satellite M40-225 download Phoenix recover tiscali pro6

    keep getting right message at the end of the failed download "year error has occurred - installation wizard has encountered a problem running chkdsk/f to correct & try again" tried 5 times and contacted tiscali advising of 'something' on my laptop do

  • External microphone no longer works on Satellite M40 PSM44

    HelloI have the problem with the external microphone on my satellite M40 PSM44. When I connect it do all the settings its fine for a while to work. All of a sudden it stop working. After that I had to uninstall audio drivers and install it again and

  • HP Phoenix h9 - 1110t Windows 8 bios upgrade available but where?

    By HP links. http://h30434.www3.HP.com/T5/desktop-operating-systems-software-recovery/HP-PCs-basic-input-output-S... https://h20564.www2.HP.com/hpsc/doc/public/display?docid=emr_na-c04393276 According to these 2 links. An update is available for my P