How to improve 'select query with subselects'?

Oracle Database 11g Release 11.1.0.7.0 - 64bit Production
PL/SQL Release 11.1.0.7.0Hi there,
I'm a C# developer so I really have troubles with understanding PL/SQL. I use a lot of subselects in my queries because I find it easy to understand, however, I need to learn to make use of the power of queries. I made this little library sample to illustrate the problem I have in a smaller scale. I have multiple subselects looking in the same table, running through the same amount of columns to obtain data. This is a waste of performance and resources.
Idea of the query:
Information about a book
- Location of this book
- Number of unique readers of this book
- Number of days it was rent
- Number of reservation of this book
Test data
with
book as (
select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
location as (
select 1 as id, '100.1' as roomnumber from dual union all
select 2 as id, '100.2' as roomnumber from dual union all
select 3 as id, '100.3' as roomnumber from dual
client as (
select 1 as id, 'client001' as name from dual union all
select 2 as id, 'client002' as name from dual union all
select 3 as id, 'client003' as name from dual
book_history as (
select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012') as rentdate from dual union all
select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012') as rentdate  from dual union all
select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate  from dual union all
select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012') as rentdate  from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate  from dual union all
select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012') as rentdate  from dual union all
select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012') as rentdate  from dual
reservation_history as (
select 1 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate from dual union all
select 3 as bookid, 1 as clientid, to_date('04-06-2012') as reservationdate  from dual union all
select 2 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate  from dual union all
select 1 as bookid, 2 as clientid, to_date('05-06-2012') as reservationdate  from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate  from dual union all
select 2 as bookid, 1 as clientid, to_date('06-06-2012') as reservationdate  from dual union all
select 3 as bookid, 1 as clientid, to_date('07-06-2012') as reservationdate  from dual
)Normally my queries look like this
select  b.id,
        b.reference,
        b.title,
        b.description,
        b.author,
        l.roomnumber,
           select count(distinct bh.clientid)
           from   book_history bh
           where  bh.bookid = b.id
           and    bh.rentdate >= to_date('01-01-2012')
           and    bh.rentdate < to_date('01-01-2013')
         ) as different_readers,
        nvl((
           select sum(bh.days)
           from   book_history bh
           where  bh.bookid = b.id
           and    bh.rentdate >= to_date('01-01-2012')
           and    bh.rentdate < to_date('01-01-2013')
         ),0) as days_of_rent,
          select count(1)
          from reservation_history rh
          where rh.bookid = b.id
          and   rh.reservationdate >= to_date('01-01-2012')
          and   rh.reservationdate < to_date('01-01-2013')
        ) as times_reserved
from    book b
join    location l
  on     b.locid = l.id  I tried to improve it to this
  select b.id,
          b.reference,
          b.title,
          b.description,
          b.author,
       l.roomnumber,
       nvl(t1.clients, 0) as different_readers,
       nvl(t1.days, 0) as days_of_rent,
          select count(1)
          from reservation_history rh
          where rh.bookid = b.id
          and   rh.reservationdate >= to_date('01-01-2012')
          and   rh.reservationdate < to_date('01-01-2013')
        ) as times_reserved
from   book b
left join location l
       on b.locid = l.id
left join (select bh.bookid as bookid,
                  count(distinct bh.clientid) as clients,
                  sum(bh.days) as days
           from   book_history bh
           where  bh.rentdate >= to_date('01-01-2012')
           and    bh.rentdate < to_date('01-01-2013')
           group by bh.bookid
           ) t1
        on t1.bookid = b.id  I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select distinct
       b.id,
          b.reference,
          b.title,
          b.description,
          b.author,
       l.roomnumber,
       nvl(count(distinct bh.clientid) over (partition by bh.bookid), 0) as different_readers,
       nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent,
          select count(1)
          from reservation_history rh
          where rh.bookid = b.id
          and   rh.reservationdate >= to_date('01-01-2012')
          and   rh.reservationdate < to_date('01-01-2013')
        ) as times_reserved
from   book b
left join location l
       on b.locid = l.id
left join book_history bh
       on b.id = bh.bookid
      and bh.rentdate >= to_date('01-01-2012')
      and bh.rentdate < to_date('01-01-2013')I suppressed same result rows by using distinct, however I am told to avoid using distinct as this slows down a lot.
Is there any other solution I can use to achieve the same?
I hope my question is clear enough to be answered! Thanks in advance for your time!
Kind regards,
Metroickha
Edited by: 858378 on 6-feb-2013 2:37

Thanks for providing the data in a usable format, that really helps.
858378 wrote:
I am told that I should try to move out the group by from the last left join or try to use partitioning.
Now this is a bit too much for my understanding. When I use partitioning
select b.id,
          b.reference,
          b.title,
          b.description,
          b.author,
l.roomnumber,
count(distinct bh.clientid) over (partition by bh.bookid) as different_readers,
nvl(sum(bh.days) over (partition by bh.bookid),0) as days_of_rent
from   book b
left join location l
on b.locid = l.id
left join book_history bh
on b.id = bh.bookid
and bh.rentdate >= '01-01-2012'
and bh.rentdate < '01-01-2013'I get multiple rows with the same values (as expected). So what do I need to add to remove the multiple same rows?Use a DISTINCT...
SQL> ed
Wrote file afiedt.buf
  1  with book as (
  2      select 1 as id, 'reference0001' as reference, 'titel0001' as title, 'description0001' as description, 'author0001' as author, 1 as locid from dual union all
  3      select 2 as id, 'reference0002' as reference, 'titel0002' as title, 'description0002' as description, 'author0002' as author, 1 as locid from dual union all
  4      select 3 as id, 'reference0003' as reference, 'titel0003' as title, 'description0003' as description, 'author0003' as author, 1 as locid from dual union all
  5      select 4 as id, 'reference0004' as reference, 'titel0004' as title, 'description0004' as description, 'author0004' as author, 2 as locid from dual union all
  6      select 5 as id, 'reference0005' as reference, 'titel0005' as title, 'description0005' as description, 'author0005' as author, 2 as locid from dual union all
  7      select 6 as id, 'reference0006' as reference, 'titel0006' as title, 'description0006' as description, 'author0006' as author, 2 as locid from dual union all
  8      select 7 as id, 'reference0007' as reference, 'titel0007' as title, 'description0007' as description, 'author0007' as author, 3 as locid from dual union all
  9      select 8 as id, 'reference0008' as reference, 'titel0008' as title, 'description0008' as description, 'author0008' as author, 3 as locid from dual
10      ),
11    location as (
12      select 1 as id, '100.1' as roomnumber from dual union all
13      select 2 as id, '100.2' as roomnumber from dual union all
14      select 3 as id, '100.3' as roomnumber from dual
15      ),
16    client as (
17      select 1 as id, 'client001' as name from dual union all
18      select 2 as id, 'client002' as name from dual union all
19      select 3 as id, 'client003' as name from dual
20      ),
21    book_history as (
22      select 1 as bookid, 1 as clientid, 10 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate from dual union all
23      select 3 as bookid, 1 as clientid, 5 as days, to_date('06-06-2012','DD-MM-YYYY') as rentdate  from dual union all
24      select 2 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate  from dual union all
25      select 1 as bookid, 2 as clientid, 5 as days, to_date('07-06-2012','DD-MM-YYYY') as rentdate  from dual union all
26      select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate  from dual union all
27      select 2 as bookid, 1 as clientid, 5 as days, to_date('08-06-2012','DD-MM-YYYY') as rentdate  from dual union all
28      select 3 as bookid, 1 as clientid, 3 as days, to_date('09-06-2012','DD-MM-YYYY') as rentdate  from dual
29      )
30  select  distinct
31          b.id
32         ,b.reference
33         ,b.title
34         ,b.description
35         ,b.author
36         ,l.roomnumber
37         ,count(distinct bh.clientid) over (partition by b.id) as different_readers
38         ,nvl(sum(bh.days) over (partition by b.id),0) as days_of_rent
39  from    book b
40          join location l on (b.locid = l.id)
41          left outer join book_history bh on (   bh.bookid = b.id
42                                             and bh.rentdate >= to_date('01-01-2012','DD-MM-YYYY')
43                                             and bh.rentdate < to_date('01-01-2013','DD-MM-YYYY')
44                                             )
45* order by 1
SQL> /
        ID REFERENCE     TITLE     DESCRIPTION     AUTHOR     ROOMN DIFFERENT_READERS DAYS_OF_RENT
         1 reference0001 titel0001 description0001 author0001 100.1                 2           15
         2 reference0002 titel0002 description0002 author0002 100.1                 2           15
         3 reference0003 titel0003 description0003 author0003 100.1                 1            8
         4 reference0004 titel0004 description0004 author0004 100.2                 0            0
         5 reference0005 titel0005 description0005 author0005 100.2                 0            0
         6 reference0006 titel0006 description0006 author0006 100.2                 0            0
         7 reference0007 titel0007 description0007 author0007 100.3                 0            0
         8 reference0008 titel0008 description0008 author0008 100.3                 0            0
8 rows selected.
SQL>also ensure you treat dates as dates and specify a format mask when using TO_DATE.

Similar Messages

  • How to write XSJS Select Query with input parameters

    Hello Experts,
    I am creating a xsjs file and in that file I am trying to write a Select Query based on a Calculation View
    I have tried it the following way:
    var query = 'SELECT TOP 100 \"Name\", \"Address\", \"City\", \"Country\" FROM \"_SYS_BIC\".\"Test.HL/AddressView\"'
        + 'WITH PARAMETERS(\'PLACEHOLDER\' = (\'$$P_Name$$\', \' Akhil \'),'
      + '\'PLACEHOLDER\' = (\'$$P_City$$\', \' Lucknow \'))';
    But it gives me the "Mixed spaces and tabs error".
    How should I write XSJS Select Query with input parameters?
    Regards,
    Rohit

    >But it gives me the "Mixed spaces and tabs error".
    Mixed spaces and tabs has nothing to do with the syntax of the statement. You used both spaces and the tab in the content - which JSLint doesn't like.  Remove the beginning spaces of each line and use only one or the other.
    The actual syntax of your statement doesn't look right.  The problem is that you are escaping the \ when you don't need to if you are using ' instead of " for your string.  You escape with \" in the first line but then escape with \' in the 2nd and 3rd line.  That is going to cause serious parsing problems with the command.

  • How to re-write this big SELECT Query with INNER JOINs?

    Hi Experts
    I have a performance killer SELECT query with an inner join of 3 tables u2013 VBAP, VBAK and VBEP together, which populates records to an internal table INT_COLL_ORD. Based on these records selected, in another SELECT query, records are fetched from VBUK table to the internal table INT_VBUK.
    SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV D~AUART E~ETTYP E~EDATU
    INTO TABLE INT_TAB_RES
    FROM VBAP AS A INNER JOIN VBAK AS D
    ON D~VBELN EQ A~VBELN AND D~MANDT EQ A~MANDT
    INNER JOIN VBEP AS E
    ON E~VBELN EQ A~VBELN AND E~POSNR EQ A~POSNR AND E~MANDT EQ A~MANDT
    WHERE  A~VBELN IN s_VBELN AND
           D~auart in s_auart AND
           D~vkorg in s_vkorg AND
           D~vbtyp eq 'C'     AND
           ( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
           OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
           A~ABGRU EQ SPACE AND
           A~MTVFP IN R_MTVFP AND
           A~PRCTR IN R_PRCT AND
           E~ETENR EQ '1'.
    SORT INT_COLL_ORD BY VBELN POSNR ETTYP.
    DELETE ADJACENT DUPLICATES FROM INT_TAB_RES COMPARING VBELN POSNR.
    CHECK NOT INT_TAB_RES [] IS INITIAL.
    SELECT VBELN UVALL CMGST INTO TABLE INT_VBUK
    FROM VBUK FOR ALL ENTRIES IN INT_TAB_RES
    WHERE VBELN = INT_TAB_RES-VBELN AND UVALL NE 'A'.
    Now, the requirement is:
    I want to split this query. Like, first join VBAK and VBUK first. With this selection, go to the inner join of VBAP and VBEP (on key VBELN) to get the results. How can I re-write this Query?
    Please help.
    Thx n Rgds

    Hi Nagraj
    As of your suggestion, I have re-written the query as below:
    * Declarations
    TYPES: BEGIN OF TYP_COLL_ORD,
            VBELN  LIKE VBAK-VBELN,
            POSNR  LIKE VBUP-POSNR,
            MATNR  LIKE VBAP-MATNR,
            KWMENG LIKE VBAP-KWMENG,
            KBMENG LIKE VBAP-KBMENG,
            ERDAT  LIKE VBAK-ERDAT,
            ERZET  LIKE VBAK-ERZET,
            PSTYV  LIKE VBAP-PSTYV,
            AUART  LIKE VBAK-AUART, u201Calready exists in type
            ETTYP  LIKE VBEP-ETTYP,
            EDATU  LIKE VBEP-EDATU.
    TYPES: END OF TYP_COLL_ORD.
    DATA: INT_COLL_ORD TYPE TABLE OF TYP_COLL_ORD WITH HEADER LINE.
    TYPES: BEGIN OF TYP_VBUK,
            AUART  LIKE VBAK-AUART, u201Chave added this field
            VBELN  LIKE VBUK-VBELN,
            UVALL  LIKE VBUK-UVALL,
            CMGST  LIKE VBUK-CMGST.
    TYPES: END OF TYP_VBUK.
    DATA: INT_VBUK TYPE TABLE OF TYP_VBUK WITH HEADER LINE.
    *QUERY#1 u2013 for VBAK & VBUK Join
    SELECT A~AUART B~VBELN B~UVALL B~CMGST
    INTO TABLE INT_VBUK
    FROM VBAK AS A INNER JOIN VBUK AS B
    ON A~VBELN EQ B~VBELN
    WHERE A~VBELN IN s_VBELN AND
    A~auart in s_auart AND
    A~vkorg in s_vkorg AND
    A~vbtyp eq 'C' AND
    B~UVALL NE 'A'.
    IF NOT INT_VBUK[] IS INITIAL.
    SORT INT_VBUK BY VBELN.
    DELETE ADJACENT DUPLICATES FROM INT_VBUK COMPARING VBELN.
    *QUERY#2 u2013 for VBAP & VBEP Join
    SELECT A~VBELN A~POSNR A~MATNR A~KWMENG A~KBMENG A~ERDAT A~ERZET A~PSTYV B~ETTYP B~EDATU
    INTO TABLE INT_COLL_ORD
    FROM VBAP AS A INNER JOIN VBEP AS B
    ON B~VBELN EQ A~VBELN AND B~POSNR EQ A~POSNR AND B~MANDT EQ A~MANDT
    FOR ALL ENTRIES IN INT_VBUK
    WHERE A~VBELN = INT_VBUK-VBELN AND
    ( ( matnr LIKE c_prefix_sp AND zz_msposnr NE 0 AND kbmeng EQ 0 )
    OR ( matnr LIKE c_prefix_fp AND kwmeng NE A~kbmeng ) ) AND
    A~ABGRU EQ SPACE AND
    A~MTVFP IN R_MTVFP AND
    A~PRCTR IN R_PRCT AND
    B~ETENR EQ '1'.
    ENDIF.
      SORT INT_COLL_ORD BY  VBELN POSNR ETTYP.
      DELETE ADJACENT DUPLICATES FROM INT_COLL_ORD
        COMPARING VBELN POSNR.
      CHECK NOT INT_COLL_ORD[] IS INITIAL.
      LOOP AT INT_COLL_ORD.
        CLEAR: L_MTART,L_ATPPR,L_ETTYP.
        IF L_PREVIOUS_ETTYP NE INT_COLL_ORD-ETTYP OR
          L_PREVIOUS_AUART NE INT_COLL_ORD-AUART.
          READ TABLE INT_OVRCTL WITH KEY AUART = INT_COLL_ORD-AUART ETTYP = INT_COLL_ORD-ETTYP.
          CHECK SY-SUBRC NE 0.
    Now, the issue is:
    Please note that declaration for INT_COLL_ORD has a field AUART, which is used in further parts of program (see the statement just above)
    But, since neither VBAP nor VBEP contains AUART field, it cannot be fetched through the QUERY#2. So this value is not populated into INT_COLL_ORD through SELECT Query.
    Since this field is used in later part of program & that the internal table has no value for this field, it dumps!!
    How to include this value into the INT_COLL_ORD?
    Plz suggest....

  • How to improve this query speed ?....help me

    How to improve the query speed. Any hints can u suggest in the query or any correction. Here i am using sample tables for checking purpose, When i am trying with my original values, this type of query taking longer time to run
    select ename,sal,comm from emp where(comm is null and &status='ok') or (comm is not null and &status='failed');
    Thanx in advance
    prasanth a.s.

    What about
    select ename,sal,comm from emp where comm is null and &status='ok'
    union all
    select ename,sal,comm from emp where comm is not null and &status='failed';
    Regards
    Vaishnavi

  • How to improve the query performance in to report level and designer level

    How to improve the query performance in to report level and designer level......?
    Plz let me know the detail view......

    first its all based on the design of the database, universe and the report.
    at the universe Level, you have to check your Contexts very well to get the optimal performance of the universe and also your joins, keep your joins with key fields, will give you the best performance.
    at the report level, try to make the reports dynamic as much as you can, (Parameters) and so on.
    and when you create a paremeter try to get it match with the key fields in the database.
    good luck
    Amr

  • How to write sql query with many parameter in ireport

    hai,
    i'm a new user in ireport.how to write sql query with many parameters in ireport's report query?i already know to create a parameter like(select * from payment where entity=$P{entity}.
    but i don't know to create query if more than 1 parameter.i also have parameter such as
    $P{entity},$P{id},$P{ic}.please help me for this.
    thanks

    You are in the wrong place. The ireport support forum may be found here
    http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&func=showcat&catid=9

  • How to create sap query with "or" relationship

    dear experts,
    I need a report to display the employee whoese WSR is
    changed in the month for infotype 0007.
    that is ,we want to search with selection
    begda OR endda between 2008-01-01 and 2008-01-31.
    how to create sap query with "or" relationship?

    hi use like this,
    CALL FUNCTION 'HR_READ_INFOTYPE'
      EXPORTING
        pernr                 =  p_pernr
        infty                   =  '0007'
       BEGDA                =  p_date1
       ENDDA                 = p_date2
      TABLES
        infty_tab             = itab .
    hi use this by passing the pernr to fm and giving the dates low and high in the p_date1 and p_date2.
    loop at itab where condition.
    endloop.
    may it helps u,
    regards,
    venkat.

  • Issue in select query with where clause

    Hi guys,
    I'm facing an issue while using select query with the where clause. When I'm selecting all the data from the table it returns the correct result. But when I'm using the where clause to get the specific rows from the table it returns no rows. But the data I'm trying to fetch using the where condition exists in the table.
    Here is my query which causing the issue,
    select * from mytable where myfield = 'myvalue'
    But if I use the following query it returns the result correctly.
    select * from mytable
    Also the myfield value 'myvalue' exists in the table.
    I have tried by running this query in both SQL Developer and SQL Plus. I have tried this query in mssql as well. It works perfectly and returns correct result sets for both the queries I have mentioned above. I'm unable to predict the issue as I'm new to ORACLE. Please help.
    Thanks,
    Ram.

    Hi Ram,
    I experienced an issue similar to this with a varchar2 field. Some of our records had a hidden newline character at the end of them, which was making queries like the one below fail:
    select * from employees
    where email = '[email protected]'The best way I found to detect this was to use
    select 'XX'||email||'XX' from employeesTo make sure that there were no newlines. But that is just a guess. If you could provide some example table data and the outputs of your selects, it would be helpful.
    Jeff

  • Absolute dynamic select query with dynamic join and where

    Has anyone ever tried creating an absolutely dynamic SELECT query with dynamic Join and Where conditions.
    I have a requirement of creating such a query in an Utility Class, and i have written the code. But its throwing my sysntax errors.
    Please let me know where am I going wrong OR is it really possible to create such a dynamic Query??
        SELECT (FIELDS) INTO TABLE IT_TABLES
          FROM ( (ME->TABLE1)  inner join ( me->table2 )
          on ( on_condition ) )
          WHERE (me->where_fields).
    Ags.

    It worked for me in a following way:
    select * into corresponding fields of table <result_table>
            from (join_string)
            where (l_where).
    Where the contents of join_string were dynamically build using concatenation. So it will be something like
    concatenate ME->TABLE1 'as a INNER JOIN' me->table2 'as b ON (' into join_string separated by space.
    <...>
    add here matching/reference colums, something like
    concatenate 'a~' me->TABLE1_JOIN_COL into temp1.
    concatenate 'b~' me->TABLE2_JOIN_COL into temp2.
    concatenate join_string temp1 '=' temp2 into join_string separated by space.
    <...>
    concatenate join_string ')' into join_string separated by space.
    And then use similar approach for l_where variable.

  • Dynamic select query with dynamic where condition

    Hi all,
    I want to use the dynamic select query with dynamic where condition. For that I used the below code but I am getting dump when using this code.
    Please advice, if there is any other way to achieve this requirement.
    Thanks,
    Sanket Sethi
    Code***************
    PARAMETERS: p_tabnam      TYPE tabname,
                p_selfl1      TYPE edpline,
                p_value       TYPE edpline,
                p_where1      TYPE edpline .
    DATA: lt_where    TYPE TABLE OF edpline,
          lt_sel_list TYPE TABLE OF edpline,
          l_wa_name   TYPE string,
          ls_where    TYPE edpline,
          l_having    TYPE string,
          dref        TYPE REF TO data,
          itab_type   TYPE REF TO cl_abap_tabledescr,
          struct_type TYPE REF TO cl_abap_structdescr,
          elem_type   TYPE REF TO cl_abap_elemdescr,
          comp_tab    TYPE cl_abap_structdescr=>component_table,
          comp_fld    TYPE cl_abap_structdescr=>component.
    TYPES: f_count TYPE i.
    FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
    *                <ls_outtab> TYPE ANY,
                    <l_fld> TYPE ANY.
    struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
    elem_type   ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
    comp_tab = struct_type->get_components( ).
    comp_fld-name = 'F_COUNT'.
    comp_fld-type = elem_type.
    APPEND comp_fld TO comp_tab.
    struct_type = cl_abap_structdescr=>create( comp_tab ).
    itab_type   = cl_abap_tabledescr=>create( struct_type ).
    l_wa_name = 'l_WA'.
    CREATE DATA dref TYPE HANDLE itab_type.
    ASSIGN dref->* TO <lt_outtab>.
    *CREATE DATA dref TYPE HANDLE struct_type.
    *ASSIGN dref->* TO <ls_outtab>.
    * Creation of the selection fields
    APPEND p_selfl1 TO lt_sel_list.
    APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
    ** Creation of the "where" clause
    *CONCATENATE p_selfl1 '= '' p_value ''.'
    *            INTO ls_where
    *            SEPARATED BY space.
    *APPEND ls_where TO lt_where.
    * Creation of the "where" clause
    APPEND p_where1 TO lt_where.
    * Creation of the "having" clause
    l_having = 'count(*) >= 1'.
    * THE dynamic select
    SELECT          (lt_sel_list)
           FROM     (p_tabnam)
           INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>.
    *       WHERE    (lt_where).

    Hi Sanket,
    The above given logic of mine works for you, put the code in the If condition and try-
    just like below:
    IF NOT P_EBELN IS INITIAL.
    lt_where = '& = ''&'' '.
    REPLACE '&' WITH p_ebeln INTO lt_where.
    REPLACE '&' WITH field_value INTO lt_where.
    SELECT (lt_sel_list) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
    FROM (p_tabnam)
    WHERE (lt_where).
    ENDIF.
    thanks\
    Mahesh

  • BDC select query with addition based on all If conditions

    Hi can any one send me the select query with conditions like
    If Itab is not initial.
    Endif. and if possible with valiadations messages also.
    IF CHECK_NUMBER of CHECK_ADVICE of the flat file = PAYR-CHECT. Then update SAP field BSEG-XREF2 .
    9.     When Flat file check Number = PAYR-CHECT then Insert or Update Flat file RECEIPT_DATE into SAP field BSEG-XREF1.
    Please send me immediately.

    >     SELECT rsnum
    >            rspos
    >            matnr
    >            werks
    >         lgort
    >            shkzg
    >            aufnr
    >            bdmng
    >            enmng                      
    >            FROM  resb INTO  TABLE it_rsnum FOR ALL ENTRIES IN it_aufnr
    >            WHERE rsnum EQ it_aufnr-rsnum
    >            AND   xloek NE 'X'
    >            AND   postp NE 'X'
    >            AND   resbbdmng GE resbenmng.
    >   ENDIF.
    >
    > Database Table RESB: 40,000,000 Records (40 Million)
    > Internal Table   it_aufnr:  20,000 Entries
    > Entries selected from RESB: 150,000.
    >
    Hi,
    the problem is the big for all entries table.
    Your 20.000 records FAE will be split into SEVERAL sql statements depending on the size of the table.
    Where do you get the it_auftrn from?
    If it's another transparent table try to JOIN this table to your big table.
    SELECT rsnum
                rspos
                matnr
                werks
            lgort
                shkzg
                aufnr
                bdmng
                enmng                      
                FROM  resb JOIN tab_auftrn  on  resbrsnum = tab_auftrnauftrn
                AND   xloek NE 'X'
                AND   postp NE 'X'
                AND   resbbdmng GE resbenmng.
    Make sure that your WHERE filter and the JOIN keys on both tables are supported by indexes.
    150.000 records about of 40 Mio. can definitly be serviced by an appropriate index:
    i.e. Index fields (check your PK or secondary indexes):
    rsnum
    enmng                      
    aufnr
    bye
    yk

  • I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    Hi Arun ,
    Can you explain little bit more ??
    what is account doc no? 
    what are the transactions should be displayed in your output??
    -Rajesh N

  • How to use SELECTION-SET with FREE-SELECTIONS?

    Hi,
    how to use SELECTION-SET with FREE-SELECTIONS?
    Regards,
    Vinay.

    Check http://help.sap.com/abapdocu_70/en/ABAPSUBMIT_SELSCREEN_PARAMETERS.htm

  • How to improve image resolution with using filters

    how to improve image resolution with using filters?

    image resolution, Please check the below links. They should give you a clear idea about Resizing and resampling images.
    http://helpx.adobe.com/photoshop/kb/advanced-cropping-resizing-resampling-photoshop.html#m ain_Resizing_and_resampling_images
    Understanding Resize vs. Resample | Learn Photoshop CS6 | Adobe TV
    Cheers!
    ~ Arpit

  • How to map a query with the Multiprovider?

    Hello All,
    Can any one please tell me how to map a query with a multiprovider?
    Appreciate your help.
    Regards,
    Soumya.

    Hi,
    Can you please elaborate , do you mean how to create a query with multiprovider. if that is your question it is very simple just create the query on the multiprovider.
    What is the purpose for mapping the query with multiprovider

Maybe you are looking for

  • Airport Express Can't be Found

    I use two airport expresses to stream music to two separate rooms. Last week they both disappeared (They were no longer showing up as options in iTunes). The green lights were still on, but could not be found using the airport utility. I hard reset b

  • Resize JTable caused ArrayIndexOutOfBoundsException

    I have called setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN) for my JTable. If I resize the right bound of the last column, I received the following exception. Does anyone know if it is my problem? Thanks! Exception occurred during event dispatchi

  • Language Translation in Smartforms - SE63

    Hi All. I have succesfully translated a smartform from English to Portugues, but as soon as i have done text changes to my smartform and activate it, and done the translation for the changes, the previous changes dissappers and revert to the first/or

  • PLS HELP!! DELL DIMENSION L866r

    I am having some problem with Solaris 8 installation over dell computer. I have Intel 810 chipset on the motherboard. I need the video driver for onboard video. First I install the O.S. it didnt find the right video driver. I Select VGA or text mode

  • Playing subtitles in a Quick Time Movie.

    Hi, I have recently installed perian on to my MacBook Pro as I want to play .srt files (subtitles) while the movie was playing. Problem is that I cannot seem to get ot to work. can anyone please suggest a reason why it won't work or how to make it wo