Top n Analysis using correlated subquery

Please explain this query. It is doing top n analysis using correlated subquery. I need explaination of execution of this query.
Select distinct a.sal
From emp a
where 1=(select count ( distinct b.sal) from emp b
     where a.sal <=b.sal)
Thanks in advance

Try breaking the query down and rewriting it in order to follow the logic;
SQL> --
SQL> -- Start by getting each salary from emp along with a count of all salaries in emp
SQL> --
SQL> select   a.sal,
        (select count (distinct b.sal) from scott.emp b ) count_sal
from scott.emp a
order by 1 desc
       SAL  COUNT_SAL
      5000         12
      3000         12
      3000         12
      2975         12
      2850         12
      2450         12
      1600         12
      1500         12
      1300         12
      1250         12
      1250         12
      1100         12
       950         12
       800         12
14 rows selected.
SQL> --
SQL> --Add a condition to the count for only salaries below or equal to the current salarySQL> --
SQL> select   a.sal,
        (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
from scott.emp a
order by 1 desc
       SAL   RANK_SAL
      5000          1
      3000          2
      3000          2
      2975          3
      2850          4
      2450          5
      1600          6
      1500          7
      1300          8
      1250          9
      1250          9
      1100         10
       950         11
       800         12
14 rows selected.
SQL> --
SQL> -- Add a condition to only pick the nth highest salary
SQL> --
SQL> select    a.sal,
         (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
from scott.emp a
where (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) = 4
       SAL   RANK_SAL
      2850          4
1 row selected.Hope this helps.

Similar Messages

  • Top n analysis using hierarchial queries

    hi all,
    can we do top n analysis in hierarchial queries using level pseudo columns. if so please give an example.
    thanks and regards,
    sri ram.

    Hi,
    Analytic functions (such as RANK) often interfere with CONNECT BY queries. Do one of them in a sub-query, and the other in a super-query, as shown below.
    If you do the CONNECT BY first, use ROWNUM (which is assigned after ORDER SIBLINGS BY is applied) to preserve the order of the CONNECT BY query.
    WITH     connect_by_results     AS
         SELECT     LPAD ( ' '
                   , 3 * (LEVEL - 1)
                   ) || ename          AS iname
         ,     sal
         ,     ROWNUM               AS r_num
         FROM     scott.emp
         START WITH     mgr     IS NULL
         CONNECT BY     mgr     = PRIOR empno
         ORDER SIBLINGS BY     ename
    SELECT       iname
    ,       sal
    ,       RANK () OVER (ORDER BY sal DESC)     AS sal_rank
    FROM       connect_by_results
    ORDER BY  r_num
    ;Output:
    INAME                  SAL   SAL_RANK
    KING                  5000          1
       BLAKE              2850          5
          ALLEN           1600          7
          JAMES            950         13
          MARTIN          1250         10
          TURNER          1500          8
          WARD            1250         10
       CLARK              2450          6
          MILLER          1300          9
       JONES              2975          4
          FORD            3000          2
             SMITH         800         14
          SCOTT           3000          2
             ADAMS        1100         12 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data. If you use only commonly available tables (such as those in the scott or hr schemas), then you don't have to post any sample data; just post the results.
    Explain how you get those results from that data.
    Always say what version of oracle you're using.

  • How to write this query using correlation subquery or non exists clause

    -- Tables description.
    --step-1- 4 employees present in EMP table.
    --step-2- each employee having 1 country_no in EMP_DOCS table.3 employees having same country_no(i.e emp's 1,2,3)
    --step-3- 1 emp document can have multiple items.In this case each employee having one each in the EMP_ITEMS table.
    --step-4- 1 EMPLOYEE  can have Multiple Documents so we have a relation between EMP_ITEMS and DOCUMENT_ITEMS.whatever items present in EMP_ITEMS those items will be inserted into DOCUMENT_ITEMS.so we have a item-item relation.
    --so we stored EMP_ITEMS id in EMP_ITEMS_REF_ID_1 of DOCUMENT_ITEMS table.
    -- step-5- DOCUMENT-INVOICE has 1-1 relation once invoice is created we stored invoice id in DOCUMENT table.
    --This is the requirement.Let's say in this example 3 employees are using same country_no and 4th employee is using another country_no
    --which is not used by other 3 employees.
    --Condtion-1:
    --if all of the employees have created INVOICE which is using same country_no of different country_no then the query should display all records.
    --Condition-2:
    --if any one of the employee not created INVOICE which is using same country_no of other employees then remaining employees also should not come in the query
    -- even though invoice is created by other employees.
    --Condition-3:
    --if any one of the employee not created even DOCUMENT  which is using same country_no of other employees then remaining employees also should not come in the query
    -- even though invoice is created by other employees.
    I hope I explain the conditions clearly.if you understand well by looking at the data i posted below may i know what is the final result should be displayed?
    create table EMP
      ID       NUMBER not null,
      TYPE     VARCHAR2(1)
    alter table EMP
      add constraint ID primary key (ID);
    create table EMP_DOCS
      ID         NUMBER not null,
      EMP_ID     NUMBER,
      COUNTRY_NO VARCHAR2(15)
    alter table EMP_DOCS
      add constraint PK_EMP_DT primary key (ID);
    alter table EMP_DOCS
      add constraint FK_EMP_DT foreign key (EMP_ID)
      references EMP (ID);
    create table EMP_ITEMS
      ID         NUMBER not null,
      EMP_DOC_ID     NUMBER
    alter table EMP_ITEMS
      add constraint PK_EMP_ITEMS_DT primary key (ID);
    alter table EMP_ITEMS
      add constraint FK_EMP_ITEMS_DT foreign key (EMP_DOC_ID)
      references EMP_DOCS (ID);
      create table DOCUMENT
      ID   NUMBER not null,
      DOCNO VARCHAR2(15),
      INVOICE_REF_1 NUMBER
    alter table DOCUMENT
      add constraint DOC_PK_ID primary key (ID);
      create table DOCUMENT_ITEMS
      ID         NUMBER not null,
      DOC_ID     NUMBER,
      EMP_ITEMS_REF_ID_1    NUMBER
    alter table DOCUMENT_ITEMS
      add constraint PK_DOCUMENT_ITEMS_DT primary key (ID);
    alter table DOCUMENT_ITEMS
      add constraint FK_DOCUMENT_ITEMS_DT foreign key (DOC_ID)
      references DOCUMENT (ID);
    create table INVOICE
      ID   NUMBER not null,
      INVOICE_NO VARCHAR2(15)
    alter table INVOICE
      add constraint INVOICE_PK_ID primary key (ID);
      INSERT INTO EMP ( ID, TYPE ) VALUES (
    1, 'A');
    INSERT INTO EMP ( ID, TYPE ) VALUES (
    2, 'A');
    INSERT INTO EMP ( ID, TYPE ) VALUES (
    3, 'A');
    INSERT INTO EMP ( ID, TYPE ) VALUES (
    4, 'A');
    INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
    1, 1, 'INDIA');
    INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
    2, 2, 'INDIA');
    INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
    3, 3, 'INDIA');
    INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
    4, 4, 'USA');
    INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
    1, 1);
    INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
    2, 2);
    INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
    3, 3);
    INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
    4, 4);
    INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
    1, 'DOC1', 1);
    INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
    2, 'DOC1', 2);
    INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
    3, 'DOC3', 0);
    INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
    4, 'DOC4', 3);
    INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
    1, 1, 1);
    INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
    2, 2, 2);
    INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
    3, 2, 2);
    INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
    4, 3, 3);
    INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
    5, 4, 4);
    INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
    1, 'INV1');
    INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
    2, 'INV2');
    INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
    3, 'INV3');
    commit;
    -- I have written below query to satisfy above conditions but still required results are not coming..
    SELECT *
      FROM EMP E
    WHERE E.TYPE = 'A'
       AND NOT EXISTS (SELECT *
              FROM EMP_DOCS       EDOC1,
                   EMP_DOCS       EDOC2,
                   EMP            E1,
                   EMP_ITEMS      EMPI,
                   DOCUMENT_ITEMS DOCITM,
                   DOCUMENT       DOC,
                   INVOICE        INV
             WHERE EDOC1.EMP_ID = E.ID
               AND EDOC2.EMP_ID = E1.ID
               AND EDOC1.ID = EMPI.EMP_DOC_ID
               AND EDOC1.COUNTRY_NO = EDOC2.COUNTRY_NO
               AND EMPI.ID = DOCITM.EMP_ITEMS_REF_ID_1
               AND DOCITM.DOC_ID = DOC.ID
               AND INV.ID = DOC.INVOICE_REF_1);

    DB version:oracle 10g;10.2

  • How to INSERT into table using CORRELATED subquery

    I have 3 tables:
    1.TEMP_PHONE(person_id, phonenumber, phone_type) - this holds all phone numbers relating to a person(just a temporary holding area)
    2.PHONE_CONNECT(PERSON_ID, PHONE_ID) this table shows all the phone numbers relating to an individual. Phone_id is a unique number to identify a phonenumber and type(cell, work, home) - so in this table a person can have multiple phone ids)
    3.MASTER_PHONE(PHONE_ID, PHONENUMBER, PHONE_TYPE) this is a master phone table. each combination of phone number and type has a unique identifier-phone_id.
    What i need to figure out is how to populate PHONE_CONNECT with the information from TEMP_PHONE IF PERSON_ID already exists but phone_id is different. In other words, if the person gets a new phone number, i need to insert a new row into phone_connect.
    Before that step is started, the master_phone is populated first with a new phone_id associated to the phonenumber/type
    any help would be much appreciated. Thanks in advance.
    So far, this is what i have come up with, but not sure if it makes sense:
    insert into phone_connect(person_id)
    select a.person_id
    from temp_phone a
    where
    person_id = (select b.person_id from phone_connect b, master_phone c
    where
    a.person_id=b.person_id
    and b.phone_id <> c.phone_id
    and c.phonenumber||c.phone_type=a.phonenumber||a.phone_type);
    update phone_connect c
    set phone_id=(
    select b.phone_id
    from temp_phone a, master_phone b
    where a.person_id = c.person_id
    and a.phonenumber||a.phone_type = b.phonenumber||b.phone_type)
    where phone_id is null;

    It does. You are right. But that's what i need help with. I don't think my code is correct. After the insert, the code is actually updating the same exact record I just inserted. I'm sure this all can be done with one insert. I just really don't know how to show that in my code.
    I need to insert a new record into phone_connect with person_id and phone_id. phone_id is already populated in master_phone. I guess my problem is how to go about creating the joins to all three tables to make sure im inserting the data correctly, or not inserting data that already exists.

  • Performance of using a Select For Update vs a correlated subquery

    I was wondering wether or not it is more effecient to use the
    Select ... For Update (with a cursor etc.) versus a correlated
    subquery.
    I can accomplish the same thing with either however performance
    at our site is an issue.

    Use select for update cursor as that is faster as it updates
    based on the rowid. One thing to keep in mind is that rowid is
    session specific and the rows to be updated get locked so that
    nobody else can update them till the lock is released. I have
    had very good performance results with these cursors.
    Good luck !
    Sudha

  • SQL Bug in "Minus" in correlated subquery presence of index (11.2.0.1.0)

    SQL Bug in "Minus" in correlated subquery presence of index
    (Oracle Database 11g Release 11.2.0.1.0)
    Below, there is a small example that shows the bug. Further below,
    there are some more comments.
    drop table Country;
    create table Country
    (code VARCHAR2(4) constraint countrykey PRIMARY KEY,
    name VARCHAR2(35));
    -- if the key constraint is not given, the bug does not occur
    drop table City;
    create table City
    (name VARCHAR2(35),
    country VARCHAR2(4),
    population number);
    drop table Locatedon;
    create table Locatedon
    (city VARCHAR2(35),
    country VARCHAR2(4),
    island VARCHAR2(35));
    insert into country values('E','Spain');
    insert into country values('F','France');
    insert into country values('S','Sweden');
    insert into country values('GB','Sweden');
    insert into city values('Ajaccio','F',53500);
    insert into city values('Paris','F',2152423);
    insert into city values('Palma','E',322008);
    insert into city values('Madrid','E',3041101);
    insert into city values('Stockholm','S',711119);
    insert into city values('London','GB',6967500);
    insert into locatedon values('Ajaccio','F','Corse');
    insert into locatedon values('Palma','E','Mallorca');
    insert into locatedon values('London','GB','Great Britain');
    -- all countries that have a city that is not located on
    -- some island: should be E, F, S.
    Select c.name
    From country c
    Where exists
    ((Select name
    From city
    Where city.country=c.code)
    minus
    (Select city
    From locatedon
    Where locatedon.country=c.code)
    -- wrong answer: only Sweden; Spain and France not in the answer!
    select distinct country from
    ((Select name, country
    From city)
    minus
    (Select city, country
    From locatedon)
    -- correct answer: E, F, S
    Comments:
    The bug has been found by students in our SQL course.
    Using a larger database from that course, the bug can be reproduced
    (same queries as above) at
    http://www.semwebtech.org/sqlfrontend/
    (wrong: 142 answers, correct: 154 answers)
    During reducing it to a simple sample, there were some interesting
    observations: trying with smaller and simpler tables (without the keys)
    and synthetic data, the bug did not occur immediately. When
    restating the query after about one day, the bug occurred. Obviously,
    Oracle creates some index on its own in course of its internal
    optimization that (or more exactly, its usage) exhibits the bug. The
    query plan (showed in SQL Developer) was the same before and after.
    Wolfgang

    There's a typo in the test data - GB should presumably not be in Sweden. However....
    the bug did not occur immediatelyIt's possible. But what would have almost certainly happened is that the execution plan DID change at some point. There are various reasons why it might not be immediate.
    Obviously, Oracle creates some index on its own in course of its internal optimizationFar from obvious, what are you on about?
    The query plan was the same before and afterBet you it wasn't.
    A clear illustration of the issue and indication that it must be a bug is below.
    Simply by hinting a different access method, we can change the result. Therefore, bug.
    See [url http://support.oracle.com]Oracle Support and search for "wrong results".
    Please raise with Oracle Support to get confirmation of bug.
    There have been so many wrong results bugs recently, it's getting ridiculous.
    It's a real issue, IMHO.
    If you can't trust the DB to get your data right....
    Note that the query plan is very much NOT the same and it is the difference in query plan which s that is the root cause of the bug.
    SQL> select * from v$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    PL/SQL Release 11.2.0.2.0 - Production
    CORE    11.2.0.2.0      Production
    TNS for Linux: Version 11.2.0.2.0 - Production
    NLSRTL Version 11.2.0.2.0 - Production
    SQL> SELECT c.name
      2  FROM   country1 c
      3  WHERE  exists ((SELECT name
      4                  FROM   city1
      5                  WHERE  city1.country=c.code)
      6                  MINUS
      7                 (SELECT city
      8                  FROM   locatedon1
      9                  WHERE  locatedon1.country=c.code));
    NAME
    Sweden
    SQL> SELECT /*+ full(c) */
      2         c.name
      3  FROM   country1 c
      4  WHERE  exists ((SELECT name
      5                  FROM   city1
      6                  WHERE  city1.country=c.code)
      7                  MINUS
      8                 (SELECT city
      9                  FROM   locatedon1
    10                  WHERE  locatedon1.country=c.code));
    NAME
    Spain
    France
    Sweden
    SQL> explain plan for
      2  SELECT c.name
      3  FROM   country1 c
      4  WHERE  exists ((SELECT name
      5                  FROM   city1
      6                  WHERE  city1.country=c.code)
      7                  MINUS
      8                 (SELECT city
      9                  FROM   locatedon1
    10                  WHERE  locatedon1.country=c.code));
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 156929629
    | Id  | Operation                    | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT             |            |     1 |    27 |    12  (25)| 00:00:01 |
    |   1 |  NESTED LOOPS                |            |       |       |            |          |
    |   2 |   NESTED LOOPS               |            |     1 |    27 |    12  (25)| 00:00:01 |
    |   3 |    VIEW                      | VW_SQ_1    |     6 |    24 |    10  (20)| 00:00:01 |
    |   4 |     MINUS                    |            |       |       |            |          |
    |   5 |      SORT UNIQUE             |            |     6 |   138 |            |          |
    |   6 |       TABLE ACCESS FULL      | CITY1      |     6 |   138 |     4   (0)| 00:00:01 |
    |   7 |      SORT UNIQUE             |            |     3 |    69 |            |          |
    |   8 |       TABLE ACCESS FULL      | LOCATEDON1 |     3 |    69 |     4   (0)| 00:00:01 |
    |*  9 |    INDEX UNIQUE SCAN         | COUNTRYKEY |     1 |       |     0   (0)| 00:00:01 |
    |  10 |   TABLE ACCESS BY INDEX ROWID| COUNTRY1   |     1 |    23 |     1   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       9 - access("VW_COL_1"="C"."CODE")
    Note
       - dynamic sampling used for this statement (level=4)
    26 rows selected.
    SQL> explain plan for
      2  SELECT /*+ full(c) */
      3         c.name
      4  FROM   country1 c
      5  WHERE  exists ((SELECT name
      6                  FROM   city1
      7                  WHERE  city1.country=c.code)
      8                  MINUS
      9                 (SELECT city
    10                  FROM   locatedon1
    11                  WHERE  locatedon1.country=c.code));
    Explained.
    SQL> select * from table(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 1378726376
    | Id  | Operation            | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT     |            |     1 |    23 |    14  (15)| 00:00:01 |
    |*  1 |  FILTER              |            |       |       |            |          |
    |   2 |   TABLE ACCESS FULL  | COUNTRY1   |     4 |    92 |     4   (0)| 00:00:01 |
    |   3 |   MINUS              |            |       |       |            |          |
    |   4 |    SORT UNIQUE       |            |     1 |    23 |     5  (20)| 00:00:01 |
    |*  5 |     TABLE ACCESS FULL| CITY1      |     1 |    23 |     4   (0)| 00:00:01 |
    |   6 |    SORT UNIQUE       |            |     1 |    23 |     5  (20)| 00:00:01 |
    |*  7 |     TABLE ACCESS FULL| LOCATEDON1 |     1 |    23 |     4   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       1 - filter( EXISTS ( (SELECT "NAME" FROM "CITY1" "CITY1" WHERE
                  "CITY1"."COUNTRY"=:B1)MINUS (SELECT "CITY" FROM "LOCATEDON1" "LOCATEDON1"
                  WHERE "LOCATEDON1"."COUNTRY"=:B2)))
       5 - filter("CITY1"."COUNTRY"=:B1)
       7 - filter("LOCATEDON1"."COUNTRY"=:B1)
    Note
       - dynamic sampling used for this statement (level=4)
    27 rows selected.Just to show that it's related to query transformation:
    SQL> SELECT /*+ 
      2             no_query_transformation
      3         */
      4         c.name
      5  FROM   country1 c
      6  WHERE  exists ((SELECT name
      7                  FROM   city1
      8                  WHERE  city1.country=c.code)
      9                  MINUS
    10                 (SELECT city
    11                  FROM   locatedon1
    12                  WHERE  locatedon1.country=c.code));
    NAME
    Spain
    France
    Sweden
    SQL> Edited by: Dom Brooks on Jun 30, 2011 2:50 PM

  • Top-n analysis in oracle 8i

    How to use top-n analysis in oracle 8i?
    I mean,take a example.
    I am maintaining a database of a 1000 employees.I want to display the names of the employees who are getting top 10 salaries(more further top 100 salaries) using a SQL query in oracle 8i only.Please answer my problem.

    Sorry, my suggestion will return 10 emp with highest salaries, not all employees with 10 highest salaries. To get all employees with 10 highest salaries in 8i:
    SQL> SELECT  ename,
      2          sal
      3    FROM  emp
      4    WHERE sal IN (
      5                  SELECT  sal
      6                    FROM  (
      7                           SELECT  sal
      8                             FROM  emp
      9                             GROUP BY sal
    10                             ORDER BY sal DESC
    11                          )
    12                    WHERE rownum <= 10
    13                 )
    14  /
    ENAME             SAL
    KING             5000
    FORD             3000
    SCOTT            3000
    JONES            2975
    BLAKE            2850
    CLARK            2450
    ALLEN            1600
    TURNER           1500
    MILLER           1300
    MARTIN           1250
    WARD             1250
    ENAME             SAL
    ADAMS            1100
    12 rows selected.
    SQL> SY.

  • BOM Performance Analysis using SAT or SE30 t-code?

    Hi,
        Currently I'm doing performance analysis of BOM to find out the bottlenecks. I'm doing the analysis using SAT or SE30 t-code.
    From my understanding, BOM performance depends upon
    1. no. of BOM items
    2. BOM Level
    3. BOM Evolution over time
    Can anyone provide me a hint to find out the bottlenecks in BOM performance. Thanks!!
    Regards,
    Saravana

    I have done the BOM Performance Analysis. Based on the most execution time and top performance consumers, please find my observations…
    Functionally, Performance of BOM execution depends upon
    Number of BOM items available in the BOM
    Number of Assemblies present in the BOM
    BOM Levels
    BOM Evolution over time
    Technically, BOM execution time is purely depends upon
    fetching the data from the SAP tables
    hitting the same table again and again when goes down the level
    loop at the tables / Nested functions used in SAP Program
    Hence, I'm closing this thread.....

  • What is Top-N analysis

    What is Top-N analysis.?? can some one explain. I encountered this while going thru
    the Sql book
    The ORDER BY clause in the subquery is not
    needed unless you are performing Top-N analysis.

    That means you are doing something like this in order to get top five paid people::
    SELECT * FROM
    (  SELECT empno, ename, sal+nvl(comm,0) as remuneration
       FROM   emp
       ORDER BY remuneration DESC )
    WHERE rownum <= 5
    /We have to do the ORDER BY in a sub-query because rownum gets applied before the rows are sorted.
    Cheers, APC

  • Performing Top-N Analysis

    An Oracle University Material in Sql Says
    The high-level structure of a Top-N analysis query is:
    SELECT [column_list], ROWNUM
    FROM (SELECT [column_list]
    FROM table
    ORDER BY Top-N_column)
    WHERE ROWNUM <= N;
    For example to display the top three earner names and salaries from the EMPLOYEES table:
    SELECT ROWNUM as RANK, last_name, salary
    FROM (SELECT last_name,salary FROM employees
    ORDER BY salary DESC)
    WHERE ROWNUM <= 3;
    My question is
    If, instead of this query, I write
    1)
    SELECT ROWNUM as RANK, last_name, salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    or
    2)
    SELECT ROWNUM as RANK, last_name, salary
    FROM ( SELECT last_name,salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    is any difference?
    The results in schema hr are the same.............
    Thank you

    is any difference? yes, there is!
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5             --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT  num FROM t ORDER BY num desc)
      8   WHERE ROWNUM <= 3;
          RANK        NUM
             1          4
             2          3
             3          2
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM t
      8   WHERE ROWNUM <= 3
      9   ORDER BY num DESC
    10  /
          RANK        NUM
             2          4
             3          3
             1          1
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT num FROM t
      8          WHERE ROWNUM <= 3
      9          ORDER BY num DESC)
    10  /
          RANK        NUM
             1          4
             2          3
             3          1
    SQL> rownum is confered before ordering is made.
    Thats why you should place the subquery with ordering in the inline view.

  • JDOQL Correlated Subquery - Bad SQL

    Hi,
    When I execute a JDOQL correlated subquery, the generated SQL is either
    invalid or incorrect. Exactly what happens depends on the exact query, and
    on the target database type, but I believe it all stems from the same
    problem, which has to do with table aliasing.
    If you need further details to reproduce this, please let me know. I'll be
    glad to help in any way I can to get this situation remedied quickly, as I
    am depending on this functionality. I have a test application to
    demonstrate the problem.
    I'm using Kodo 3.3.3 and application identity.
    Paul Mogren
    CommerceHub

    For the record, this is in part due to a bug in Kodo's SQL92 joining.
    See http://bugzilla.solarmetric.com/show_bug.cgi?id=1156
    -Patrick
    Paul Mogren wrote:
    Certainly... Here's a simple example using Microsoft's JDBC Driver for SQL
    Server 2000, and kodo.jdbc.sql.SQLServerDictionary, which produces invalid
    SQL.
    The query:
    pm.newQuery(Container.class,
    "(select from Entry entry where entries.contains(entry) &&
    entry.containedId != 1).isEmpty()");
    The classes:
    class Contained {
    private int id; //pk
    class Container {
    private int id; //pk
    private Set entries = new HashSet(); //<Entry>
    class Entry {
    private int containerId; //pk
    private int containedId; //pk
    private Container container; //persistent-redundant
    private Contained contained; //persistent-redundant
    The result:
    Incorrect syntax near the keyword 'WHERE'. {prepstmnt 31598780 SELECT
    t0.container_id, t0.lock FROM  WHERE (NOT EXISTS (SELECT DISTINCT
    t2.contained_id, t2.container_id FROM dbo.entry t2 WHERE (t1.contained_id
    = t2.contained_id AND t1.container_id = t2.container_id AND
    t2.contained_id <> ?) AND t0.container_id = t1.container_id))
    [params=(int) 1]} [code=156, state=HY000]
    Patrick Linskey wrote:
    Hi Paul,
    Kodo's correlated subquery support does have some known limitations. Can
    you post a sample JDOQL statement + corresponding SQL statement?
    -Patrick
    Paul Mogren wrote:
    Hi,
    When I execute a JDOQL correlated subquery, the generated SQL is either
    invalid or incorrect. Exactly what happens depends on the exact query, and
    on the target database type, but I believe it all stems from the same
    problem, which has to do with table aliasing.
    If you need further details to reproduce this, please let me know. I'll be
    glad to help in any way I can to get this situation remedied quickly, as I
    am depending on this functionality. I have a test application to
    demonstrate the problem.
    I'm using Kodo 3.3.3 and application identity.
    Paul Mogren
    CommerceHub

  • Getting Top N Value using SQL

    There are thousands of records in a table. The table has column TOTAL_NUMBER. I want to find the top 3rd using the column TOTAL_NUMBER. How can I do this using SQL?

    Google:
    Using RANK() to Obtain a Top-N Query
    To obtain a top-N query, use RANK() in a subquery and then apply a filter condition outside the subquery:
    SELECT Empno, Ename, Job, Mgr, Hiredate, Sal
    FROM
    (SELECT Empno, Ename, Job, Mgr, Hiredate, Sal,
    RANK() OVER
    (ORDER BY SAL Desc NULLS LAST) AS Emp_Rank
    FROM Emp
    ORDER BY SAL Desc NULLS LAST)
    WHERE Emp_Rank < 6;

  • How does oracle execute a correlated subquery .... some confusion

    How does oracle 10g execute a correlated subquery?
    I read some articles online & i am a little confused.
    example:
    select * from emp e
    where e.deptno in (select d.deptno from dept d
    where e.deptno = d.deptno);
    My questions .......
    1.In the above example, does oracle read the entire outer table first and then run the inner query using the rows returned by the outer query?
    I read in some articles that they execute simultaneously.
    How does this work?
    2.Should the inner query have lesser amount of rows compared to the outer query for a good performance?
    3.Can every correlated subquery be converted to a join and if so which one to use?
    Truly appreciate any inputs on how oracle executes it at the backend.
    Thanks in advance.

    user10541890 wrote:
    How does oracle 10g execute a correlated subquery?
    I read some articles online & i am a little confused.
    example:
    select * from emp e
    where e.deptno in (select d.deptno from dept d
    where e.deptno = d.deptno);
    My questions .......
    1.In the above example, does oracle read the entire outer table first and then run the inner query using the rows returned by the outer query?
    I read in some articles that they execute simultaneously.
    How does this work?SQL is not a procedural language. SQL code specifies what the system sill do, not how the system wlll do it; that's entirely up to the system.
    What does it matter to you whether the two are done together, or if one is completed before the other begins?
    The system will probably choose to run ucorellated subqueiris only once, and correlated queries multiple times as needed.
    2.Should the inner query have lesser amount of rows compared to the outer query for a good performance?That usually doesn't matter.
    It some cases, you may want to consider whether the subquery is correlated or not. If the subquery is very costly, and produces, say, 1 million rows, but you know the main query will only produce about 5 rows, then you may want to do a correlated subquery rather than an uncorrelated one.
    3.Can every correlated subquery be converted to a join and if so which one to use?I believe so.
    Use whichever is easier to code and debug. That will change depnding on the data and the requirements.
    If performance is an issue, try different ways. Usually, where I've noticed a big difference, join was fastest.
    By the way, it's unusual to have a correlated IN-subquery.
    Usually IN-subqueris are uncorrelated, like this:
    select  *
    from      emp     e
    where     e.deptno     in ( select  d.deptno
                        from    dept     d
                      );(This and the queries below produce the same resutls as your original query.)
    Correlated subqueries are usually used for scalar subqueries or EXISTS subqueries, like this:
    select  *
    from      emp     e
    where     EXISTS ( select  d.deptno
               from    dept     d
                    where   e.deptno = d.deptno
                );To do the same thing with a join:
    select  e.*
    from      emp     e
    join     dept     d     on     e.deptno     = d.deptno
    ;assuming dept.deptno is unique.

  • Generated a report which gives PR(Purchase Requisition) analysis using ALV.

    hi experts,
    please give me tables and fields for following report, and also exlain me briefly,
    Generated a report which gives PR(Purchase Requisition) analysis using ALV.
    thanks in advance,
    radhakrishna

    Hi
    please find this report which link SO PO PR and Prd Ord and there status.
    >
    REPORT z_so_info.
    TABLES: vbak, vbap, afko, afpo.
    *Field catalog
    TYPE-POOLS: slis.
    DATA: lv_repid TYPE sy-repid VALUE sy-repid,
    xfield TYPE slis_t_fieldcat_alv,
    afield TYPE slis_fieldcat_alv.
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 1(25) text-002.
    SELECT-OPTIONS: so_so FOR vbak-vbeln OBLIGATORY.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN END OF BLOCK b1.
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-003.
    PARAMETERS:
    p_kunnr LIKE vbak-kunnr, " sold-to
    p_kunwe LIKE vbak-kunnr. " ship-to
    SELECTION-SCREEN END OF BLOCK b2.
    *Constants
    CONSTANTS: c_zor TYPE vbak-auart VALUE 'ZOR',
    c_we TYPE vbpa-parvw VALUE 'WE',
    c_ag TYPE vbpa-parvw VALUE 'AG'.
    c_space TYPE space.
    *Ranges
    RANGES: ra_parvw FOR vbpa-parvw,
    ra_kunnr FOR vbpa-kunnr.
    *Tables
    DATA: BEGIN OF gt_output OCCURS 0,
    vbeln LIKE vbak-vbeln, " sales order number
    posnr LIKE vbap-posnr, " SO item number
    matnr LIKE vbap-matnr, " material number
    sh LIKE vbpa-kunnr, " Ship-to
    sp LIKE vbpa-kunnr, " Sold-to
    lifnr LIKE ekko-lifnr, " Vendor
    bstnk LIKE vbak-bstnk, " PO number
    banfn LIKE vbep-banfn, " Purchase requi
    po_st TYPE char30, " PO status text
    pstyv TYPE vbap-pstyv, " Item catagory
    aufnr LIKE afpo-aufnr, " Production Order
    prd_stat TYPE string, " Prd order status
    END OF gt_output.
    DATA: wa_output LIKE gt_output.
    FIELD-SYMBOLS: <fs_output> LIKE gt_output.
    *Table for sales order and PO
    TYPES : BEGIN OF gs_data,
    vbeln TYPE vbak-vbeln,
    posnr TYPE vbap-posnr,
    pstyv TYPE vbap-pstyv,
    matnr TYPE vbap-matnr,
    END OF gs_data.
    DATA: gt_data TYPE STANDARD TABLE OF gs_data,
    wa_data TYPE gs_data.
    *Table for Production Orders
    TYPES: BEGIN OF gs_prd,
    aufnr TYPE afpo-aufnr,
    posnr TYPE afpo-posnr,
    kdauf TYPE afpo-kdauf,
    kdpos TYPE afpo-kdpos,
    wepos TYPE afpo-wepos, "Goods Receipt Indicator
    elikz TYPE afpo-elikz, "Delivery Completed Indicator
    objnr TYPE aufk-objnr, "Object number
    getri TYPE afko-getri, "Confirmed Order Finish Date
    gltri TYPE afko-gltri, "Actual finish date
    END OF gs_prd.
    DATA: gt_prd TYPE STANDARD TABLE OF gs_prd,
    wa_prd TYPE gs_prd.
    *Table for partner data
    TYPES: BEGIN OF gs_partner,
    vbeln TYPE vbak-vbeln,
    posnr TYPE vbap-posnr,
    parvw TYPE vbpa-parvw,
    kunnr TYPE vbpa-kunnr,
    END OF gs_partner.
    DATA: gt_partner TYPE STANDARD TABLE OF gs_partner,
    wa_partner TYPE gs_partner.
    TYPES: BEGIN OF gs_po,
    ebeln TYPE ekkn-ebeln,
    ebelp TYPE ekkn-ebelp,
    vbeln TYPE ekkn-vbeln,
    vbelp TYPE ekkn-vbelp,
    END OF gs_po.
    DATA: gt_po TYPE STANDARD TABLE OF gs_po,
    wa_po TYPE gs_po.
    TYPES: BEGIN OF gs_preq,
    vbeln TYPE vbep-vbeln,
    posnr TYPE vbep-posnr,
    banfn TYPE vbep-banfn,
    END OF gs_preq.
    DATA: gt_preq TYPE STANDARD TABLE OF gs_preq,
    wa_preq TYPE gs_preq.
    TYPES: BEGIN OF gs_po_stat,
    ebeln TYPE ekko-ebeln,
    procstat TYPE ekko-procstat,
    lifnr TYPE ekko-lifnr,
    END OF gs_po_stat.
    DATA: gt_po_stat TYPE STANDARD TABLE OF gs_po_stat,
    wa_po_stat TYPE gs_po_stat.
    *Field symbols
    FIELD-SYMBOLS: <fs> TYPE tj02t-txt04,
    <fs_temp> TYPE tj02t-txt04,
    <fs_stat> TYPE char30.
    START-OF-SELECTION.
    PERFORM fr_build_range.
    PERFORM fr_get_data.
    PERFORM fr_build_fc.
    PERFORM fr_output.
    *& Form fr_get_data
    text
    --> p1 text
    <-- p2 text
    FORM fr_get_data.
    *Get SO
    SELECT avbeln aposnr apstyv amatnr
    FROM vbap AS a
    JOIN vbak AS b
    ON avbeln = bvbeln
    JOIN vbpa AS c
    ON bvbeln = cvbeln
    INTO TABLE gt_data
    WHERE b~vbeln IN so_so
    AND b~auart EQ c_zor "Only Sales Orders
    AND c~kunnr IN ra_kunnr. "from selection screen
    DELETE ADJACENT DUPLICATES FROM gt_data COMPARING vbeln posnr.
    *get data of the production order
    IF NOT gt_data[] IS INITIAL.
    SELECT aaufnr aposnr akdauf akdpos awepos aelikz
    b~objnr
    FROM afpo AS a
    JOIN aufk AS b
    ON aaufnr = baufnr
    INTO TABLE gt_prd
    FOR ALL ENTRIES IN gt_data
    WHERE a~kdauf EQ gt_data-vbeln
    AND a~kdpos EQ gt_data-posnr.
    ENDIF.
    *Get partner data
    IF NOT gt_data[] IS INITIAL.
    SELECT vbeln posnr parvw kunnr
    FROM vbpa
    INTO TABLE gt_partner
    FOR ALL ENTRIES IN gt_data
    WHERE vbeln EQ gt_data-vbeln.
    ENDIF.
    *Get Purchase Order
    IF NOT gt_data[] IS INITIAL.
    SELECT ebeln ebelp vbeln vbelp
    FROM ekkn
    INTO TABLE gt_po
    FOR ALL ENTRIES IN gt_data
    WHERE vbeln EQ gt_data-vbeln
    AND vbelp EQ gt_data-posnr.
    SELECT vbeln posnr banfn
    FROM vbep
    INTO TABLE gt_preq
    FOR ALL ENTRIES IN gt_data
    WHERE vbeln EQ gt_data-vbeln
    AND posnr EQ gt_data-posnr.
    ENDIF.
    IF NOT gt_po[] IS INITIAL.
    SELECT aebeln aprocstat a~lifnr
    FROM ekko AS a
    JOIN ekpo AS b
    ON aebeln = bebeln
    INTO TABLE gt_po_stat
    FOR ALL ENTRIES IN gt_po
    WHERE b~ebeln EQ gt_po-ebeln
    AND b~ebelp EQ gt_po-ebelp.
    ENDIF.
    *Move data to output table
    LOOP AT gt_data INTO wa_data.
    wa_output-vbeln = wa_data-vbeln.
    wa_output-posnr = wa_data-posnr.
    wa_output-pstyv = wa_data-pstyv.
    wa_output-matnr = wa_data-matnr.
    READ TABLE gt_po INTO wa_po WITH KEY vbeln = wa_data-vbeln
    vbelp = wa_data-posnr.
    IF sy-subrc EQ 0.
    wa_output-bstnk = wa_po-ebeln.
    READ TABLE gt_po_stat INTO wa_po_stat WITH KEY ebeln = wa_po-ebeln.
    IF sy-subrc EQ 0.
    wa_output-lifnr = wa_po_stat-lifnr.
    CASE wa_po_stat-procstat.
    WHEN '01'.
    wa_output-po_st = 'Version in process'.
    WHEN '02'.
    wa_output-po_st = 'Active'.
    WHEN '03'.
    wa_output-po_st = 'In release'.
    WHEN '04'.
    wa_output-po_st = 'Partially released'.
    WHEN '05'.
    wa_output-po_st = 'Released Completely'.
    WHEN '08'.
    wa_output-po_st = 'Rejected'.
    ENDCASE.
    ENDIF. "inner read subrc
    ENDIF. "outer read subrc
    READ TABLE gt_preq INTO wa_preq WITH KEY vbeln = wa_data-vbeln
    posnr = wa_data-posnr.
    IF sy-subrc EQ 0.
    wa_output-banfn = wa_preq-banfn.
    ENDIF.
    READ TABLE gt_prd INTO wa_prd WITH KEY kdauf = wa_data-vbeln
    kdpos = wa_data-posnr.
    IF sy-subrc EQ 0.
    wa_output-aufnr = wa_prd-aufnr.
    *get the purchase requisition for production order as well
    SELECT SINGLE banfn
    FROM ebkn
    INTO wa_output-banfn
    WHERE aufnr EQ wa_prd-aufnr.
    *Get the status of the production order
    PERFORM fr_get_prd_stat USING wa_prd-objnr
    CHANGING wa_output-prd_stat.
    ENDIF. " sy-subrc
    READ TABLE gt_partner INTO wa_partner WITH KEY vbeln = wa_data-vbeln
    parvw = c_we.
    IF sy-subrc EQ 0.
    wa_output-sh = wa_partner-kunnr.
    ENDIF.
    READ TABLE gt_partner INTO wa_partner WITH KEY vbeln = wa_data-vbeln
    parvw = c_ag.
    IF sy-subrc EQ 0.
    wa_output-sp = wa_partner-kunnr.
    ENDIF.
    APPEND wa_output TO gt_output.
    CLEAR: wa_data, wa_prd,wa_partner,wa_output.
    ENDLOOP.
    ENDFORM. " fr_get_data
    *& Form fr_build_range
    text
    --> p1 text
    <-- p2 text
    FORM fr_build_range .
    *Range for partner function
    MOVE 'I' TO ra_parvw-sign.
    MOVE 'EQ' TO ra_parvw-option.
    MOVE 'SH' TO ra_parvw-low. " we
    APPEND ra_parvw.
    CLEAR ra_parvw.
    MOVE 'I' TO ra_parvw-sign.
    MOVE 'EQ' TO ra_parvw-option.
    MOVE 'SP' TO ra_parvw-low. " ag
    APPEND ra_parvw.
    CLEAR ra_parvw.
    *Range for ship-to and sold-to
    MOVE 'I' TO ra_kunnr-sign.
    MOVE 'EQ' TO ra_kunnr-option.
    MOVE p_kunnr TO ra_kunnr-low.
    APPEND ra_kunnr.
    CLEAR ra_kunnr.
    MOVE 'I' TO ra_kunnr-sign.
    MOVE 'EQ' TO ra_kunnr-option.
    MOVE p_kunwe TO ra_kunnr-low.
    APPEND ra_kunnr.
    CLEAR ra_kunnr.
    ENDFORM. " fr_build_range
    *& Form fr_build_fc
    text
    --> p1 text
    <-- p2 text
    FORM fr_build_fc .
    sales order number
    afield-fieldname = 'VBELN'.
    afield-seltext_s = 'Sales #'.
    afield-ref_tabname = 'VBAK'.
    APPEND afield TO xfield. CLEAR afield.
    sales ITEM number
    afield-fieldname = 'POSNR'.
    afield-seltext_s = 'Item #'.
    afield-ref_tabname = 'VBAP'.
    APPEND afield TO xfield. CLEAR afield.
    Material Number
    afield-fieldname = 'MATNR'.
    afield-seltext_s = 'Material #'.
    afield-ref_tabname = 'VBAP'.
    afield-outputlen = 10.
    APPEND afield TO xfield. CLEAR afield.
    *Vendor Number
    afield-fieldname = 'LIFNR'.
    afield-seltext_s = 'Vendor Num.'.
    afield-ref_tabname = 'EKKO'.
    APPEND afield TO xfield. CLEAR afield.
    ship-to-party
    afield-fieldname = 'SH'.
    afield-seltext_s = 'Ship-to'.
    afield-ref_tabname = 'VBPA'.
    APPEND afield TO xfield. CLEAR afield.
    sold-to-party
    afield-fieldname = 'SP'.
    afield-seltext_s = 'Sold-to'.
    afield-ref_tabname = 'VBPA'.
    APPEND afield TO xfield. CLEAR afield.
    *PO number
    afield-fieldname = 'BSTNK'.
    afield-seltext_s = 'PO NUM'.
    afield-ref_tabname = 'EKKO'.
    APPEND afield TO xfield. CLEAR afield.
    *PO status
    afield-fieldname = 'PO_STAT'.
    afield-seltext_s = 'Step'.
    afield-ref_tabname = 'zbacklog_v2'.
    APPEND afield TO xfield. CLEAR afield.
    *PO step status
    afield-fieldname = 'PO_ST'.
    afield-seltext_s = 'PO.Status'.
    afield-ref_tabname = 'zbacklog_v2'.
    APPEND afield TO xfield. CLEAR afield.
    *Purchase requisition
    afield-fieldname = 'BANFN'.
    afield-seltext_s = 'Pur. Req.'.
    afield-ref_tabname = 'VBEP'.
    APPEND afield TO xfield. CLEAR afield.
    *Item catagory
    afield-fieldname = 'PSTYV'.
    afield-seltext_s = 'Itm. Catg'.
    afield-ref_tabname = 'VBAP'.
    APPEND afield TO xfield. CLEAR afield.
    *Prodcution Order number
    afield-fieldname = 'AUFNR'.
    afield-seltext_m = 'Prod.Order'.
    afield-ref_tabname = 'AFKO'.
    APPEND afield TO xfield. CLEAR afield.
    *PRODCUTION status
    afield-fieldname = 'PRD_STAT'.
    afield-seltext_s = 'Prd. Step'.
    afield-ref_tabname = 'zbacklog_v2'.
    afield-outputlen = 20.
    APPEND afield TO xfield. CLEAR afield.
    *PRODUCTION step status
    afield-fieldname = 'PRD_ST'.
    afield-seltext_s = 'Prd. Status'.
    afield-ref_tabname = 'zbacklog_v2'.
    APPEND afield TO xfield. CLEAR afield.
    ENDFORM. " fr_build_fc
    *& Form fr_output
    text
    --> p1 text
    <-- p2 text
    FORM fr_output .
    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
    i_callback_program = sy-repid
    I_CALLBACK_PF_STATUS_SET = ' '
    I_CALLBACK_USER_COMMAND = ' '
    I_STRUCTURE_NAME =
    IS_LAYOUT =
    it_fieldcat = xfield[]
    i_default = 'X'
    i_save = 'A'
    TABLES
    t_outtab = gt_output
    EXCEPTIONS
    program_error = 1
    OTHERS = 2.
    ENDFORM. " fr_output
    *& Form fr_get_prd_stat
    text
    -->P_WA_PRD_OBJNR text
    FORM fr_get_prd_stat USING p_objnr CHANGING p_prd_stat.
    DATA: lt_status TYPE STANDARD TABLE OF jstat,
    wa_status TYPE jstat,
    lv_status TYPE tj02t-txt04,
    lv_temp2 TYPE char5,
    lv_buff TYPE string.
    CALL FUNCTION 'STATUS_READ'
    EXPORTING
    client = sy-mandt
    objnr = p_objnr
    only_active = 'X'
    TABLES
    status = lt_status
    EXCEPTIONS
    object_not_found = 1
    OTHERS = 2.
    LOOP AT lt_status INTO wa_status.
    IF wa_status-stat(1) EQ 'I'.
    SELECT txt04 FROM tj02t
    INTO lv_status
    WHERE istat EQ wa_status-stat
    AND spras EQ 'E'.
    ENDSELECT.
    lv_temp2 = lv_status.
    CONCATENATE lv_temp2 p_prd_stat INTO p_prd_stat
    SEPARATED BY ','.
    ENDIF.
    CLEAR: wa_status, lv_status, lv_temp2.
    ENDLOOP.
    lv_buff = p_prd_stat.
    *Status of Production Order
    IF lv_buff CS 'CRTD'.
    p_prd_stat = 'Not Active'.
    ENDIF.
    IF lv_buff CS 'REL'.
    IF lv_buff CS 'GMPS'.
    p_prd_stat = 'Printed In Prod'.
    ELSE.
    p_prd_stat = 'Printed'.
    ENDIF.
    ENDIF.
    IF lv_buff CS 'TECO'.
    p_prd_stat = 'Technically Compt.'.
    ENDIF.
    ENDFORM. " fr_get_prd_stat
    >

  • Allignment in ALV top of page using oops

    I am displaying some fields with their values on top of page using Classes.but i am not able to get a proper alignment.
    i want the field and their respective values one-below the other with once the field description and the values below after the description. eg...
      field : 0125465
               0123654
               23654100
    sales : au01
               sd02
               GH26
    org    : 101
               102
               103

    Check this sample..
    Take the code from the HTML_TOP_OF_PAGE from the below report.
    REPORT  ztest_page.
    DATA : it_flight TYPE TABLE OF sflight WITH HEADER LINE.
    data: t type x.
    START-OF-SELECTION.
      SELECT * FROM sflight INTO TABLE it_flight.
    END-OF-SELECTION.
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program          = sy-repid
          i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
          i_callback_top_of_page      = 'TOP_OF_PAGE'
          i_structure_name            = 'SFLIGHT'
        TABLES
          t_outtab                    = it_flight
        EXCEPTIONS
          program_error               = 1
          OTHERS                      = 2.
    *&      Form  TOP_OF_PAGE
    *       text
    FORM top_of_page.
      WRITE 'This is the Top of page which triggers in print'.
    ENDFORM.                    "TOP_OF_PAGE
    *&      Form  html_top_of_page
    FORM html_top_of_page USING document TYPE REF TO cl_dd_document.
      DATA : dl_text(255) TYPE c.  "Text
    * Add new-line
      CALL METHOD document->new_line.
      CALL METHOD document->new_line.
      CLEAR : dl_text.
    * program ID
      dl_text = 'Program Name :'.
      CALL METHOD document->add_gap.
      CALL METHOD document->add_text
        EXPORTING
          text         = dl_text
          sap_emphasis = cl_dd_area=>heading
          sap_color    = cl_dd_area=>list_heading_int.
      CLEAR dl_text.
      dl_text = sy-repid.
      CALL METHOD document->add_text
        EXPORTING
          text         = dl_text
          sap_emphasis = cl_dd_area=>heading
          sap_color    = cl_dd_area=>list_negative_inv.
    * Add new-line
      CALL METHOD document->new_line.
      CLEAR : dl_text.
      dl_text = sy-uname.
      CALL METHOD document->add_gap
        EXPORTING
          width = 34.
      CALL METHOD document->add_text
        EXPORTING
          text         = dl_text
          sap_emphasis = cl_dd_area=>heading
          sap_color    = cl_dd_area=>list_negative_inv.
    * Add new-line
      CALL METHOD document->new_line.
      CLEAR : dl_text.
      CALL METHOD document->add_gap
        EXPORTING
          width = 34.
    * Move date
      WRITE sy-datum TO dl_text.
      CALL METHOD document->add_text
        EXPORTING
          text         = dl_text
          sap_emphasis = cl_dd_area=>heading
          sap_color    = cl_dd_area=>list_negative_inv.
    * Add new-line
      CALL METHOD document->new_line.
      CLEAR : dl_text.
    * Move time
      WRITE sy-uzeit TO dl_text.
      CALL METHOD document->add_gap
        EXPORTING
          width = 34.
      CALL METHOD document->add_text
        EXPORTING
          text         = dl_text
          sap_emphasis = cl_dd_area=>heading
          sap_color    = cl_dd_area=>list_negative_inv.
    * Add new-line
      CALL METHOD document->new_line.
    ENDFORM.                    "HTML_TOP_OF_PAGE
    same Code can be used in the class. you take the above code and see..

Maybe you are looking for

  • Error 1434 - what do I do???

    Hi I bought my 160gb ipod earlier this year and have not had any problems until now. It stopped working so went online and followed the instructions on to get an error message 1434 - that has no solution to it via the troubleshooter. How can I resolv

  • Projects data by GL segments

    Hi All, 1. We got a requirement to pull the project expenditure cost, revenue, budget & forecast data from Oracle, w.r.t gl code combination wise. As expenditures and revenues are posted to GL, (also based on cost distribution lines and pa events tab

  • ABAP run time error SAPLMCEX

    Hi, When i am in RSA3 (Extractor checker in R/3)... Entered a DataSource name and then press entered.. But after extraction...some ABAP runtime error is occuring SAPLMCEX... Pls suggest me for further process... Thanks

  • Entering letters using number keys only?

    How do I enter letters using only the number keypad, like on a regular phone?  Sometimes when I call a business, I get their automated directory, and I have to enter a few letters of the person's name using the number keypad.  How do you do that on a

  • Can't get rid of an ACL

    I have an ACL assigned to a folder. I can't delete it. Keeps popping back in when I click save. Not REAL comfortable with the terminal commands. Any advice?