SELECTing records from two tables. Set Operators, CASE, DECODE, ...

Hi all,
I have two tables:
CRETE TABLE T1 (T1_COL1 NUMBER, T1_COL2 NUMBER)
CRETE TABLE T2 (T2_COL1 NUMBER, T2_COL2 NUMBER)
T1 may or may not have records. T2 always has records. There are two scenarios.
Scenario 1:
=======
SELECT * FROM T1 returns five rows, and SELECT * FROM T2 returns 10 rows.
Now I need the five rows from T1.
Scenario 2:
=======
SELECT * FROM T1 returns zero rows, and SELECT * FROM T2 returns 10 rows.
Now I need the 10 rows from T2.
In other words, if records present in T1, I need them all. If not, I need records from T2.
There are no common columns (for joins).
Now need a single query to achive this. I tried set operators, CASE and DECODE. But I'm unable to solve it.
Please help. Thanks in advance.

Iniyavan wrote:
Yes, Justin. I'm sure that this is the way it's modelled. I also find it's tough and odd.Are you sure it's the right way for that data to be modeled? I understand that's the way it is being modeled, but a data model change may be the best option.
Is there any other way, which is simpler, without using RANK?That's the simplest option I can think of. You could also do something like
SELECT t1_col1, t1_col2
  FROM (
    SELECT t1_col1, t1_col2, rownum rn
      FROM (
        SELECT t1_col1, t1_col2
          FROM (
            SELECT t1_col1, t1_col2, 1 tbl
              FROM t1
            UNION ALL
            SELECT t2_col2, t2_col2, 2 tbl
              FROM t2       
         ORDER BY tbl
WHERE rn = 1I'm not sure that's any simpler...
Justin

Similar Messages

  • Can i select Records from two tables into an itab.

    Hi,
        Suppose that i have two tables spfli and sflight.
        Now i want the records from both the tables into a single internal table.
        If so please let me know how this can be done.
        itab should consist of all the columns in sflight and spfli.
    Regards,
    Sai

    Sai,
    Yes u can do it using JOINs.
    Use this link for ref
    http://www.sap-img.com/abap/inner-joins.htm
    INNER JOIN results are an intersection of the tables being joined where in only if both the tables havethe data the result is pused onto the result set.
    WHERE as in LEFT OUTR JOIN you can push the data of the LEF T table on the resultset even when the join condition is not met.
    The use is that you wantto have all the data that is there in left table and also the right table if the join condition is a success then right table fileds will have data else they are initial.
    OUTJOIN's are used in MAINTENANNCE VIEWS, HELP VIEWS .
    INNER JOINS are used DATABSE VIEWS.
    Inner Join:-
    SELECT pcarrid pconnid ffldate bbookid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM ( ( spfli AS p
    INNER JOIN sflight AS f ON pcarrid = fcarrid AND
    pconnid = fconnid )
    INNER JOIN sbook AS b ON bcarrid = fcarrid AND
    bconnid = fconnid AND
    bfldate = ffldate )
    WHERE p~cityfrom = 'FRANKFURT' AND
    p~cityto = 'NEW YORK' AND
    fseatsmax > fseatsocc.
    Left Outer Join
    SELECT scarrid scarrname p~connid
    INTO CORRESPONDING FIELDS OF TABLE itab
    FROM scarr AS s
    LEFT OUTER JOIN spfli AS p ON scarrid = pcarrid AND
    p~cityfrom = 'FRANKFURT'.
    Reward if helpful,
    Karthik

  • Selecting data from two tables

    I am trying to select data from two tables.  The only problem that I am running into, is that i am only seeing results from my 'uploads' table.  there is also a record in documents where user = 1 that should show up.  here is my sql:
    $userIDNum = 1;
    $sql="Select *
    from uploads, documents
    WHERE uploads.user = documents.user AND uploads.user = $userIDNum
    ORDER BY uploads.title ASC, documents.title ASC";

    You'll need to explain a little more about your data and what you are trying to accomplish. Your current sql will select all columns from both the uploads and documents tables but only rows where the user id in both tables match, AND the user id equals 1. Is that not what you are seeing? Where's the code that outputs the results?

  • Select data from two tables...!

    HI Experts...!
    i m a beginner user and i want to select data from two tables proj and prps.....using joins.....and internal tables i have written a code...
    SELECT prps~pspnr
           prps~objnr
           prps~psphi
           proj~ernam
           proj~erdat
           proj~pspnr
    INTO  table itab   -
    itab is internal table
    FROM prps inner join proj
    WHERE pspnr in p_no and prpspsphi = projpspnr.
    but there is error in from clause ..please help me....
    Advance thanx....

    Hi,
    check the sample code bellow above two reply will solve out your problem but one more extra line in your code pointed out bellow.
    TABLES: prps, proj.
    TYPES:  BEGIN OF ty_test,
            pspnr LIKE prps-pspnr,
            objnr LIKE prps-objnr,
            psphi LIKE prps-psphi,
            ernam LIKE proj-ernam,
            erdat LIKE proj-erdat,
            END OF ty_test.
    DATA: itab TYPE STANDARD TABLE OF ty_test WITH HEADER LINE.
    SELECT-OPTIONS: p_no FOR prps-pspnr.
    SELECT  prps~pspnr
            prps~objnr
            prps~psphi
            proj~ernam
            proj~erdat
    *        proj~pspnr " No need for this you have selected this in
    *     the first line because it is commone so you only need to select from any one
            INTO TABLE itab
    FROM prps INNER JOIN proj ON ( prps~pspnr = proj~pspnr  )
    WHERE prps~pspnr IN p_no.
    Best Regards,
    Faisal
    Edited by: Rob Burbank on Dec 24, 2009 12:24 PM

  • Help with listing records from two tables

    Hi: I have two tables joined by the first field. The field is primary key in first table. Need help listing records from both tables with each record one line/record.
    create table EVENTS (
    event_key varchar2(64) primary key,
    event_description varchar2(64),
    create_time int
    create table EVENT_UPDATES (
    event_key varchar2(64) NOT NULL ,
    update_description varchar2(64),
    update_time int
    insert into EVENTS values('Event1', 'This is event1', 1);
    insert into EVENT_UPDATES values('Event1', 'Ticket created', 3);
    insert into EVENT_UPDATES values('Event1', 'Event cleared', 10);
    insert into EVENTS values('Event2', 'This is event2', 4);
    insert into EVENT_UPDATES values('Event2', 'Ticket created', 6);
    insert into EVENT_UPDATES values('Event2', 'Event cleared', 8);I want to print each record in EVENTS table as one line and corresponding records in EVENT_UPDATES as one line/record like this
    Event1   1     This is event1
                3     Ticket created
                10   Event cleared
    Event2   4     This is event2
                6     Ticket created
                8     Event clearedTIA
    Ravi

    select  case weight
              when 1 then event_key
            end key,
            time_val,
            description
      from  (
              select  event_key,
                      create_time time_val,
                      event_description description,
                      1 weight
                from  events
             union all
              select  event_key,
                      update_time,
                      update_description,
                      2 weight
                from  event_updates
      order by event_key,
               weight
    KEY          TIME_VAL DESCRIPTION
    Event1              1 This is event1
                        3 Ticket created
                       10 Event cleared
    Event2              4 This is event2
                        6 Ticket created
                        8 Event cleared
    6 rows selected.
    SQL> SY.

  • Selecting columns from two table is slow but same

    I am selecting 27 columns from two tables
    which running for more than 30 minutes. but
    if I select count(*) with the same query
    except the columns it is coming in seconds.
    Where is the error?

    If you post
    1) The table definitions for the underlying tables
    2) The indexes that are on the tables
    3) The two SQL statements you're running
    4) The explain plan for both statements
    we can probably be of some assistance.
    My guess is that the count(*) is able to return much more quickly because the optimizer is able to use a significantly faster query plan that is based on an index which the longer-running query cannot utilize. Without the information I've requested, though, it's hard to do more than speculate.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • How do you Select data from two tables with similar data amd merge the output together.

    I have two Tables containing Sales Data. I want to read the Table a sort by date and accumulate dollars by order date. Then I want to read the second table and accumulate these dollar amounts by date and then merge the records together so that I gave 1 row
    with amounts for type A and amounts for type b.
    Here are the tables I am looking at.
    Select Cast(J.Order_Date As Varchar(11))) As [Order Date]
              ,Sum(Case when Sales_Code like '%Comm%' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Comm]
              ,Sum(Case when Sales_Code = '5-Day' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
              ,Sum(Case when Sales_Code like '%Auto%" then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
              ,Sum(Case when Sales_Code = '' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Fixed]
              ,Sum(Case when Sales_Code = 'XX' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Comm)
              ,Sum(Case when Sales_Code = 'YY' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Auto)
              ,Sum(Case when Sales_Code = 'ZZ' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Fixed)
    from [PRODUCTION].dbo.Job As J
    union all
    Select Cast(SH.Order_Date As Varchar(11))) As [Order Date]
              ,Sum(Case when Sales_Code like '%Comm%' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Comm]
              ,Sum(Case when Sales_Code = '5-Day'     then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
              ,Sum(Case when Sales_Code like '%Auto%" then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
              ,Sum(Case when Sales_Code = ''          then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Fixed]
              ,Sum(Case when Sales_Code = 'XX' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Comm)
              ,Sum(Case when Sales_Code = 'YY' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Auto)
              ,Sum(Case when Sales_Code = 'ZZ' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Fixed)
    from [PRODUCTION].dbo.SO_Detail As SD
    Inner Join [PRODUCTION].dbo.SO_Header As SH
        on SD.Sales_Order = SH.Sales_Order
    Group by J.Order_Date
    Order by J.Order_Date Desc
    Looking for output like
    Order Date   Job Comm   Job AUto   Job Fixed    SO Comm  SO AUto  SO Fixed
    Mar-11-2014    100.00     250.00       50.00     200.00   300.00    400.00
    Mar-10-2014    500.00     340.00        0.00     110.00   400.00    500.00
    Mar-09-2014    600.00     333.00       56.00     210.00   500.00    300.00
    Thanks for your help
    SWProduction

    Seeing the output it looks like what you need is this
    select COALESCE(p.[Order Date],q.[Order Date]) AS [Order Date],
    COALESCE([Job Comm],0) AS [Job Comm],
    COALESCE([Job AUto],0) AS [Job AUto],COALESCE([Job Fixed],0) AS [Job Fixed],COALESCE([SO Comm],0) AS [SO Comm],COALESCE([SO AUto],0) AS [SO AUto],COALESCE([SO Fixed],0) AS [SO Fixed]
    from
    Select Cast(J.Order_Date As Varchar(11))) As [Order Date]
    ,Sum(Case when Sales_Code like '%Comm%' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Comm]
    ,Sum(Case when Sales_Code = '5-Day' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
    ,Sum(Case when Sales_Code like '%Auto%" then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Auto]
    ,Sum(Case when Sales_Code = '' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [Job Fixed]
    ,Sum(Case when Sales_Code = 'XX' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Comm)
    ,Sum(Case when Sales_Code = 'YY' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Auto)
    ,Sum(Case when Sales_Code = 'ZZ' then (J.Order_Quantity * J.Unit_Price) Else 0 end) As Decimal(11,2) As [SO Fixed)
    from [PRODUCTION].dbo.Job As J
    )p
    full join
    Select Cast(SH.Order_Date As Varchar(11))) As [Order Date]
    ,Sum(Case when Sales_Code like '%Comm%' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Comm]
    ,Sum(Case when Sales_Code = '5-Day' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
    ,Sum(Case when Sales_Code like '%Auto%" then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Auto]
    ,Sum(Case when Sales_Code = '' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [SO Fixed]
    ,Sum(Case when Sales_Code = 'XX' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Comm)
    ,Sum(Case when Sales_Code = 'YY' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Auto)
    ,Sum(Case when Sales_Code = 'ZZ' then SD.Ext_Amt Else 0 end) As Decimal(11,2) As [Job Fixed)
    from [PRODUCTION].dbo.SO_Detail As SD
    Inner Join [PRODUCTION].dbo.SO_Header As SH
    on SD.Sales_Order = SH.Sales_Order
    Group by J.Order_Date
    )q
    on p.[Order Date] = q.[Order Date]
    Order by COALESCE(p.[Order Date],q.[Order Date]) Desc
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to pass selected records from one table to another ?

    Hi,
    In my view i have designed a table with certain records.  I need to pass only specific records of this table to another table which been designed in another view. Can anybody please give sum idea for this.
    Rgds
    Sudhanshu

    hi,
    Refer the below  code:
    1. I have selected some data from the table.
    2. The selected data is moved to some other internal table.
    3. Internal table is further binded to the node in which data is to be shown.
    data : lo_nd type ref to if_wd_context_node,
      lo_nd1 type ref to if_wd_context_node,
      lt_temp type wdr_context_element_set,
      wa_temp type ref to if_wd_context_element,
      ls_node1 type sflight,
      lt_node1 type STANDARD TABLE OF sflight.
    lo_nd = wd_context->get_child_node('CN_MAIN').  <CN_MAIN is my node>
      CALL METHOD lo_nd->get_selected_elements  <here selected data is moved to lt_temp>
       RECEIVING
           set = lt_temp.
      loop at lt_temp INTO wa_temp.
          CALL METHOD wa_temp->get_static_attributes
          IMPORTING
            static_attributes = ls_node1.       <Selected data in work area.>
        APPEND ls_node1 TO lt_node1.  < Data moved to internal Table>
        CLEAR ls_node1.
      ENDLOOP.
    Finally Internal table is binded to the node required.
      lo_nd1 = wd_context->get_child_node('CN_MAIN2').
      lo_nd1->bind_table( lt_node1 ).
    In your case , map this CN_MAIN2 with the Component Controller and from component controller you can access data in your second view also.
    I hope it helps.
    Thanx.
    Saurav.

  • Records from two tables in different DB's

    I have two tables (in different DB's) that I need to build
    results from. I want to return results from all records in table A
    and when a matching record in table B is found, retrieve the value
    of a sqlbit field. The catch is that there may or may not be any
    records in Table B that match Table A, and there may be more than
    one record in Table B, but always only one record for Table A.
    Table A is [Order] (yes I know, a reserved word - not my
    design) and Table B is tblOrderFollowup - here is my current SQL
    statement:
    ****************BEGIN SQL*************************
    SELECT o.StoreID AS xStore
    , o.Time AS dCreated
    , o.ID AS xPurchase
    , c.FirstName AS sFirstName
    , c.LastName AS sLastName
    , c.Company AS sCompany
    , c.ID AS xCustomer
    , o.Type AS xType
    , o.Closed AS bClosed
    , o.Tax AS nTax
    , fu.OrderID AS xFUOrder
    , fu.bComplete AS xFUComplete
    FROM [Order] o
    INNER JOIN Customer c
    ON o.CustomerID = c.ID
    AND o.StoreID = c.StoreID
    LEFT OUTER JOIN db2.dbo.tblOrderFollowup fu
    ON o.ID = fu.OrderID
    AND o.StoreID = fu.StoreID
    WHERE o.StoreID IN ( #allowableStoreIDs# )
    AND o.StoreID = <cfqueryparam cfsqltype="cf_sql_integer"
    value="#FORM.StoreID#" />
    AND o.Time >= <cfqueryparam
    cfsqltype="cf_sql_timestamp"
    value="#CreateODBCDateTime(StartOfDay(FORM.dFrom))#" />
    AND o.Time <= <cfqueryparam
    cfsqltype="cf_sql_timestamp"
    value="#CreateODBCDateTime(EndOfDay(FORM.dTo))#" />
    AND o.Closed = <cfqueryparam cfsqltype="cf_sql_bit"
    value="1" />
    ORDER BY o.Time DESC
    ***********END SQL**********************
    The problem here is that when I have more than 1 record per
    OrderID/StoreID pair in Table B, I get more than one result - I
    only want one. I am guessing I need a sub query, but don't know how
    to go about doing that at all ...

    You say you want only one result. Do you know which one you
    want?
    By the way, your query is probably not giving you the right
    answer. My experience is that if you do this:
    select stuff
    from table1 left join table2 on something
    where table2.somefield = somevalue
    you get the wrong answer. What I do instead is,
    select stuff
    from table1 left join
    (select stuff from table2
    where whatever) need_an_alias on somthing

  • Delete records from two tables

    Hi All,
    I have two tables (tableA and tableB) and i have four combination primary key.
    How can I delete the records from tableA that are not in tableB?
    In my sample below I need to delete the records from year 2013.
    I have a loop to insert the new ones.
    Thanks
    Johnny
    create table tableA(
    keyA_id number,
    keyB_id number,
    keyC_id number,
    keyD_id number,
    amount  number,
    CONSTRAINT "PK_TABLEA" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2011,10);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2012,40);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2013,20);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2011,10);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2012,30);
    insert into tableA(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2013,20);
    create table tableB(
    keyA_id number,
    keyB_id number,
    keyC_id number,
    keyD_id number,
    amount  number,
    CONSTRAINT "PK_TABLEB" PRIMARY KEY (keyA_id, keyB_id, keyC_id, keyD_id)
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2011,10);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2012,40);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,1,2014,40);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2011,10);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2012,30);
    insert into tableB(keyA_id,keyB_id,keyC_id,keyD_id,amount)
    values(1,1,2,2014,30);

    r you trying to do something like below ?
    Delete from TableA A
    where not exists (select 1 from TableB B where
    A.keyA_id=B.keyA_id and
    A.keyB_id=B.keyB_id and
    A.keyC_id=B.keyC_id and
    A.keyD_id=B.keyD_id)

  • Find matching records from two tables, useing three key fields, then replace two fields in table1 from table2

    I have two tables - table1 and table2 - that have the exact same schema. There are three fields that can be used to compare the data of the two tables, field1, field2, and field3. When there are matching rows in the two tables (table1.field1/2/3 = table2.field1/2/3)
    I want to replace table1.field4 with table2.field4 and replace table1.field5 with table2.field5.
    I have worked with the query but have come up with goobly goop. I would appreciate any help. Thanks.

    If your field1, field2, and field3 combinations in these tables are unique, you
    can do a join on them.
    Select t1.field4, t2.field4 , t1.field5, t2.field5
    from table1 t1 inner join table2 t2 on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t1.field3=t2.field3
    --You can update your table1 with following code:
    Merge table1 t1
    using table2 t2 on
    on t1.field1 =t2.field1 and t1.field2=t2.field2 AND t3.field3=t2.field3
    When matched then
    Update Set
    t1.field4= t2.field4
    ,t1.field5 = t2.field5 ;

  • Selecting Records from one table where records don't exist in a linked table

    Post Author: RMC
    CA Forum: Crystal Reports
    Tables:
    Stock (Main Table)
    Transactions (Left Outer Join)
    Criteria:
    Select all items with no transactions between a selected date range.
    The items will exist in Main table but I want to select all items that haven't been in a transaction between a date range so I can identify no movement with stock items just sitting in the database doing nothing.
    Any suggestions??

    Post Author: yangster
    CA Forum: Crystal Reports
    Sorry for the delay.  Dam meetings all day yesterday.You are essentially looking for something that doesn't exist in the db so you need to look at it from a different approach.You definitely don't want to use a full outer join.  From what i can tell from your description above you are looking for stocks that are inactive/idle.So you need not look at data that doesn't exist but the last activity date.So you'd want something along the lines ofselect s.stock_name, s.stock_blah, max(t.transaction_date) Transaction_datefrom stocks sjoin transactions t on s.itemid = t.itemidwhere whatever restrictions you need to addgroup by s.stock_name, s.stock_blahthis will produce one record for each stock you have in the stock table showing you only the last date of activityput that in a command in crystalin the selection expert in crystal reports put in a simple criteriatransaction_date <= some datethis will only pull in records based on your date selection so if you say give me all records from dec 31, 2006  you will know all the records being pulled in are stocks that have been idle since dec 31, 2006 or earlierlet me know if this doesn't work

  • Select record from database table

    how to select a record from data-base table for current month in module-pool, i.e if current month is august then records selected should between 1/08/10 to 31/08/10.
    And, i also want to select records for last month i.e if current month is august then records selected should be between 1/07/10 to 31/07/10.
    And one thing more, it should work for every scenario. Like for months are changing then it should work according to that.
    Example: If month changes to October then current month ll be October and last month ll become September.
    Plz help me out.
    Moderator message: you chose a better forum this time, but the question is still too basic to be asked here, please (re)search yourself first, these forums are not catering to beginners.
    locked by: Thomas Zloch on Aug 8, 2010 9:41 PM

    Hi Nishant,
    You can use the option <b>ORDER BY </b> in your SELECT clause.
    This is the SAP Documentation for <b>ORDER BY </b> option.
    <i>Variant 1</i>
    <b>...ORDER BY PRIMARY KEY</b>
    <b>Effect</b>
    Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
    <i>Notes</i>
    Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
    <i>Variant 2</i>
    <b>ORDER BY f1 ... fn</b>
    <i>Effect</i>
    Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.
    By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.
    The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
    Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.
    REPORT zarun_1.
    DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
    START-OF-SELECTION.
      SELECT * FROM mara
                INTO TABLE it_mara
                <b>UP TO 10 ROWS                "No of rows you need(give 1 here)
               ORDER BY matnr DESCENDING.    "Specify the Key Fields here.</b>
      LOOP AT it_mara.
        WRITE : / it_mara-matnr,
                  it_mara-mtart.
      ENDLOOP.
    Regards,
    Arun Sambargi.
    Message was edited by: Arun Sambargi

  • How do I extract matching records from two tables?

    I'm trying to extract client records from our client database to put together a very targeted email campaign. As an example, the result I want is a list of names and email addresses of those clients who have a record in our system, have not had a visit in our clinic in the last year, and live within 200 miles of our clinic. I can capture the first two criteria in one extract, and the second in another. I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
    Can someone tell me if this is possible and if so, how to do it?
    I'd be very appreciative of any help, thank you.

    conejo61 wrote:
    I can then import those as tab delimited data into Numbers. What I then need to do is create a third table that represents ONLY those records that exist in both tables.
    You can create a column that generates a serial marker on the table from which you want to transfer the data, making all the names that also appear on the other table. Not that the formula will mark only exact matches.
    Here's a short example:
    Table 1 on the left, is one of the two tables imported from the tab delimited data files. It has other data, but only the names in column A are used to identify records appearing on both tables.
    Table 2 on the right, contains the names and other data (represented by the email addresses in column B), to be transferred to the third table.
    Column C of this table contains the formula that counts off the rows containing names appearing on both tables. The version below is in C2, and is filled down to the end of the column.
    =IF(COUNTIF(Table 1 :: $B,A2)>0,MAX($C$1:C1)+1,"")
    Note that jane Doe is not counted (or transferred) as her name is recorded differently on the two tables.
    Table 3 is the results table. It contains one formula for each column to be transferred from Table 2 to Table 3.
    A2:   =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $A))
    B2:   =IF((ROW()-1>MAX(Table 2 :: $C)),"",LOOKUP(ROW()-1,Table 2 :: $C,Table 2 :: $B))
    Regards,
    Barry
    EDIT: I was working in Numbers '09 when developing this, and got the following warning when I saved a copy as an iWork '08 document:
    Referencing row or column ranges that include header or footer cells isn't supported. The formula references were updated to exclude header and footer cells.
    The formulas in Table 2::C2 references C1. You may have to ensure that Table 2 does not include a header row.
    B.
    EDIT2: A check on the file in Numbers '08 showed no apparent change to the formula, and editing Jane Doe's name on table 1 resulted in her name being added to Table 3.
    Message was edited by: Barry

  • How to access records from two tables which have no relation

    Hi,
    I trying to generate a report where i need to print the company details at the top of the page and invoice details of that down the page.There is no relation between these two tables.I am not able to write two queries for one report.Pls some one assist me in getting this thing done.
    Regards,
    Tulacenath.

    Hi Tulacenath
    So your invoice table does not have a reference to the customers (companys) that the invoices belong to?
    Tim

Maybe you are looking for

  • ISync crashes when syncing Address Book and Google contacts

    "20/03/2009 11:32:29 gconsync[506] ISyncSession record validation failure: Value other pushed for enumeration property service on com.apple.contacts.IM is not an allowed value. 20/03/2009 11:32:29 com.apple.syncservices.SyncServer[340] 2009-03-20 11:

  • Using an exteral hard drive to edit movies

    Hello, I need some help. I have a iBook G4 and am running OS X 10.3.9. I have a lot of footage that I want to import and edit in imovie. I do not have enough hard drive space on my laptop to do that. I would like to buy and external hard drive to edi

  • Can I run a Floppy drive to transfer photos from old floppies

    I have some photos that where professionally placed on 3.5 floppys. Can I purchase a inexpensive USB floppy drive ("mac compatible") and transfer these photos to my iMac 10.8.2 to create CD's? thanx in advance David

  • Best way to install oracle server + odp on same machine

    Hello, I've been having problems with this configuration: Win 2003 x64 R2 + IIS Server + Oracle DataBase 10g Release 2 102010_win64_x64_database + 10.2.02 patchset Win2003 x64 + ODAC 10.2.02 x64 Beta So far, I've installed all in the same Oracle_home

  • Dreaded error #2032

    [dumbfounded] I'm trying to access an online PHP file which returns XML. The PHP is passed GET parameters. It works in a browser just fine. A barebones test (see code below) works in an AIR app too while testing from the Flash IDE but testing for Fla