Select top 10 in pivot special...

How can I get the top 10 customers in the selected period. Group only customer.
Then use these 10 customers and list them in each month, regardless what value they have on each month. The total value in the hole period should decide that. Group Year_Month and Customer. Show each customer in a separate field. Order by the biggest customer first. I'm stucked... Alex has help me so far with this, but this is not 100% right.
Select sales, year_month, customer From
Select sales, year_month, customer, rank() over (Partition by year_month Order by sales Desc) Rang
From (Select Sum(SalesQty) As sales, customer, year_month From CustTable Where year_month Between 200709 And 200801 Group by customer, year_month) tempa
) tempb
Where Rang <=10 Order by year_month, sales Desc;It should return something like this:
YEAR_MONTH                        Statiol                             Mc Donalds 
200709                                 90443062,9                             86446636,4
200710                               103277956,29                           100488081,38
200711                                 95907009,9                             39525297,2
200712                                93945336,26                            58435507,81
200801                                85986272,49                            29581584,28

Sorry, I overlooked "top 10 customers in the selected period. Group only customer" which tells me you need to rank sales for the whole period. If so, use:
SELECT  year_month,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 1
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_1,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 2
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_2,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 3
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_3,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 4
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_4,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 5
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_5,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 6
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_6,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 7
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_7,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 8
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_8,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 9
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_8,
        SUM(
            CASE CustomerTotalSalesRank
              WHEN 10
                THEN
                  SalesQty
                ELSE
                  NULL;
            END
           ) customer_10
  FROM  (
         SELECT  year_month,
                 SalesQty,
                 ROW_NUMBER() OVER(PARTITION BY customer ORDER BY CustomerTotalSales DESC) CustomerTotalSalesRank
           FROM  (
                  SELECT  customer,
                          year_month,
                          SalesQty,
                          SUM(SalesQty) OVER(PARTITION BY customer) CustomerTotalSales
                    FROM  CustTable
                    WHERE year_month BETWEEN 200709 AND 200801
  WHERE CustomerTotalSalesRank <= 10
  GROUP BY year_month
/SY.

Similar Messages

  • How to select top one in CDS view ?

    I have tried following logic to select top one in CDS view but its giving error -
    define view Cds_View_First_Reference as select  top one CReferredObject from CDSVIEWCROSSREF

    Hi Ruchi,
    since you posted this question in "ABAP in Eclipse" I assume you are asking about CDS in ABAP. This is important because the CDS features sets in ABAP and (native) HANA are different.
    Be that as it may,, SELECT TOP 1 is neither supported in the CDS implementation in ABAP nor in HANA.
    In ABAP you might consider using the min() or max() function together with the appropriate GROUP BY clause in the CDS view (depending on what you want to achieve), but you can also easily "implement" this in the Open SQL statement which selects from your CDS view.
    Using the additions SELECT SINGLE or UP TO 1 ROWS and an appropriate ORDER BY clause in Open SQL, you can achieve the same as SELECT TOP 1.
    Unfortunately there is currently no possibility to define a view which delivers the TOP 1 which you can again use in another view ("view on view").
    Kind regards
    Chris

  • Select query with pivot

    Hi All,
    I have written a query as belows: but I dont get the output as desired.
    This is a with query -  I need the query output in a single record so I have pivoted the first query spl. The second set of query stk gives me the stock.
    My requirement is whnever there is a not matching mmg_id of 1st query with mmg5_id of 2nd query it should give the second record as follows
    LOC_LOC_ID      MMG_ID     TP_TP_ID      'S10'       'O3'            'O7'            'O8'       CN_STK_QTY      CN_STK_VAL
    1097                  1610           20132123       207.5      62.08       7504           25.06      603                       93027.5
    1097                  0024          20132123                                                                    -1                        -15.99
    {code}
    Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.2.0
    Connected as sizsupport
    SQL>
    SQL> WITH spl AS
      2  (
      3  SELECT * FROM
      4  (SELECT '1097' loc_loc_id, '1610' mmg_id, '20132123' tp_tp_id, 'S10' measure_name,'207.5' val FROM dual UNION ALL
      5  SELECT '1097' , '1610' , '20132123' , 'O3' measure_name, '62.08'  FROM dual UNION ALL
      6  SELECT '1097' , '1610' , '20132123' , 'O7' measure_name, '7504'  FROM dual UNION ALL
      7  SELECT '1097' , '1610' , '20132123' , 'O8' measure_name, '25.06'  FROM dual )
      8  pivot ( sum(val) FOR measure_name IN
      9  ('S10','O3','O7','O8'))
    10  ),
    11  stk AS
    12  (
    13  SELECT '1097' loc_loc_id, '0024' mmg5_id, '20132123' tp_tp_id, '-1' cn_stk_qty, '-15.99' cn_stk_val FROM dual
    14  UNION ALL
    15  SELECT '1097' loc_loc_id, '1610' mmg5_id, '20132123' tp_tp_id, '603' cn_stk_qty, '93027.5' cn_stk_val FROM dual
    16  )
    17  SELECT spl.*,stk.cn_stk_qty,stk.cn_stk_val
    18  FROM spl  RIGHT OUTER JOIN stk
    19  ON spl.loc_loc_id = stk.loc_loc_id
    20  AND spl.tp_tp_id  = stk.tp_tp_id
    21  AND spl.mmg_id    = stk.mmg5_id
    22  ;
    LOC_LOC_ID MMG_ID TP_TP_ID      'S10'       'O3'       'O7'       'O8' CN_STK_QTY CN_STK_VAL
    1097       1610   20132123      207.5      62.08       7504      25.06 603        93027.5
                                                                           -1         -15.99
    SQL>
    {code}

    Hi,
    When there is no match between stk and spl, then all columns from spl will be NULL, but the values from stk will be available.  Take the columns you need to display from stk, not from spl.
    WITH  spl  AS
        SELECT  *
        FROM    raw_data
        PIVOT   (   SUM (val)
                FOR measure_name IN ( 'S10' AS s10
                                    , 'O3' AS p_03
                                    , 'O7' AS p_07
                                    , 'O8' AS p_08
    SELECT  stk.loc_loc_id
    ,       stk.mmg5_id
    ,       stk.tp_tp_id
    ,       spl.s10
    ,       spl.p_03
    ,       spl.p_07
    ,       spl.p_08
    ,       stk.cn_stk_qty
    ,       stk.cn_stk_val
    FROM             stk
    LEFT OUTER JOIN  spl  ON   spl.loc_loc_id = stk.loc_loc_id
                          AND  spl.tp_tp_id   = stk.tp_tp_id
                          AND  spl.mmg_id     = stk.mmg5_id
    I gave the pivoted columns standard names, like s10 and p_03.  You don't have to do that; you can use "'S10'" and "'P_03'" if you prefer.

  • Prepared Statement, executing  SELECT TOP in Ms Access

    I'm having the following problem. I built an application fetching data without any problem from MS SQL Server, this was using prepared statements. Now i'm having this strange problem with the following query:
    SELECT TOP 1 *
    FROM table
    WHERE appliance_id = ?
    AND ttimestamp_initpk <= ?
    AND ((Type = ?) OR (Type = ?) OR (Type = ?) OR (Type = ?) OR (Type = ?) OR (Type = ?))
    ORDER BY ttimestamp_initpk DESC
    While on SQL Server this fetches just a record, on MS Access it fetches the whole set of records without worrying about TOP instruction.
    That is, it acts like a SELECT * FROM, and this slows down runtime behaviour.
    I'm using sun.jdbc.odbc.JdbcOdbcDriver, is it the problem?
    Thanks, Mirko

    Thanks for your answer, but i'm not sure this is the reason.
    I made one more test: i wrote a .Net application connecting to a MS Access using an ODBC connection. SELECT TOP statement is working fine, so i think i'll try another JDBC driver. Any other solution??
    Thanks,
    Mirko

  • ALTERNATIVE FOR 'SELECT TOP STATEMENT

    HI FRIENDS
    IF ANY ONE KNOWS HOW TO CONVERT THE SQL STATEMENT GIVEN BELOW PLEASE HELP ME
    SELECT TOP 1 CardID FROM EasyRechargeMaster WHERE CardGroup = EasyRecharge.CardGroup
    THANKS & REGARDS

    Check this query. It should give you desired results....Are you sure about that? Does TOP 1 mean "whatever happens to be the first row fetched"?

  • SELECT TOP query in HANA

       Hi,
    Can somebody give me the HANA equivalent of the following SQL query:
    SELECT TOP (731-624+1) COLUMNNAME FROM TABLENAME
    In case of HANA, it gives syntax error near "(".

    Hi Yash,
    As per the HANA SQL syntax supported case is
    <select_clause> ::= SELECT [TOP <unsigned_integer>] [ ALL | DISTINCT ]
    <select_list>
    After Top only an unsigned intger is expected and cannot handle an <expression> like in your example.
    Hope this is useful.
    Best Regards,
    Ranjit

  • SELECT TOP 1 .... ORDER BY

    Hi,
    I have the following statement in SQL SERVER:
    SELECT TOP 1 NAME FROM STUDENT ORDER BYGRADE
    Now, I am trying to translate it ti ORACLE PLSQL, I came up with this statement:
    SELECT NAME FROM STUDENT WHERE ROWNUM=1 ORDER BY GRADE
    It doesn't work. I guess ORACLE generates ROWNUM before the ORDER BY operation...
    How can I have SELECT TOP N ... ORDER BY ... in Oracle the way that I have it in SQL server?
    Any help would be apprecited,
    Alan

    Can you cut and paste exactly what you're doing in a SQL*Plus session? It seems to work for me
    SCOTT @ hp92 Local> create table t as select * from all_objects;
    Table created.
    Elapsed: 00:00:08.40
    SCOTT @ hp92 Local> desc t;
    Name                                                  Null?    Type
    OWNER                                                 NOT NULL VARCHAR2(30)
    OBJECT_NAME                                           NOT NULL VARCHAR2(30)
    SUBOBJECT_NAME                                                 VARCHAR2(30)
    OBJECT_ID                                             NOT NULL NUMBER
    DATA_OBJECT_ID                                                 NUMBER
    OBJECT_TYPE                                                    VARCHAR2(18)
    CREATED                                               NOT NULL DATE
    LAST_DDL_TIME                                         NOT NULL DATE
    TIMESTAMP                                                      VARCHAR2(19)
    STATUS                                                         VARCHAR2(7)
    TEMPORARY                                                      VARCHAR2(1)
    GENERATED                                                      VARCHAR2(1)
    SECONDARY                                                      VARCHAR2(1)
    SCOTT @ hp92 Local> create index idx_t on t( object_id );
    Index created.
    Elapsed: 00:00:00.78
    SCOTT @ hp92 Local> analyze table t compute statistics for all indexed columns;
    Table analyzed.
    Elapsed: 00:00:00.53
    SCOTT @ hp92 Local> set autotrace on;
    SCOTT @ hp92 Local> select *
      2    from (select * from t order by object_id)
      3   where rownum < 2;
    OWNER                          OBJECT_NAME                    SUBOBJECT_NAME
    OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE        CREATED   LAST_DDL_ TIMESTAMP           STATUS  T G S
    SYS                            DUAL
           222            222 TABLE              12-MAY-02 12-MAY-02 2002-05-12:16:20:50 VALID   N N N
    Elapsed: 00:00:00.13
    Execution Plan
       0      SELECT STATEMENT Optimizer=CHOOSE
       1    0   COUNT (STOPKEY)
       2    1     VIEW
       3    2       TABLE ACCESS (BY INDEX ROWID) OF 'T'
       4    3         INDEX (FULL SCAN) OF 'IDX_T' (NON-UNIQUE)
    Statistics
              0  recursive calls
              0  db block gets
              3  consistent gets
              1  physical reads
              0  redo size
           1142  bytes sent via SQL*Net to client
            511  bytes received via SQL*Net from client
              2  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
              1  rows processedJustin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • Looking for the oracle equivalent of T-SQL 'SELECT TOP n'

    Hi,
    I'm looking for the Oracle equivalent of T-SQL 'SELECT TOP n'
    and can't find any. There is SAMPLE(n) function but it supposed
    to pick up random values and I'm not sure if it's possible to
    make it select top values. Please help 8-)
    Thanx

    Hi Marina.
    Oracle does not have a functionality like SQL Server for TOP
    selection. The ROWNUM option should be used with great care and
    you may get unreliable results.
    Try looking at Metalink
    Doc ID: 291065.999
    Doc ID: 267329.999
    - They discuss this issue, and solutions.

  • Group By Select Top 1 Rows

    Hi Guys,
    I tried Group By from Select Top 1 Records,  As below the script wasn't work.
    Please help.
    SELECT TOP 1 NR.SHGId, NR.Amount
    FROM NEW_DCB_REPORT NR
    WHERE NR.SHGId = ND.SHGId
    AND NR.ShgMemberId = ND.ShgMemberId 
    AND NR.LoanNumber = ND.LoanNumber 
    GROUP BY NR.SHGId,NR.Amount
    ORDER BY NR.LedgerNumber DESC
    SHGId
    SHGMemberId
    LoanNumber
    Amount
    Select Top 1 Row
    1028
    147852
    1
    1000
    Select Top 1 Row
    1028
    147853
    1
    2000
    Select Top 1 Row
    1028
    147854
    1
    1000
    Select Top 1 Row
    1028
    147855
    1
    1000
    5000
    I expected the results are:
    SHGId
    Amount
    1028
    5000

    Yes I did it.
    Select  SHGId,
    SUM(Amount)
    amt  FROM
    SELECT  NR.SHGId,
    NR.Amount,
    , Row_number()
    Over(partition
    by NR.SHGId,
    NR.ShgMemberId , NR.LoanNumber
    Order
    by NR.LedgerNumber
    DESC
    ) rn
    FROM NEW_DCB_REPORT NR
    JOIN
     NEW_DCB_REPORT ND
    ON
    NR.SHGId
    =
    ND.SHGId
    AND NR.ShgMemberId
    = ND.ShgMemberId
    AND NR.LoanNumber
    = ND.LoanNumber
    ) t
    Where rn=1
    GROUP
    BY  SHGId
    I just changed above the script Partition by add one more column and
    [yourNDTable]
    Should come NEW_DCB_REPORT table.
    However, It shows below the error ,. Please help.
    Invalid object name 'NEW_DCB_REPORT'.

  • "select * from table" and "select top 14260 from table" get nothing but select top 14259 get result successful

    select * from tablename                   ------always running,but get nothing
    select top 1 *  from tablename         -------get result quickly
    select top 2 *  from tablename         -------get result quickly
    select top 14259 * from tablename  --------get result quickly
    select top 14260 * from tablename  --------always running,but get nothing
    the thread is:
    java.net.SocketInputStream.socketRead0(Native Method)
    java.net.SocketInputStream.read(SocketInputStream.java:150)
    java.net.SocketInputStream.read(SocketInputStream.java:121)
    com.microsoft.sqlserver.jdbc.TDSChannel.read(IOBuffer.java:1782)
    com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4838)
       - 已锁定com.microsoft.sqlserver.jdbc.TDSReader@54269910
    com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6150)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:402)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
       - 已锁定java.lang.Object@320b1499
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:332)

    when I run the below sql of Uri Dimant,I get many rows,I think you are right!
    Do you have some method to handle this problem?
    Or do you have some information for me to Learn?
    Thanks a lot.
    SELECT
    owt.session_id AS waiting_session_id,
        owt.blocking_session_id,
    DB_NAME(tls.resource_database_id) AS database_name,
        (SELECT SUBSTRING(est.[text], ers.statement_start_offset/2
    + 1,
    (CASE WHEN ers.statement_end_offset = -1
    THEN LEN(CONVERT(nvarchar(max), est.[text])) * 2
    ELSE ers.statement_end_offset
    END
    - ers.statement_start_offset
    ) / 2)
    FROM sys.dm_exec_sql_text(ers.[sql_handle]) AS est) AS waiting_query_text,
    CASE WHEN owt.blocking_session_id > 0 
    THEN (
    SELECT
    est.[text] FROM sys.sysprocesses AS sp
    CROSS APPLY sys.dm_exec_sql_text(sp.[sql_handle]) as est
    WHERE sp.spid = owt.blocking_session_id)
    ELSE
    NULL
    END AS blocking_query_text,
        (CASE tls.resource_type
    WHEN 'OBJECT' THEN OBJECT_NAME(tls.resource_associated_entity_id, tls.resource_database_id)
    WHEN 'DATABASE' THEN DB_NAME(tls.resource_database_id)
    ELSE (SELECT  OBJECT_NAME(pat.[object_id], tls.resource_database_id)
    FROM sys.partitions pat WHERE pat.hobt_id = tls.resource_associated_entity_id)
    END
    ) AS object_name,
    owt.wait_duration_ms,
    owt.waiting_task_address,
    owt.wait_type,
    tls.resource_associated_entity_id,
    tls.resource_description AS local_resource_description,
    tls.resource_type,
    tls.request_mode,
    tls.request_type,
    tls.request_session_id,
    owt.resource_description AS blocking_resource_description,
    qp.query_plan AS waiting_query_plan
    FROM sys.dm_tran_locks AS tls
    INNER JOIN sys.dm_os_waiting_tasks owt ON tls.lock_owner_address = owt.resource_address
    INNER JOIN sys.dm_exec_requests ers ON tls.request_request_id = ers.request_id
    AND owt.session_id = ers.session_id
    OUTER APPLY sys.dm_exec_query_plan(ers.[plan_handle]) AS qp
    GO

  • Can SELECT TOP have argument?

    Newbie’s question, I believe.
    I wanted to make a stored procedure to returns last several rows or last several hundred rows depending on the situation, so I started writing queries in order to copy/paste them into procedure. There are two queries, first works, but
    second one shows syntax error in select statement. Can anyone tell me why, and how to go around this?
    SELECT TOP 5 * FROM Table1 ORDER BY ID DESC
    DECLARE @Counter as int
    SET @ Counter = 5
    SELECT TOP @ Counter FROM Table1 ORDER BY ID DESC

    This works. But I still don?t realize what was wrong with my syntax. Do those () will convert what?s inside parameter to string so parser could read it correctly? What actually is going on here?
    It's just the way it is. Originally there was only SELECT TOP n, where the value after TOP had to be a constant. Then they added the ability to have expression, but then they added the parens. This is so that you can say things like:
    SELECT TOP (SELECT ... FROM ...) ... FROM
    if you feel like.
    The old syntax is deprecated but remains for compatibility reasons.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Oracle 10g Select Top 100 Percent

    Hello
    How to convert MS sql 2008 select top 100 query Oracle PL/SQL ? MY sample MSSQL select query
    SELECT TOP (100) PERCENT dbo.Operations.OpID, dbo.CompanyInfo.Name AS CompanyName, dbo.CustomerInfo.SubscriberNo, dbo.CustomerInfo.FirstName,
    dbo.CustomerInfo.LastName, dbo.Operations.OpDate, dbo.Operations.BillCount, dbo.Operations.TotalAcceptedCash, dbo.Operations.KioskID,
    dbo.Operations.ReceiptNo, dbo.Operations.KioskOpID, dbo.KioskInfo.Name AS KioskName, dbo.Operations.TotalBill, dbo.Operations.TotalPayBack,
    dbo.CompanyInfo.CompanyID, dbo.Operations.ConfirmedRecord, dbo.PayMethod.ACK AS PayMethod
    FROM dbo.Operations INNER JOIN
    dbo.CustomerInfo ON dbo.Operations.SubscriberNo = dbo.CustomerInfo.SubscriberNo INNER JOIN
    dbo.CompanyInfo ON dbo.Operations.CompanyID = dbo.CompanyInfo.CompanyID INNER JOIN
    dbo.KioskInfo ON dbo.Operations.KioskID = dbo.KioskInfo.KioskID INNER JOIN
    dbo.PayMethod ON dbo.Operations.PayMethodID = dbo.PayMethod.PayMethodID
    ORDER BY dbo.Operations.OpDate DESC

    Hi,
    Please read SQL and PL/SQL FAQ
    Additionally when you put some code please enclose it between two lines starting with {noformat}{noformat}
    i.e.:
    {noformat}{noformat}
    SELECT ...
    {noformat}{noformat}
    From what I have found on MS SQL documentation it seems that TOP keyword limits the output to a specified percentage of total number of rows. In your case, having specified 100, you are returning 100% of rows.
    So you can simply remove *TOP (100) PERCENT*
    Regards.
    Al                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Select Top 10 when it includes a GroupBy

    I frequently use Select Top 10... to test a query before running it on a large MSSQL database. Works fine, except when the query includes a Group By Clause when it then seems to take as long as not using Top 10. I suspect this is beause it is actually
    reading the whole table to get the top 10 Group Bys, whereas all I wanted was for it  read the first 10 records and Group the result. Is there a way to write so that it does that                                                                                                                                
    Gerry Mescal

    sample code ..modify and try this SELECT a.*
    FROM
    SELECT a.*, ROW_NUMBER() OVER(PARTITION BY m.[col1] ORDER BY m.[col2] DESC) an
    FROM [table] a
    ) a
    WHERE a.an <= 10
    ORDER BY m.[col2] DESCmark as answer if it is useful

  • Update with Select top n statement

    Hi All,
    Can I use update statemet like this..
    Update table1
    set Name=(select top (100) t2.name from table2 t2 where table1.ID=t2.ID)
    Please let me know ASAP its very urgent
    Thanks in Advance
    RH
    sql

    Hi sql9 !
    You might need below query;
    CREATE TABLE Table1 (ID INT,Name VARCHAR(10))
    CREATE TABLE Table2 (ID INT,Name VARCHAR(10))
    --TRUNCATE TABLE Table1
    --TRUNCATE TABLE Table2
    INSERT INTO Table1
    SELECT 1,'ABC' UNION ALL
    SELECT 2,'DEF' UNION ALL
    SELECT 3,'GHI'
    INSERT INTO Table2
    SELECT 1,'JKL' UNION ALL
    SELECT 1,'MNO' UNION ALL
    SELECT 2,'PQR' UNION ALL
    SELECT 2,'STU' UNION ALL
    SELECT 3,'VWX' UNION ALL
    SELECT 3,'YZ'
    UPDATE Table1 SET Name = (SELECT TOP 1 T2.Name FROM Table2 T2 WHERE Table1.ID = T2.ID)
    SELECT * FROM Table1
    --ID Name
    --1 JKL
    --2 PQR
    --3 VWX
    --You can simply re-write your SQL with below query and you don't have to specify TOP Clause inside subquery
    UPDATE T1 SET T1.Name = T2.Name
    FROM Table1 T1
    INNER JOIN Table2 T2 ON T2.ID = T1.ID
    SELECT * FROM Table1
    --ID Name
    --1 JKL
    --2 PQR
    --3 VWX
    Note : In your first query TOP 100 returning more than 1 value from subquery which is not allowed.
    Please let me know if this doesn’t work for you. Hope I have answered you correctly.
    Thanks,
    Hasham

  • Select top n

    Hello,
    I was wondering if it was possible to limit the amount of lines selected from a table.
    I tried select top n, as it works in SQL, but SAP doesn 't support this syntax..
    Is this doesn't work, is there any other way to limit the amount of data selected ?

    Hi,
    Use select upto n rows
    Select * from MARA into table ITAB UPTO 10 rows.
    select top 10 rows in table.
    Regards
    SAB

  • Selecting top five rows

    hi,
    pls I have this issue that needs to be resolved. I need to select top five rows from a table depending on the values of a column stock_value. pls I will appreciate if someone can help me out, Francois pls try and help
    This is the senerio:
    I have a table stock, on the table I have company_name, stock_value, sector.
    Now I want to get top five rows from this table depending on the five highest value of stock_value and for each sector.
    lets try and write a query that will retrive the records after that I will put it in a procedure.
    I know am suppose to ask this on mssql forum but most mssql forums takes time to reply and this is very urgent cos I have to deliver in 45 minutes time
    thank you very much
    jideofor

    hy,
    try out:
    select s.name, sp. sp_sales total_sales
    from salesperson s,
    (select salesperson_id,sum(tot_sales) sp_sales
    RANK() OVER (ORDER BY SUM(tot_SALES) desc) SALES_RANK
    FROM ORDERS
    WHERE YEAR = 2006
    group by salesperson_id) sp
    where sp.sales_rank <= 5 (...n)
    and sp.salesperson_id = s.salesperson_id
    order by sq.sales_rank;
    give result in:
    name total_sales
    a 100
    b 90
    c 80
    d 70
    e 60
    hope help you
    regards

Maybe you are looking for

  • How to use home sharing for more that five Apple TV 2nd?

    Hi, i know that itunes works (with "home sharing" functions activated) only with max n°5 Apple TV 2nd and n°5 Idevices. Now, i've a problem because i need to have n°10 Apple TV that work in the same time with itunes installed on Mac Mini (lion OSX).

  • AE 7.0 Professional won't install on Windows 7 64bit

    When I insert the disc and the Autoplay comes up, I click on the first option "Install After Effects 7.0". A window pops up asking me what language I am using. I select English and then the setup window comes up with an error message window behind it

  • File content conversion for file with hierarchical structure

    Hi I want to read a flat file in the following message structure using file content conversion. Message structure is : 01 ( occurence = 1) _ 1B (occurence = unbounded) _ 1G (occurence = unbounded) _1H (occurence = 1) _1N ( occurence = unbounded) 99 (

  • Drag and Drop Problem 3.0.1

    Hi, I upgraded from 3.0 to 3.0.1. I modified a form using the Drag and Drop Layout. On "Next" session the item's names are modified blank char to"%20", "à" to "%C3%A0" for example prod id --> prod%20Id Località-->Localit%C3%A0 Has anybody hit this pr

  • Purchase Order Response BUS2232

    We are using SRM-SUS and I am looking to create a Purchase Order Response )aka PO Confirmation) from a SUS supplier to the original PO. But I cannot see where in the Supplier users access this document is created. The documentation discusses setting