Need help with SQL statement

So I am new to SQL and ORACLE. I need to know how to do the commands below in a single statement. I did it in two here, but I have no idea how do it in one. Any ideas?

ALTER TABLE books
ADD Catcode VARCHAR2 (3);

ALTER TABLE books
ADD CONSTRAINT books_catcode_fk FOREIGN KEY (catcode)
REFERENCES (catcode) category;

Any help appreciated ^^
SQL> ed
Wrote file afiedt.buf

  1  alter table books
  2    add( catcode varchar2(3),
  3         constraint books_catcode_fk
  4            foreign key( catcode )
  5*        references category( catcode ) )
SQL> /

Table altered.

Justin

Tags: Database

Similar Questions

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

    I have a third-party site that transforms the information into a csv summary. I can't change the format, and they are not ready to give directly from db and db, so I try and then import it. I'm trying to use SQL Loader, but I can't seem to make it work. I looked in the documentation (VERY rare examples) as well as several websites and I can't find anything that can help out me.

    Control file:
    LOAD DATA
    INFILE 'Daily_Diagnostics_test.csv'
    BADFILE 'Diag_failed.csv'
    APPEND
    INTO TABLE ROAD_DIAGS
    FIELDS TERMINATED BY ","
    --WHEN VEHICLE != 'Vehicle' AND VEHICLE != 'Total'
    (VEHICLE, DRIVER, START_DATE, START_TIME, START_TZ, END_DATE, END_TIME, END_TZ, 
     TM_DRV_MINS, TM_IDLE_MINS, TM_PTO_MINS, TRIP_TM_MINS,
     FUEL_DRV_GALS, FUEL_IDLE_GALS, FUEL_PTO_GALS, FUEL_TRIP_GALS, FUEL_START_GALS,
     MPG, DRV_MPG, START_ODOM, END_ODOM, DIFF_ODOM, AVG_MPH, MAX_MPH, MAX_RPM,
     DATE1, TM1, TZ1, ADDRESS_NUM1, ADDRESS_STREET1, ADDRESS_ST1, ADDRESS_ZIP1, ADDRESS_COUNTY1,
     DATE2, TM2, TZ2, ADDRESS_NUM2, ADDRESS_STREET2, ADDRESS_ST2, ADDRESS_ZIP2, ADDRESS_COUNTY2,
     STOP_CT, FAULT_CODE, HARD_BRAKE)
    Note that the line WHEN is commented out, because I can't make it work.

    and here is a sample of the data:
    Vehicle,Driver,Start Date,Start Time,Timezone,End Date,End Time,Timezone,Driving (hh:mm),Idle (hh:mm),PTO (hh:mm),Trip (hh:mm),Driving (gals),Idle (gals),PTO (gals),Trip (gals),Before Trip (gals), MPG,Driving MPG,Start,End,Trip Distance (miles),Average(MPH),Maximum(MPH),Maximum RPM(rpm),Date,Time,Timezone,Address,Street,City,State,Zip,County,Date,Time,Timezone,Address,Street,City,State,Zip,County,Stop Count,Fault Code,Hard Brake,
    26033, ,01/23/08,12:40 AM,MST,01/23/08,12:42 AM,MST,0,3,0,3,0,0.12,0,0.12, ,0,0,295829.22,295829.22,0,0,6.84, ,01/23/08,12:23 AM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,01/23/08,12:23 AM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,0,4, ,
    26033, ,01/23/08,12:43 AM,MST,01/23/08,04:03 AM,MST,126,74,0,200,0,0,0,0, ,0,0,295829.22,295914.97,85.75,25.72,67.11, ,01/23/08,12:23 AM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,01/23/08,03:56 AM,MST,0,COUNTY ROAD 125,SIDNEY,MT,59270,RICHLAND,8,3, ,
    26033, ,01/23/08,04:03 AM,MST,01/23/08,12:29 PM,MST,317,190,0,507,0,0,0,0, ,0,0,295914.97,296139.9,224.94,26.64,65.87, ,01/23/08,03:56 AM,MST,0,COUNTY ROAD 125,SIDNEY,MT,59270,RICHLAND,01/23/08,12:29 PM,MST,756,BELL RD,SIDNEY,MT,59270,RICHLAND,14,5, ,
    Total, , , ,443,267,0,710,0,0.12,0,0.12, ,0,0,295829.22,296139.91,310.69,26.38,67.11, , , , , , , , , , , , , , , , , , , ,22,12,0,
    Vehicle,Driver,Start Date,Start Time,Timezone,End Date,End Time,Timezone,Driving (hh:mm),Idle (hh:mm),PTO (hh:mm),Trip (hh:mm),Driving (gals),Idle (gals),PTO (gals),Trip (gals),Before Trip (gals), MPG,Driving MPG,Start,End,Trip Distance (miles),Average(MPH),Maximum(MPH),Maximum RPM(rpm),Date,Time,Timezone,Address,Street,City,State,Zip,County,Date,Time,Timezone,Address,Street,City,State,Zip,County,Stop Count,Fault Code,Hard Brake,
    26035, ,01/23/08,03:59 AM,MST,01/23/08,09:05 AM,MST,222,85,0,306,50.87,1.5,0,52.37, ,3.32,3.42,285056.53,285230.5,173.98,34.07,68.97, ,01/23/08,03:24 AM,MST,756,BELL RD,SIDNEY,MT,59270,RICHLAND,01/23/08,09:00 AM,MST,0,COUNTY ROAD 338,SAVAGE,MT,59262,RICHLAND,7,3, ,
    26035, ,01/23/08,09:06 AM,MST,01/23/08,03:37 PM,MST,273,118,0,391,68.5,1.5,0,70, ,3.24,3.31,285230.5,285457.3,226.8,34.8,69.59, ,01/23/08,09:00 AM,MST,0,COUNTY ROAD 338,SAVAGE,MT,59262,RICHLAND,01/23/08,03:37 PM,MST,756,BELL RD,SIDNEY,MT,59270,RICHLAND,11,3, ,
    26035, ,01/23/08,03:45 PM,MST,01/23/08,04:38 PM,MST,32,21,0,53,7.37,0.25,0,7.62, ,3.42,3.54,285457.3,285483.4,26.1,29.73,67.73, ,01/23/08,03:37 PM,MST,756,BELL RD,SIDNEY,MT,59270,RICHLAND,01/23/08,04:33 PM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,2,3, ,
    26035, ,01/23/08,04:49 PM,MST,01/23/08,10:22 PM,MST,256,76,0,332,52,1.25,0,53.25, ,3.71,3.8,285483.4,285681.0,197.6,35.67,64, ,01/23/08,04:33 PM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,01/23/08,10:17 PM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,9,3, ,
    26035, ,01/23/08,10:25 PM,MST,01/23/08,10:35 PM,MST,4,5,0,10,0.37,0,0,0.37, ,1.68,1.68,285681.0,285681.62,0.62,3.9,24.85, ,01/23/08,10:17 PM,MST,0,COUNTY ROAD 108,SAVAGE,MT,59262,RICHLAND,01/23/08,10:35 PM,MST,0,COUNTY ROAD 338,SAVAGE,MT,59262,RICHLAND,1,5, ,
    Total, , , ,787,305,0,1092,179.11,4.5,0,183.61, ,3.4,3.49,285056.53,285681.62,625.1,34.35,69.59, , , , , , , , , , , , , , , , , , , ,30,17,0,
    As you can see, I need to have the SQL Loader to ignore the header and summary lines that appear for each grouping. Your suggestions are welcome as I need to implement this in an automatic process, I can call with a batch script.

    blarman74 wrote:
    Can anyone suggest how to fix this problem? The log file shows this error:

    Record 8: Rejected - Error on table ROAD_DIAGS, column DATE2.
    ORA-01858: a non-numeric character was found where a numeric was expected
    

    The error message is clear - you try to load a value "non-date" in a DATE column and, therefore, sql * loader berms.
    That points to a discrepancy between the list of columns in the control file and, in the data file.
    Note the position of the table of the "DATE2" column in the table "ROAD_DIAGS":

    test@XE>
    test@XE> l
      1  select table_name,
      2         column_name,
      3         column_id
      4    from user_tab_columns
      5   where table_name='ROAD_DIAGS'
      6*    and column_name='DATE2'
    test@XE> /
    
    TABLE_NAME      COLUMN_NAME      COLUMN_ID
    --------------- --------------- ----------
    ROAD_DIAGS      DATE2                   34
    
    test@XE>
    

    If your control file specifies that the 34th column in the data file must be "DATE2". However-

    test@XE>
    test@XE> -- display the line number and the 34th column of the data file
    test@XE>
    test@XE> !perl -ne '{@a=split/,/; print $.,"\t",$a[33],"\n"}' Daily_Diagnostics_test.csv
    1       County
    2       RICHLAND
    3       RICHLAND
    4       RICHLAND
    5
    6       County
    7       RICHLAND
    8       RICHLAND
    9       RICHLAND
    10      RICHLAND
    11      RICHLAND
    12
    
    test@XE>
    

    the 34th column in your data file is 'COUNTY', which is counted in your control file.

    Here is the list of the columns for 29 to 37 positions in the csv file:

    test@XE>
    test@XE> !perl -ne '{if ($. == 1) {@a=split/,/; foreach $i (28..36) {print $i+1,"\t",$a[$i],"\n"}}}' Daily_Diagnostics_test.csv
    29      Address
    30      Street
    31      City
    32      State
    33      Zip
    34      County
    35      Date
    36      Time
    37      Timezone
    
    test@XE>
    test@XE>
    

    And here are the columns specified in the control file:

      ...
      ...
      ADDRESS_NUM1,
      ADDRESS_STREET1,
      ADDRESS_ST1,
      ADDRESS_ZIP1,
      ADDRESS_COUNTY1,
      DATE2            DATE "MM/DD/YYYY",
      TM2              DATE "HH:MI AM",
      TZ2,
      ...
      ...
    

    (You can open the csv file in MS Excel to get a better picture of the data in there).

    As seen above, due to the incompatibility, sql * loader loads 'City' in ADDRESS_ST1, 'State' in ADDRESS_ZIP1, "Zip" in ADDRESS_COUNTY1 and DATE2 "County" - where it error.

    The solution would be-

    (a) If you want to load the 'City' information from the data file, and then add the "City" column in the table (if nonexistent) and specify in the control file.

    (b) If you do not want to load the 'City' information from the data file, you can specify a column to 'FILL' in the file of control as follows:

      ...
      ...
      ADDRESS_NUM1,
      ADDRESS_STREET1,
      city1            FILLER,
      ADDRESS_ST1,
      ADDRESS_ZIP1,
      ADDRESS_COUNTY1,
      DATE2            DATE "MM/DD/YYYY",
      TM2              DATE "HH:MI AM",
      TZ2,
      ADDRESS_NUM2,
      ADDRESS_STREET2,
      city2            FILLER,
      ADDRESS_ST2,
      ADDRESS_ZIP2,
      ADDRESS_COUNTY2,
      ...
      ...
    

    Hope that helps,
    isotope

  • 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 - 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, and bash

    Hi guys,.

    My main problem/situation is when sqlplus returns no data (no message selected lines)

    Bash shell script condition

    echo $open_items

    15 if [[$open_items == "0"]]

    16. can

    echo 17 0

    18 else

    19 1 echo

    20 fi

    and real sql is as follows:

    elect the count (*) a table with a condition.

    I get the output as follows:

    + open_items ='

    COUNT (*)

    ----------

    0'

    + echo 'COUNT (*)'---0

    COUNT ()) - 0

    + [[

    COUNT (*)

    ----------

    0 == \0 ]]

    + echo 1

    1

    What to do so that I can get a number in the output of sql is my question because the condition of shell script is based entirely on sqlplus out. I tried set feedback off, but it does not help.

    Any help would be much appreciated!

    SQL lover wrote:

    OK I try it and give you feedback

    In fact, I take out execution of sql into a variable

    MyVar = $(sqlplus-s/as sysdba)<>

    Set echo off feedback off verify off trimsp head off pages 0

    Select count (*) from dba_users;

    output

    EXPRESSIONS OF FOLKLORE)

    echo myvar is $myvar

  • Need help with SQL

    Hi all

    Database Version: 10g

    I have a situation where, as below:

    < select recipes

    Since then (select max (version_number), recipes

    of pa_budget_versions

    where project = 2304285

    and budget_status_code = 'B '.

    "trunc (creation_date) between 1 March 2014 ' and 30 March 2014"

    Income group) >

    I want to create a function with the above query and to return the amount of income.


    My query above returns the amount and date of data and the end of the beginning will be the settings I will pass. But if the query does not return the recipes for the start date and the end date, I want to go back a month and check if there are recipes, like this I wish to go back until I get the income.


    "Example: if I run the query for March 1, 2014 'and 30 March 2014 ' amount no., I want to continue and run to February 1, 2014' and 28 February 2014" and so on, continue until I get the income.


    Could you please help me to get this. Thank you.

    Thanks and greetings

    Srinivas

    Hello

    user2626293 wrote:

    ... could you please explain to me how does this WITH clause and how do you know that in a given situation, we have to use WITH the clause. ...

    I'm not sure that understand the question.

    A WITH clause that is very similar to a view.  Both allow you to use the result set of a query in the table.

    How can you know that when a WITH clause that is necessary?  As I explained in answer #22 above, a WITH clause that is rarely necessary; in most situations, you can use an online, or a regular view, instead.

    You want to know when a WITH clause or view online or a regular view is necessary?  Whenever you need to use the result set of a query as if it were a table.

    A set of common situation when you want to use the result of a query as if it were a table when you want to use the same calculation multiple times in a query.  In this example, I wanted to use the last_creation_date more than once.  I could have repeated the same exact calculation each time that I needed it, but it would be tedious, and it would make the code difficult to maintain and probably ineffective.  Instead, I calculated it once, in the subquery, I called got_last_creation_date.

  • need help with sql queries

    Dear all,

    I have a parameter like below

    11000048,11000050

    I need to convert in SQL format below, please help me

    "11000048","11000050"


    ' parameter would be so separated by commas, I need to convert in above format, if the parameter is a number say 1111111 must convert it as "1111111" in sql.


    Please help me.


    Thank you

    You can do it

    your_parameter: = "" ' | " Replace (your_parameter, ",", "",""). '"';

  • Need help with SQL strings

    Team,

    I have a requirement in my project where I need to subscribe to a channel of sup of the main chain and store them in the other column.

    for example,.

    I have a table with 2 columns and table has more than 500 discs.

    1st column has values as follows

    "High pressure 0.5 in.

    "Plate 0.3 in.

    ' Health 3.0 in.

    "" 8721 hygienic 1.0 in ""

    My requirement is that I have to take inches part and store in the 2nd column.

    my 2nd column must contain values such as 0.3, 0.5, 1.0, 0.3

    Thanks in advance,

    RAK

    Hey, Rak,

    This sounds like a job for regular expressions, particularly REGEXP_SUBSTR.

    Exactly how depends on your specific needs.  If the number always consists of 1 or more digits on either side of a decimal point, then

    REGEXP_SUBSTR (str

    , '\d+\.\d+'

    )

    Returns the number of the largest Dim Str as string.

    I hope that answers your question.

    If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.

    Point where the above expression is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places.

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

  • 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;
    
  • Need help with update statement to be used in the stored procedure

    I have the following update statement and I get "ORA-06512. Someone of you can help me.

    ----
    UPDATE MO X SET X.MOMODEL = (SELECT Y.MOMODEL FROM MO Y WHERE Y.MOID = A_TEMP_INT3)
    WHERE X.MOID in (UPDATE MO X SET X.MOMODEL = (SELECT Y.MOMODEL FROM MO Y WHERE Y.MOID = A_TEMP_INT3)
    WHERE X.MOID in (A_TEMP_INT1, (select assocchild from mo_association
    Connect prior assocchild = assocparent
    Start with assocparent = A_TEMP_INT1)); (by selecting assocchild in mo_association
    Connect prior assocchild = assocparent
    Start with assocparent = A_TEMP_INT1));
    ----

    Note: A_TEMP_INT1 and A_TEMP_INT3 are the values of cursor and is assigned to numeric values in the code.


    Thanks for all the help in advance.

    Kind regards
    REDA
    WHERE X.MOID IN
     (SELECT ASSOCCHILD   FROM MO_ASSOCIATION
          CONNECT BY PRIOR ASSOCCHILD = ASSOCPARENT
         START WITH ASSOCPARENT = A_TEMP_INT1
         UNION
      SELECT A_TEMP_INT1 FROM DUAL );
    

    Should work.
    VR,
    Sudhakar B.

  • 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 Server 2008 R2 deadlock uninstall process

    I installed SQL Server 2008 R2 trial version on my PC running Windows Vista Ultimate, a day ago, but he placed himself completely in the disk partition (E :) that I specified. [i.e. I found some parts of the installation in my C:\program files as well as a USB as well].

    I tried to uninstall SQL Server 2008 R2, but was only partially successful.  I'm prevented from uninsalling the main parts using the programs and features Panel - that is "Microsoft SQL Server 2008 R2" and "Microsoft SQL Server 2008 R2 Setup (in English).

    Still visible are:

    1 C:\Program Files (x 86) \Microsoft SQL Server

    2 C:\Program Files (x 86) \Microsoft Visual Studio 9.0

    3 C:\ProgramData\Microsoft\Windows\Start demarrer\programmes\microsoft SQL Server 2008 R2

    I would like your help to uninstall the other goals of SQL Server, so that I can do a clean install once more.

    Any advice on how SQL Server 2008 R2 evaluation version must be correctly installed what a future unistall is not messy, would be welcome.

    Hello

    Your question of Windows 7 is more complex than what is generally answered in the Microsoft Answers forums. It is better suited for the SQL Server forums. Please post your question in the SQL Server forums.

    http://social.msdn.Microsoft.com/forums/en/category/SQLServer

Maybe you are looking for

  • Can alternative text applies paragraphs, tabs, styles?

    1. is it possible to apply paragraph returns, or tabs or text styles to text in settings to create alternative text keyboard shortcuts, for example, an address 2 line: 123 Main Street New York, NY 100012 In Word, for example or some macro programs, u

  • SA60-106 pointing device

    I have one of the above, and it was recently damaged, sent to a Toshiba authorized service for repair center and returned, works OK for two days, after which the pointing device no longer works. A USB mouse works OK, but when I look on Device Manager

  • HP envy 6-1202eo: Windows 8/10, 0xc0000034 error.

  • Help synchronize my LG mytouch Q to my PC

    Original title: device I'm having a problem to sync my LG mytouch Q to my pc. It is not read my phone when iI hang it upward through my usb cable.

  • problems with loading iTunes

    The installer has sufficient privileges to modify this file: C:\program Files\iTunes\acknowledgements.rtf I tried to reinstall itunes but it wont let me no more, I can't remove the program either "Fatal error during installation" someone has seen thi