Need help with SQL for the following logic.

I have a table with 3 columns and the following data:
DATE NAME S_NO
1 ABC1 JANUARY 1, 2001
1 ABC2 JANUARY 10, 2001
1 ABC3
1 ABC4
ABC1 2 1 DECEMBER 2010
ABC2 2
2 ABC3 DECEMBER 10, 2010
2 ABC4 DECEMBER 5, 2010
3 XYZ1 FEBRUARY 1, 2001
3 XYZ2 FEBRUARY 2, 2001

I need to retrieve the value of NAME for each S_NO corresponding to the maximum DATE. In the above example, it will be:
1 ABC2
2 ABC3
3 XYZ2
/* Formatted on 9/10/2012 2:50:13 PM (QP5 v5.139.911.3011) */
WITH t AS (SELECT 1 S_NO, 'ABC1' NAME, '1-JAN-2001' dt FROM DUAL
           UNION ALL
           SELECT 1, 'ABC2', ' 10-JAN-2001' FROM DUAL
           UNION ALL
           SELECT 1, 'ABC3', NULL FROM DUAL
           UNION ALL
           SELECT 1, 'ABC4', NULL FROM DUAL
           UNION ALL
           SELECT 2, 'ABC1', '1-DEC-2010' FROM DUAL
           UNION ALL
           SELECT 2, 'ABC2', NULL FROM DUAL
           UNION ALL
           SELECT 2, 'ABC3', '10-DEC-2010' FROM DUAL
           UNION ALL
           SELECT 2, 'ABC4', '5-DEC-2010' FROM DUAL
           UNION ALL
           SELECT 3, 'XYZ1', '1-FEB-2001' FROM DUAL
           UNION ALL
           SELECT 3, 'XYZ2', '2-FEB-2001' FROM DUAL)
SELECT *
  FROM (SELECT S_NO,
               name,
               RANK ()
               OVER (PARTITION BY s_no
                     ORDER BY TO_DATE (dt, 'dd-mon-yyyy') DESC)
                  rk
          FROM t
         WHERE dt IS NOT NULL)
 WHERE 1 = RK

Tags: Database

Similar Questions

  • Help with Sql for the annual report by month

    Hi, I got the task of creating an annual report each month that shows the benefits of the enterprise by month and total in the last column to show what branch was the hightest income.
    Branch | January | February | March | April | Can | June... | Total |
    ABC | $0.00 | $0.00 | $0.00 | $0.00 | $0.00 | $0.00 | Total of the Amt |
    DEF | $18.01. $3.88. $18.01. $4.12 | $18.01. $3.97 | Total of the Amt |

    Can someone please help me give an idea of how to write sql for this report... ? I build subqueries for everymonth giving dates for Jan/Feb/March... but I think that this is not the right way to do it...
    -----------------------------------------------------------------------------------------------------------------------
    SELECT
    Sum (a.Commission) December,
    Sum (b.Commission) November
    Of
    (
    Select
    c.account_ID,
    c.Officer,
    c.account_product_class_id,
    Sum (c.dp_monthly_premium) Commission
    Of
    c contract
    Where
    c.account_ID = 109 and
    c.Status = 'APPROVED' and
    c.protection_effective between December 1, 2009 "and on December 31, 2009"
    Group by
    c.account_ID,
    c.Officer,
    c.account_product_class_id
    ),
    (
    Select
    c.account_ID,
    c.Officer,
    c.account_product_class_id,
    Sum (c.dp_monthly_premium) Commission
    Of
    c contract
    Where
    c.account_ID = 109 and
    c.Status = 'APPROVED' and
    c.protection_effective between November 1, 2009 "-November 30, 2009"
    Group by
    c.account_ID,
    c.Officer,
    c.account_product_class_id
    ) b
    -----------------------------------------------------------------------------------------------------------
    I always hight hope this forum. So please help. Thanks in advance.

    Published by: Aditi_Seth on January 26, 2010 14:29

    You may reach the columns using decode:

    Select sum (decode (to_char (c.protection_effective, 'MM'), 1, c.dp_monthly_premium, 0)) January
    , sum (decode (to_char (c.protection_effective, 'MM'), 2, c.dp_monthly_premium, 0)) February

    etc.

    At least, this will improve efficiency, because you need only make one pass.

    no guarantee that the media are quite right - I did not execute the query, but I think that you should get the idea!

  • Need help with italics for the ePub and InDesign CS5

    Can anyone one help out me. I can't find the answer to this and I know it must be simple, I just lack.

    I create an ePub from InDesign CS5. All I want to do is have the italics that are displayed in the text box in InDesign, see the in the epub file.

    I prefer not to say what I did (I think I've tried everything, but obviously I got them all wrong, so no point listing them) and I hope that someone can make a simple guide, step by step how to make italics appear in the ePub file.

    Sorry if this question is under everyone, it's really annoying that I can't figure this on my own.

    Yes unfortunately sometimes does. ID is really a ship in order to get the content in epub format.

    You must go through your CSS for every job.

    Addition of

    serif; or without serifs; After each occorence a police.

    also, most in CS5 importanley make sure you have font name in the top of the CSS file "" round then, otherwise it appears correctly in the Adobe Digital Editions software, actually to check the rest of the code in this a well for example:

    {@font-face}
    Police-family: Minion Pro;
    make-style: normal;
    make-weight: normal;
    SRC:URL("fonts/MinionPro-regular.OTF");
    }

    I really only the minimum required in ID and do the rest in dreamweaver

  • Need help with SQL/PL/SQL for Dates

    Hi Experts - need help with a SQL query.

    I need to insert some date fields in a table called CALENDAR_PERIOD.

    Current data in CALENDAR_PERIOD table with their data types:

    STARTPERIOD (DATE) YEAR (NUMBER) PERIOD_LABEL (Varchar2 255)

    02/11/2014 2014 2014/02/11 SUN

    03/11/2014 2014 14/03/11 MON

    04/11/2014 2014 11/04/14 MAR

    I have to increment above values up to the year 2025. I don't know how to write SQL and increment of these values.

    Ex: My next value should insert: 05/11/2015 2014 11/05/14 WED like that I need to insert data until 12 31, 2025.

    Can you please help me with PL/SQL block?

    Really appreciate your help!

    DB version:

    Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

    PL/SQL Release 11.2.0.3.0 - Production

    CORE Production 11.2.0.3.0

    AMT for IBM/AIX RISC System/6000: Version 11.2.0.3.0 - Production

    NLSRTL Version 11.2.0.3.0 - Production

    Thank you
    Sandy

    Hello Sandy,

    Maybe something like

    INSERT INTO calendar_period (startperiod, year, period_label)

    SELECT DATE '' 2014-11-04 + LEVEL

    , TO_NUMBER (TO_CHAR (DATE '' 2014-11-04 + LEVEL, "YYYY"))

    , TO_CHAR (DATE '' 2014-11-04 + LEVEL, "MM/DD/YY DY")

    OF the double

    CONNECT BY LEVEL<= date="" '2025-12-314="" -="" date="">

    ;

    ((mais je ne comprends pas pourquoi nous créons une telle table "année" et "period_label" peuvent être calculé à partir de startperiod))

    Best regards

    Bruno Vroman.

  • Need help with Sql Tunning

    Under Update taking 2 h complete 3000 sets.

    UPDATE ARR_TRANSACTIONS ARR

    SET = NUMBER99

    (SELECT ARR_1.NUMBER99

    OF ARR_TRANSACTIONS ARR_1, ARR_HEADER BH

    WHERE ARR_1.ARR_ID = BH. ARR_ID

    AND BH. FLAG = "Y".

    AND ARR_1.LINE_ID = ARR. ORIG_INV_LINE_ID

    AND ARR_1.NUMBER9 IS NOT NULL

    AND ROWNUM = 1)

    WHERE ARR BATCH_ID = 26219

    AND ARR. NUMBER99 IS NULL

    I added more some condition where to reduce cost and its working fine. But I need to rise more than performance on SQL.

    Please help with SQL.

    I do not see the number of 3000 lines in the plan - so I guess that the optimizer does not compute with the correct numbers. Could generate the plan with dbms_xplan.display_cursor? This could tell us something about filter predicates and access.

    In the given situation, I would check:

    • is the access to the index in step 1 (filtered by the relevant conditions: this could be BATCH_ID = 26219) return actually 248 entries (or something similar)?
    • Access table in step 2 returning 204 lines (or something similar) after applying the filters given? If the number of lines is much bigger then a Nested Loops is perhaps not a good idea.

    To make this much simpler check, you could create a plan with statistics of content (RowSource) given that the plan containing the real and the number of estimated rows.

    If the estimated cardinalities are not plausible the next question would be if the statistics are strong and up to date.

  • Need help with coding for HTML5 with emergency Flash video

    Hello, need help with my coding in Dreamweaver CS5.5 for HTML5 with emergency Flash video. Not sure if the coding is correct. Do I need any other JavaScript etc.?

    code.jpg

    Another thing is that you don't have to include SWF as aid manual if you provide all 3 ogv, webm & mp4 video formats as one of these formats will certainly be played in any browser that is fully compatible HTML5.

    Still one thing you could do if you don't mind hosting your video on Youtube is to get an embed code for Youtube - in this way, you don't have to convert all the files. YouTube will do it for you and it will play on devices of HTML5 as well as supports Flash player of relief for devices that cannot play HTML5.

  • Need help with auto-fill the fields

    Hello

    I need help with a form that has multiple pages.  I have some areas I are duplicated in my document.  How I got these fields auto fill throughout the form/paper? For example, I'm going to fname lname, address, telephone, etc... How can I get these fields automatically on the other pages of the form?  Is this possible?  Any suggestions are welcome.

    Thank you in advance.

    If it's just a fill operation then make sure the fields have the same name. Then set the binding to the global level and all the fields with the same name will get the same value.

    Paul

  • Accidentally, I purchase an upgrade when I was trying to buy Acrobat Pro. I need help in Exchange for the upgrade to what I need.

    I can't find a certain number of support the customer to solve this problem.

    You can contact the Support from Adobe using link below:

    Contact the customer service

    [Click on always need help button and select the Chat option]

  • Need help with SQL selection ID when the sequence does not match...

    I have the following dilemma:
    Database identifiers as follows:
    Incident #, case #, & sequence manufacturer
    example of
    Record 1
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 1

    Worksheet 2
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 2

    Sometimes the user will delete (say) 2 Record after creating a new Record 3
    The sequencing will therefore now as follows:

    Record 1
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 1

    Worksheet 2
    Incident_Number = 123456
    Case_Number = 1
    Part_sequence = 3

    Now it is no longer a Part_Sequence 2

    Need a SQL to select all records where the sequence of the maximum part > than Incident_number County | » -'|| Case_number

    I tried the following:
    select a.incident_number||'-'||a.case_number||'-'||a.part_sequence
    from chsuser.a_compl_summary a
    where a.entry_date >= '01-may-2011'
    and max(a.part_sequence) > count(distinct a.incident_number||'-'||a.case_number)
    I end up getting an ORA-00934: Group feature is not allowed here (emphasis on the portion (a.part_sequence) Max.

    Advice/suggestions

    Thank you
    select  incident_number || '-' || case_number || '-' || part_sequence
      from  (
             select  incident_number,
                     case_number,
                     part_sequence,
                     max(part_sequence) over(partition by incident_number,case_number) max_seq,
                     count(*) over(partition by incident_number,case_number) cnt
               from  chsuser.a_compl_summary
               where entry_date >= DATE '2011-05-01'
            )
      where cnt != max_seq
    /
    

    SY.

  • Need help with SQL logic to fine tune output screen

    Hi all

    I have the following query:
    select x.area_id
             , x.ORG_id
            , x. product                
            , case when x.param_info_key in (400, 410, 420, 430, 440)
                         then x.new_value
                end as facility_average_price
            , case when x.param_info_key in (660, 670,680,690,700)
                         then x.new_value
                end as royalty_rate_adjustment     
    from
    (
         select d.area_id
                 , d.ORGANIZATION_NUMBER as org_id
                 , d.PARAM_INFO_KEY
                 , case when d.param_info_key in (400, 660)
                                      then 'Gas'
                          when d.param_info_key in (410, 700)  
                                  then 'Ethane' 
                          when d.param_info_key in (420, 680) 
                                  then 'Propane'
                          when d.param_info_key in (430, 690) 
                                  then 'Butane'     
                          when d.param_info_key in (440, 670) 
                                  then 'Pentane'                                                                               
                    end as product
                 , d.new_value
                 , m.CHANGE_DATE_TIME
                 , max(m.CHANGE_DATE_TIME) over(partition by area_id, organization_number,d.param_info_key) max_change_date
         from accruals2.accrual_parm_chng_hist_detail d
               , accruals2.accrual_parm_chng_hist_master m
               , accruals2.accrual_period p
               , accruals2.accrual_param_info i
         where d.PARM_CHNG_HIST_MASTER_KEY = m.PARM_CHNG_HIST_MASTER_KEY
                and m.PERIOD_KEY = p.PERIOD_KEY
                and d.PARAM_INFO_KEY = i.PARAM_INFO_KEY
                and d.param_info_key in (400, 410, 700, 680, 660, 430, 690, 440, 670)
                and area_id = 1013
                and p.ACCOUNTING_DATE = date '2010-04-30'
    ) x
    where x.change_date_time = x.max_change_date     
    order by x.area_id, x.org_id
           , x.product,  x.PARAM_INFO_KEY
    It returns a set of data that looks like this: EDIT - sorry about the way in which the columns are looking below - they match very well when I create the post. How to format the columns and data remain aligned?
    AREA_ID     ORG_ID     PRODUCT     FACILITY_AVERAGE_PRICE                ROYALTY_RATE_ADJUSTMENT
                        
    1013          1            Butane          0.08     
    1013          1            Butane                                             0.0015
    1013          1            Ethane          0.06     
    1013          1            Ethane                                             0.003
    1013          1            Gas               -0.1     
    1013         1             Gas                                                                            0.002
    1013          1           Pentane          0.09     
    1013          1           Pentane                                             0.006
    1013          1           Propane                                             0.007
    Now I want the result should look like this:
    AREA_ID     ORG_ID     PRODUCT     FACILITY_AVERAGE_PRICE         ROYALTY_RATE_ADJUSTMENT
                        
    1013          1            Butane          0.08                              0.0015
    1013          1            Ethane          0.06                              0.003                                   
    1013          1            Gas               -0.1                                             0.002
    1013          1           Pentane          0.09                              0.006                                   
    1013          1           Propane     
    In the past, I got these results by Group on area_id ord_id and the product. Then by adding the facility_average_price and royalty_rate_adjustment. However, this time, I don't want the sum or max or whatever the number is, I want to just display as above. Is it possible to do this?

    Thank you.

    Published by: dgouin on Sep 8, 2010 06:41

    dgouin wrote:
    In the past, I got these results by Group on area_id ord_id and the product. Then by adding the facility_average_price and royalty_rate_adjustment. However, this time, I don't want the sum or max or whatever the number is, I want to just display as above. Is it possible to do this?

    You must sum/max. The reason is because you have one row for each column that you are rotating, IE. a single column is filled for each line. To overwrite all the lines into a single line, you must make an aggregate query - the sum/max will not change the values.

    ETA: If you're on 11g, you also have the option of using the PIVOT.

    Published by: Boneist on 08-Oct-2010 14:44

  • need help with typing on the keyboard for palm

    for special characters. I want to make one & and thought that I would just have to press the SHIFT key, but makes only the uppercase.

    not sure if this is the right place to ask this question, but I thought I'd ask.

    Thank you!

    For the sign & (and) you hold option (Silver) and then press on the & key.

  • 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

  • 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.

  • 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.

Maybe you are looking for