Where statement with a select statement

Hi everybody
Is possible to create a sql statement in query generator like:
SELECT T0.[Name] FROM [dbo].[@FAMILIA_ARTIGOS_SUB]  T0 WHERE T0.[U_Familia] =SELECT $[OITM.U_Familia]
Regards
Vasco

Hi!
You are trying to create a form-context query that will be used in Items card, so it's enough to use
SELECT T0.[Name] FROM [dbo].[@FAMILIA_ARTIGOS_SUB] T0 WHERE T0.[U_Familia] = $[OITM.U_Familia]
No SELECT is needed in the end.
If the problem is deeper, please define more exactly what is the aim of the task.
hth,
Alexey

Similar Messages

  • Update with a select statement

    hi experts,
    I need some help again.. :)
    I have an insert query here with a select statement.. Juz wondering how to do it in update?
    insert into newsmail_recipient
            (IP_RECIPIENTID,NF_LISTID,NF_STATUSID,D_CREATED,D_MODIFIED,C_NAME,C_EMAIL,USER_ID,NEWSMAIL_ID)
            select newsmail_recipientid_seq.nextval
              , liste
              , 1
              , sysdate
              , sysdate
              , null
              , null
              , ru.nf_userid
              , null
            from roleuser ru, unit u, userinfo ui
            where u.ip_unitid = ru.nf_unitid
            and u.ip_unitid = unit_id
            and ui.ip_userid = ru.nf_userid
            and ui.nf_statusid = 1
            and n_internal = 0
            and ui.ip_userid not in (select user_id
                                       from newsmail_recipient
                                      where user_id = ui.ip_userid
                                        and nf_listid = liste
                                        and nf_statusid = 1);let me know your thoughts.
    Regards,
    jp

    Hi,
    924864 wrote:
    ... I have an insert query here with a select statement.. Juz wondering how to do it in update?How to do what, exactly?
    MERGE can UPDATE existing rows, and INSERT new rows at the same time. In a MERGE statement, you give a table or a sub-query in the USING clause, and define how rows from that result set match rows in your destination table. If rows match, then you can UPDATE the matching row with values from the sub-query.
    Very often, MERGE is easier to use, and perhaps more efficient, than UPDATE just for changing existing rows. That is, while MERGE can do both INSERT and UPDATE, it doesn't have to . You can tell MERGE just to do one or the other if you wish.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Since you're asking about a DML statement, such as UPDATE, the sample data will be the contents of the table(s) before the DML, and the results will be state of the changed table(s) when everything is finished.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Values from a Multi-Select in the where clause of a Select statement

    I have a web page that solicits query parameters from the user.
    The selections that the user makes will populate the WHERE clause of a Select statement.
    One of the controls on the page is a multi-select control.
    When this page posts, I would like to execute a Select statement wherein the selected values from this control appear in the .. Column IN ( <list here> ) portion of the WHERE clause.
    This is an extremely common scenario, but I cannot seem to locate a how-to or message thread that addresses this specific case.
    I have an idea that it may involve dynamic SQL or Execute Immediate, but cannot seem to pin down the answer.
    Any help would be greatly appreciated!

    anonymous - As illustrated here: Re: Search on a typed in list of values
    Scott

  • Smart scan not working with Insert Select statements

    We have observed that smart scan is not working with insert select statements but works when select statements are execute alone.
    Can you please help us to explain this behavior?

    There is a specific exadata forum - you would do better to post the question there: Exadata
    I can't give you a definitive answer, but it's possible that this is simply a known limitation similar to the way that "Create table as select" won't run the select statement the same way as the basic select if it involves a distributed query.
    Regards
    Jonathan Lewis

  • Case sensitive statement in the select-statement

    Hi All,
    i have a table in the abap-dictionary filled with names...when i try to select them with the select-statement with condition:
    table-name_column like 'some_name'
    I have encountered some problems...the inquiry is case-sensitive. What i want to do is to read the value from the abap-dictionary table uppercase or lowercase and compare it with the needed value also translated in uppercase or lowercase.
    The only idea i have is to select all values of the dictionary table into an internal table and to translate the values there in uppercase or lowercase and then to loop trough it. But this approach would cost a lot of performance.
    Do someone has an other proposal?

    check...
    Select statement
    It would be difficult , because it is based on how data is stored in the data base , now consider the scenario of the system i am using , we can material description as 'test' or 'TEST' , 'Test' .
    If in your system there are only stored in either caps or small the you can perform the select twice .
    But i myself dont find it to be such a good solution , but you can give it a try
    There is one more solution specific to material description ,and that is in the table MAKT there is a field MAKTG , which stored the description in uppercase , so for this you can first convert the description to uppercase and then perform select on this field

  • Using if logic in the where clause of a select statement

    I have a select clause. And in the select clause there is a variable all_off_trt that can be 'Y' or 'N'.
    In the where clause I want to make it so that if a form variable is checked and all_off_trt is 'Y' then
    exclude it else if the form variable isn't checked then select it no matter what all_off_trt is.
    Is there any way to include either and if statement or a case statement within the where clause to acheive this? If not is there another way of doing it?
    Basically I am looking for a case statement like this
    case
    when all_off_trt = 'Y' and mail_para.code = 'Y' then false
    else true
    end
    Message was edited by:
    Tugnutt7

    Ok, so that really doesn't solve my problem. I have 3 different fields that I need to do that with. Each combining in a select statement to print an email list, as well as other thing limiting the where clause.
    This is currently what I have, tested and working 100%.
    cursor email_cur is
         select unique p.email,s.all_off_trt,s.all_deceased,s.no_enroll
    from participant p, trialcom t, ethics s
    where p.status='A'
    and p.surname=t.surname
    and p.initials=t.initials
    and s.trial_cd = t.tricom
    and s.centre = t.centre
    and p.email is not null
    and (t.centre in (select code from mail_parameters where user_name=user and mail_para='CENTRE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='CENTRE'))
    and (t.tricom in (select code from mail_parameters where user_name=user and mail_para='TRIAL')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='TRIAL'))
    and (t.role in (select code from mail_parameters where user_name=user and mail_para='ROLE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='ROLE'))
    and (p.country in (select code from mail_parameters where user_name=user and mail_para='COUNTRY')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='COUNTRY'))
    and (t.represent in (select code from mail_parameters where user_name=user and mail_para='REPRESENT')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='REPRESENT'));
    This is in a program unit that runs when a button is clicked. At the end of that I need to add on the 3 case statements that help further narrow down the selection of emails to be printed. Then it prints the emails selected from this statement into a file. So it has to be done right in the select statement. The three table variables are the all_off_trt, all_deceased, and no_enroll. The form has 3 checkboxes. One for each, that when checked (giving the variable associated with the checkboxes a value of 'Y') excludes all emails that have a 'Y' in the coresponding table variable.

  • Short dump with a select statement

    I have the following select statement (I have also inluded my data statement(s)) and I get a short dump.  The error is as follows:
    <b>What happened?                                                                 
        Error in ABAP application program.                                                                               
    The current ABAP program "ZPARTNER" had to be terminated because one of the
        statements could not be executed.                                                                               
    This is probably due to an error in the ABAP program.                                                                               
    Following a SELECT statement, the data read could not be placed in AN      
        the output area.                                                           
        A conversion may have been intended that is not supported by the           
        system, or the output area may be too small.                                                                               
    Error analysis                                                                 
        An exception occurred. This exception will be dealt with in more detail    
        below. The exception, assigned to the class 'CX_SY_OPEN_SQL_DB', was not   
         caught, which                                                             
         led to a runtime error. The reason for this exception is:                 
        The data read during a SELECT access could not be inserted into the        
        target field.                                                              
        Either conversion is not supported for the target field's type or the      
        target field is too short to accept the value or the data are not in a     
        form that the target field can accept          </b>                            
    it_adrc TYPE TABLE OF adrc,
          wa_adrc LIKE LINE OF it_adrc,
    SELECT addrnumber name1 street city1 region post_code1 tel_number
      FROM adrc INTO TABLE it_adrc
      FOR ALL ENTRIES IN it_detail
      WHERE addrnumber = it_detail-adrnr.
    Regards,
    Davis

    If you only need the fields mentioned in the select query, then follow this:
    TYPES: BEGIN OF ADRC_TYPE,
         ADDRNUMBER LIKE ADRC-ADDRNUMBER,
         NAME1        LIKE ADRC-NAME1,
         STREET        LIKE ADRC-STREET,
         CITY        LIKE ADRC-CITY,
         REGION        LIKE ADRC-REGION,
         POST_CODE1 LIKE ADRC-POST_CODE1,
         TEL_NUMBER LIKE ADRC-TEL_NUMBER,
           END OF ADRC_TYPE,
           ADRC_T_TYPE TYPE TABLE OF ADRC_TYPE.
    DATA: IT_ADRC TYPE ADRC_T_TYPE WITH HEADER LINE.
    IF NOT IT_DETAIL[] IS INITIAL.
    SELECT addrnumber name1 street city1 region post_code1 tel_number
      FROM adrc INTO TABLE it_adrc
      FOR ALL ENTRIES IN it_detail
      WHERE addrnumber = it_detail-adrnr.
    ENDIF.
    Thanks,
    SKJ

  • Field symbols as Table name and in where condition in a select statement

    Hello All,
    I have a scenario where I need to get user input on table name and old field value and new field value. Then based on user input, I need to select the record from the database. The column name for all the tables in question is different in the database, however there data type is the same and have same values.
    I am not able to use a field symbol for comparing the old field value to fetch the relevant record in my where clause.
    I cannnot loop through the entire table as it has 10 millilon records, please advice on how to add the where clause as field symbol as the table name is also dynamically assigned.
    Here is my code:
    DATA: TAB       LIKE SY-TNAME,
          TAB_COMP1 LIKE X031L-FIELDNAME,
          TAB_COMP2 LIKE X031L-FIELDNAME,
          NO_OF_FLD TYPE N.
    DATA: BEGIN OF BUFFER,
            ALIGNMENT TYPE F,
            C(8000)   TYPE C,
          END OF BUFFER.
    FIELD-SYMBOLS: <WA>   TYPE ANY,
                  <COMP1> TYPE ANY,
                  <COMP2> TYPE ANY.
    GET TABLE NAME GIVEN BY USER IN LOCAL VARIABLE
      TAB = TAB_NAME.
    CREATE FIELD NAME BASED ON THE TABLE NAME ENTERED.
      CASE TAB_NAME.
      WHEN 'OIUH_RV_GL'.
          KEY FIELD
            TAB_COMP1  = 'GL_GL_SYS_NO'.
            NO_OF_FLD  = 1.
      WHEN 'OIUH_RV_OPSL'.
          KEY FIELD
            TAB_COMP1  = 'OPSL_GL_SYS_NO'.
            NO_OF_FLD  = 1.
      WHEN 'OIUH_RV_OTAX'.
          NOT THE ONLY KEY FIELD
            TAB_COMP1  = 'OTAX_GL_SYS_NO'.
            TAB_COMP2  = 'OTAX_TAX_POS_NO'.
            NO_OF_FLD  = 2.
      WHEN 'OIUH_RV_GTAX'.
          NOT THE ONLY KEY FIELD
            TAB_COMP1  = 'GTAX_GL_SYS_NO'.
            TAB_COMP2  = 'GTAX_TAX_POS_NO'.
            NO_OF_FLD  = 2.
      WHEN OTHERS.
            EXIT.
      ENDCASE.
    SET FIELD SYMBOL WITH APPROPRIATE TYPE TO BUFFER AREA.
    ASSIGN BUFFER TO <WA> CASTING TYPE (TAB).
    How to add where clause and remove the if condition in the select -- endselect
    SELECT * FROM (TAB) INTO <WA>. 
      ASSIGN COMPONENT TAB_COMP1 OF STRUCTURE <WA> TO <COMP1>.
      IF NO_OF_FLD = 2.
        ASSIGN COMPONENT TAB_COMP2 OF STRUCTURE <WA> TO <COMP2>.
      ENDIF.
      IF <COMP1> = OLD_SYS_NO.
        code for updating table would come here
          WRITE: 'MATCH FOUND'.
          EXIT.
      ENDIF.
    ENDSELECT.
    Please advice. Thanks much.
    Edited by: Shipra Jhunjhunwala on Jul 22, 2009 1:33 PM
    Edited by: Shipra Jhunjhunwala on Jul 22, 2009 1:34 PM
    Edited by: Shipra Jhunjhunwala on Jul 22, 2009 1:35 PM

    1. Create single column table for holding field name depending on the table entered.
    2. Take input from user: for e.g. table_name
    3. Using case load single column table with required fields
       for e.g.
      CASE TAB_NAME.
       WHEN 'OIUH_RV_GL'.
             Append 'GL_GL_SYS_NO' to KEY_FIELD --> KEY_FIELD is the single line internal table as mentioned in step 1.
       WHEN 'OIUH_RV_OPSL'.
             Append 'OPSL_GL_SYS_NO'.
       WHEN 'OIUH_RV_OTAX'.
             Append 'OTAX_GL_SYS_NO' to KEY_FIELD.
               APPEND 'OTAX_TAX_POS_NO' to KEY_FIELD.
       WHEN 'OIUH_RV_GTAX'.
             Append 'GTAX_GL_SYS_NO' to KEY_FIELD.
               APPEND 'OTAX_TAX_POS_NO' to KEY_FIELD.
       WHEN OTHERS.
          EXIT.
       ENDCASE.
       Now depending on the table name you have required column ready
    4. Create dynamic internal table using following sudo code
       Fill the fieldcatlog using the single column field table and DD03L table, See what all columns from DD03L you want to fill in field catlog table
       loop at internal table with all the fields.
        move it to field catalog.
        append field catalog.
       endloop.
    5. Pass this field catalog table to static method create_dynamic_table method
       DATA table TYPE REF TO DATA. --> data object for holding handle to dynamic internal table.
       call method cl_alv_table_create=>create_dynamic_table
       exporting
          it_fieldcatalog = fieldcatalog_tab
       importing
          ep_table = table.
    6. Now assign table reference to field symbol of type table.
       ASSIGN table->* to <field-tab>.
    7. Also create work area <field-wa> using refrence of table.
       create data object wa LIKE LINE OF <field-tab>.
       ASSIGN wa->* to <field-wa>.
    8. Also define field symbol for field name.
       for e.g. <field_name>
    4. Dynamic internal table is ready
    5. Now execute the select statement as follows:
       SELECT (KEY_FIELD)
         INTO <ITAB> --> created dynamically above
          FROM (TABLE_NAME)
         WHERE (WHERE).  --> WHERE is single line internal table having line type of CHAR72. So for every old value there will be one line
         Where condition is same as like we give in static way only difference in this case it will stored in internal table line wise.
        In this case you need to append all your where condition line by line in to WHERE.     
    5. To fill this dynamic internal table using ASSIGN COMPONENT <Comp_number> OF STRUCTURE <field-wa> TO <field-name>
       So in this case if first field of structure STRUCT1 is user_id then sudo-code will be
       loop at internal table containing list of fields into field_wa --> single column field table
           ASSIGN COMPONENT field_wa OF STRUCTURE <field-wa> TO <field>. "Here field_wa is wa area for single column internal table holding all the fieldnames.
           Now <field-name> points to user_id field. Move some value into it as nornally we do with variables.
           Move <your_new_value> to <field-name>. --> Assign new value
            or
            <field-name> = <your_new_value>.
       Endloop.
    6. After completing all the fields one row will be ready in <field_wa>.
       APPEND <field_wa> to <field_tab>.
    Hope this helps you.
    Thanks,
    Augustin.

  • A Select statement with Appending table statement in it.

    Hi,
      How can I use a select statement with a <Appening table> statement in it.
    SELECT DISTINCT <field Name>
                    FROM <DB table name>
                    APPENDING TABLE <itab>
                    WHERE <fieldname> EQ <Itab1-fieldname>
                      AND <fieldname> EQ <itab2-fieldname>.
    Can I use the above select statement.If I'm using this...how this works?
    Regards
    Dharmaraju

    Hi, Dharma Raju Kondeti.
    I found this in the SAP online help, hope this can help you.
    Specifying Internal Tables
    When you read several lines of a database table, you can place them in an internal table. To do this, use the following in the INTO clause:
    SELECT ... INTO|APPENDING [CORRESPONDING FIELDS OF] TABLE <itab>
                              [PACKAGE SIZE <n>] ...
    The same applies to the line type of <itab>, the way in which the data for a line of the database table are assigned to a table line, and the CORRESPONDING FIELDS addition as for flat work areas (see above).
    The internal table is filled with all of the lines of the selection. When you use INTO, all existing lines in the table are deleted. When you use APPENDING; the new lines are added to the existing internal table <itab>. With APPENDING, the system adds the lines to the internal table appropriately for the table type. Fields in the internal table not affected by the selection are filled with initial values.
    If you use the PACKAGE SIZE addition, the lines of the selection are not written into the internal table at once, but in packets. You can define packets of <n> lines that are written one after the other into the internal table. If you use INTO, each packet replaces the preceding one. If you use APPENDING, the packets are inserted one after the other. This is only possible in a loop that ends with ENDSELECT. Outside the SELECT loop, the contents of the internal table are undetermined. You must process the selected lines within the loop.
    Regards,
    feng.
    Edited by: feng zhang on Feb 21, 2008 10:20 AM

  • Where condition problem in select statement.

    Hi,
    I am trying to write select statement as below
      SELECT objectclas objectid FROM cdhdr INTO CORRESPONDING FIELDS OF TABLE ltab_chgdocu
                       WHERE objectclas IN ('STUE' , 'STUE_V')
                       AND   ( TCODE eq 'CS01u2019 or tcode eq u2019CS02u2019 or   tcode eq 'C201' or tcode eq 'C202' ) .
    But I am getting the error like "literals that takes up more than one line or not permitted"
    Please let me know whats wrong and correct me .
    Thanks,
    Vinay.

    Hi Vinay,
    Declare constants
    constants : c_stue type CDOBJECTCL value 'STUE',
                 c_stue_v type CDOBJECTCL value 'STUE_V',
                 c_cs01 type sy-tcode value 'CS01',
                 c_cs02 type sy-tcode value 'CS02',
                 c_C201 type sy-tcode value 'C201',
                 c_C202 type sy-tcode value 'C202'.
    And replace your hardcoded values with these constants in your select query.
    SELECT objectclas objectid FROM cdhdr INTO CORRESPONDING FIELDS OF TABLE it_tab
                                        WHERE objectclas IN (c_STUE , c_STUE_V)
                                                     AND ( TCODE eq c_CS01
                                                     or tcode eq c_CS02
                                                     or  tcode eq c_C201
                                                      or tcode eq c_C202 ).
    Thanks & Regards
    - Always Leaner

  • Problem with my select statement

    Dear buddies,
    Really seeking someone to guide me in this sql issue.
    Here is the scenario.
    - should check for the number of prospects(to be customers) downloaded from a file for every dealer based on the
    region
    description about columns shown
    1st column, region
    2nd column, dealer code
    3rd column, number of prospect received
    4th column, the percentage of conversion
    5th column, i must order by the 4th column and select the top 20 percentage and take its average.
    sample data
    region dealer_code no. of no of % of % of Comparing with
    prospect prospect prospect Top 20 top 20 conversion
    received converted conversion Conversion
    A     dealer 1     150     50     33%     56%     Red
         dealer 2     100     40     40%     56%     Red
         dealer 3     120     60     50%     56%     Red
              130     70     54%     56%     Red
              200     20     10%     56%     Red
    B          140     60     43%     56%     Red
              110     60     55%     56%     Red
              100     60     60%     56%     Green
              130     69     53%     56%     Red
              140     75     54%     56%     Red
    D          110     40     36%     56%     Red
              100     30     30%     56%     Red
              140     74     53%     56%     Red
              100     70     70%     56%     Green
              110     65     59%     56%     Green
    E          130     85     65%     56%     Green
              150     65     43%     56%     Red
              150     70     47%     56%     Red
              160     95     59%     56%     Green
              170     100     59%     56%     Green
    F          200     150     75%     56%     Green
              100     65     65%     56%     Green
              120     40     33%     56%     Red
              140     80     57%     56%     Green
              150     85     57%     56%     Green
    Legend (for the sample data)
    when % of prospect conversion > % of top 20 conversion, then green
    when % of prospect conversion = % of top 20 conversion, then yellow
    when % of prospect conversion < % of top 20 conversion, then red
    NOTE
    i have a few more columns after this, but if i know how to fix these columns, i can manage them too.
    here is my sql
    select a.dealer_region,
             a.dealer_code,
             count(b.id) as  "No of Prospect Received",
             COUNT (case when b.id in (select d.id
                                                   from contact_details d
                                                   where d.contact_result ='04'
                                                   and d.dealer_code = c.dealer_code)
                                then b.id end) "No of Prospect Converted to Booking",
           (COUNT (case when  b.id in (select d.id
                                                    from contact_details d
                                                    where d.contact_result ='04'
                                                    and d.dealer_code = c.dealer_code)
                                then b.id end)
                         / (count(b.id))*100 || '%')"%of Prospect Conversion" ,    
           ((COUNT (case when  b.id in (select d.id
                                                     from contact_details d
                                                     where d.contact_result ='04'
                                                     and d.dealer_code = c.dealer_code
                                                     and rownum <20)
                                then b.id end)
                                                       /20 )* 100 || '%')"% Top 20 Conversion",  
                                    /*(avg((COUNT (case when  b.id in (select d.id
                                                                                       from contact_details d
                                                                                       where d.contact_result ='04'
                                                                                       and d.dealer_code = c.dealer_code)
                                                                   then b.id end)
                                                                                    / (count(b.id))*100 || '%'))
            (case when((COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='04'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <20)
                       then b.id end)
                                / 20 )* 100) > ((count(case when  b.id in (select d.id
                                                                                                                       from contact_details d
                                                                                                                       where d.contact_result ='04'
                                                                                                                      and d.dealer_code = c.dealer_code
                                                                                                                      and rownum <20)
                                                                                                   then b.id end)
                                                                                                                       / 20 )* 100)
                            then 'Green'     
              when((COUNT (case when  b.id in (select d.id
                                                                from contact_details d
                                                                where d.contact_result ='04'
                                                                and d.dealer_code = c.dealer_code
                                                                and rownum <20)
                                           then b.id end)
                                                     / 20 )* 100) = ((count(case when b.id in (select d.id
                                                                                                                                         from contact_details d
                                                                                                                               where d.contact_result ='04'
                                                                                                                      and d.dealer_code = c.dealer_code
                                                                                                                                             and rownum <5)
                                                                                                                  then b.id end)
                                                                                                                           / 20 )* 100)
           then 'Yellow'
           else
           'Red'
            end) "Comparison with Top 20 Convers",
    /* i can handle this part if the sql given above  is solved */
            COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code)
                        then b.id end) "#of Lead ConvertedC",
           (COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code)
                       then b.id end)
               / (count(b.id))*100 || '%')"%of Lead ConversionC",
           ((COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <21)
                       then b.id end)
               / 20 )* 100 || '%')"% Top 20 ConversionC",
          /*  (avg((COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='04'
                                               and d.dealer_code = c.dealer_code)
                       then b.id end)
               / (count(b.id))*100 || '%'))
            (case when((COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <21)
                       then b.id end)
               / 20 )* 100) > ((count(case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <21)
                                               then b.id end)
                                               / 20 )* 100)
           then 'Green'    
           when((COUNT (case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <21)
                       then b.id end)
               / 20 )* 100) = ((count(case when  b.id in (select d.id
                                               from contact_details d
                                               where d.contact_result ='05'
                                               and d.dealer_code = c.dealer_code
                                               and rownum <21)
                                               then b.id end)
                                               / 20 )* 100)
           then 'Yellow'
           else
           'Red'
            end) "Comparison with Top 20 ConverC"                                                                              
    from dealers a, prospect_list b, files c, marketing_details e, customer f
    where b.file_id = c.file_uid
    and c.dealer_code = a.dealer_code
    and b.id = e.id
    and e.id = f.id
    group by a.dealer_region, a.dealer_code; hope someone can help me out.
    i actually can change the count(d.dealer_code) to 20 and solve the problem but what if they are less than 20 dealers in the table?
    because the table will be refreshed every morning with new data and the unnecessary one will be removed.
    it works fine now when i divide it by 20.
    pls advice
    thanks
    alagu
    Edited by: user645399 on Jul 1, 2009 7:49 PM
    Edited by: user645399 on Jul 1, 2009 7:56 PM
    Edited by: user645399 on Jul 1, 2009 8:14 PM
    Edited by: user645399 on Jul 1, 2009 10:20 PM

    Hope this helps. First rank all the rows. Then find the average for the top3 using CASE statement
    LPALANI@l11gr2>select ename, sal, dense_rank() over (order by sal desc) dr,
      2  avg(sal) over() avg_sal_all
      3  from scott.emp;
    ENAME                   SAL               DR      AVG_SAL_ALL
    KING                  5,000                1            2,073
    FORD                  3,000                2            2,073
    SCOTT                 3,000                2            2,073
    JONES                 2,975                3            2,073
    BLAKE                 2,850                4            2,073
    CLARK                 2,450                5            2,073
    ALLEN                 1,600                6            2,073
    TURNER                1,500                7            2,073
    MILLER                1,300                8            2,073
    WARD                  1,250                9            2,073
    MARTIN                1,250                9            2,073
    ADAMS                 1,100               10            2,073
    JAMES                   950               11            2,073
    SMITH                   800               12            2,073
    14 rows selected.
    LPALANI@l11gr2>
    LPALANI@l11gr2>select ename, sal,
      2  avg(case when dr <=3 then sal end) over () avg_sal_top,
      3  count(case when dr <=3 then 1 end) over() cnt_of_top3_earners,
      4  avg_sal_all,
      5  dr
      6  from (select ename, sal, dense_rank() over (order by sal desc) dr, avg(sal) over() avg_sal_all from scott.emp);
    ENAME                   SAL      AVG_SAL_TOP CNT_OF_TOP3_EARNERS      AVG_SAL_ALL               DR
    KING                  5,000            3,494                   4            2,073                1
    FORD                  3,000            3,494                   4            2,073                2
    SCOTT                 3,000            3,494                   4            2,073                2
    JONES                 2,975            3,494                   4            2,073                3
    BLAKE                 2,850            3,494                   4            2,073                4
    CLARK                 2,450            3,494                   4            2,073                5
    ALLEN                 1,600            3,494                   4            2,073                6
    TURNER                1,500            3,494                   4            2,073                7
    MILLER                1,300            3,494                   4            2,073                8
    WARD                  1,250            3,494                   4            2,073                9
    MARTIN                1,250            3,494                   4            2,073                9
    ADAMS                 1,100            3,494                   4            2,073               10
    JAMES                   950            3,494                   4            2,073               11
    SMITH                   800            3,494                   4            2,073               12

  • Put prepended "WITH" into Select statement possible?

    Assume I have a SQL statement like:
    with t1 as (select ......),
    t2 as (select ......)
    Select .... From t1, t2 Where ....;
    Can I merge somehow the prepended "with" term into the actual select statement?
    As far as I remember there is an equivalent statement syntax like:
    Select .... from t1 as (select ....), t2 as (select ....) where ....;
    but this does not work.
    Peter

    It will be like
    Select ....
    From ( select .. From ...) t1
    ,( select .. From ...) t2
    WHERE ...
    Regards
    Arun

  • Help needed with a SELECT statement. How can I make it run faster?

    Hi,
    not sure if my brain is just too tired but I can't seem to crack this problem today.
    Here is my scenario.
    I have 2 tables
    TABLE1 (searchId INTEGER, routeId INTEGER);
    TABLE2 (routeId INTEGER, cityId INTEGER);
    There are indexes on all 4 columns.
    (routeId on TABLE1 is a primary key).
    In the data I am using, a given search has more than 500 routes, each route has between 10 and 300 cities among more than 4000 possible different cities.
    Now, what I want to create is the list of route couple, within a certain search, that do not have a single city in common.
    That list should populate a table with the following structure
    TABLE3 (searchId INTEGER, routeId1 INTEGER, routeId2 INTEGER)
    Here is the fastest select statement I have found so far.
    SELECT :searchId, t1.routeId, t2.routeId FROM table1 t1, table1 t2
    WHERE t1.searchId=:searchId AND t2.searchId=:searchId
    AND t1.routeId>t2.routeId
    AND NOT EXISTS (
    SELECT cityId FROM table2
    WHERE routeId=t1.routeId
    INTERSECT
    SELECT cityId FROM table2
    WHERE routeId=t2.routeId);
    But it still seem really slow to me.
    Any suggestion for an improved version is welcome.
    Thanks,
    Martin.
    Title was edited by:
    user453358

    I originaly posted this thread because I tought I was missing something "obvious" that would perform better that would make my SELECT statement perform better.
    So I did not want to go as deep as using TKPROOF yet.
    Here is the statistics I gets on my statement.
      1   SELECT t1.searchId,t1.routeId, t2.routeId id2
      2      FROM table1 t1, table1 t2
      3     WHERE t1.searchid=t2.searchid
      4      AND t1.searchId=91
      5      AND t1.routeId>t2.routeId
      6      AND NOT EXISTS (
      7             SELECT cityId FROM table2
      8              WHERE routeId=t1.routeId
      9             INTERSECT
    10             SELECT cityId FROM table2
    11*            WHERE routeId=t2.routeId)
    SQL> /
    43302 rows.
    Tidsåtgång: 00:01:55.02
    Körschema
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=9 Card=1 Bytes=14)         
       1    0   FILTER                                                             
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'table1' (TABLE) (Cost=1 Card=1 Bytes=7)                                                
       3    2       NESTED LOOPS (Cost=3 Card=1 Bytes=14)                          
       4    3         TABLE ACCESS (BY INDEX ROWID) OF 'table1' (TABLE) (Cost=2 Card=1 Bytes=7)
       5    4           INDEX (RANGE SCAN) OF 'table1_IDX1' (INDEX) (Cost=1 Card=1)                                                    
       6    3         INDEX (RANGE SCAN) OF 'table1_IDX1' (INDEX) (Cost=0 Card=1)                                                      
       7    1     INTERSECTION                                                     
       8    7       SORT (UNIQUE) (Cost=3 Card=108 Bytes=864)                      
       9    8         TABLE ACCESS (BY INDEX ROWID) OF 'table2' (TABLE) (Cost=2 Card=108 Bytes=864)                                 
      10    9           INDEX (RANGE SCAN) OF 'table2_IDX1' (INDEX) (Cost=1 Card=108)                                               
      11    7       SORT (UNIQUE) (Cost=3 Card=108 Bytes=864)                      
      12   11         TABLE ACCESS (BY INDEX ROWID) OF 'table2' (TABLE) (Cost=2 Card=108 Bytes=864)                                 
      13   12           INDEX (RANGE SCAN) OF 'table2_IDX1' (INDEX) (Cost=1 Card=108)                                               
    Statistik
              1  recursive calls                                                   
              0  db block gets                                                     
        2872765  consistent gets                                                   
              0  physical reads                                                    
            812  redo size                                                         
         964172  bytes sent via SQL*Net to client                                  
          32245  bytes received via SQL*Net from client                            
           2888  SQL*Net roundtrips to/from client                                 
         860256  sorts (memory)                                                    
              0  sorts (disk)                                                      
          43302  rows processed  Looks like a big number of consistent gets! Any idea how to improve on that?
    Martin.

  • Quick help with a select statement...

    Hello all,
    I will try to lay this out as simply as possible.  I have the following table in my code:
    TYPES: BEGIN OF ty_table,
        vbeln TYPE likp-vbeln,
        lfdat TYPE likp-lfdat,
        lfart TYPE likp-lfart,
        wadat_ist TYPE likp-wadat_ist,
        vstel TYPE likp-vstel,
        route TYPE likp-route,
        vsbed TYPE likp-vsbed,
        inco1 TYPE likp-inco1,
        inco2 TYPE likp-inco2,
        kunnr TYPE likp-kunnr,
        kunag TYPE likp-kunag,
        brgew TYPE lips-brgew,
        matnr TYPE lips-matnr,
        arktx TYPE lips-arktx,
        meins TYPE lips-meins,
        lgmng TYPE lips-lgmng,
        vgbel TYPE lips-vgbel,
        name2 TYPE adrc-name2,
        street TYPE adrc-street,
        city1 TYPE adrc-city1,
        region TYPE adrc-region,
        post_code1 TYPE adrc-post_code1,
        lifnr  TYPE lfa1-lifnr,
        name1 TYPE adrc-name1,
        so_vbeln TYPE vbak-vbeln,
        erdat TYPE vbak-erdat,
      END OF ty_table.
    DATA: it_table TYPE TABLE OF ty_table.
    I then run through a select statement as follows:
    * Get data from LIKP and LIPS.
      SELECT a~vbeln a~lfdat a~lfart a~wadat_ist a~vstel a~route a~vsbed b~brgew a~inco1 a~inco2 a~kunnr a~kunag
               b~matnr b~meins b~lgmng b~vgbel b~arktx INTO CORRESPONDING FIELDS OF TABLE it_table
               FROM likp AS a
                INNER JOIN lips AS b ON b~vbeln = a~vbeln
                WHERE a~vstel IN so_vstel
                AND a~wadat_ist IN so_wadat
                AND b~mtart IN so_mtart.
    That works fine.  The next statement, in the report, is the following select.  When this next select runs it clears all of the fields except for vbeln and erdat.  I want c~vbeln to go into it_table-so_vbeln.  Now it is going into it_table-vbeln.
    * Get Sales order info from VBAK
      SELECT c~vbeln c~erdat INTO CORRESPONDING FIELDS OF TABLE it_table
          FROM lips AS a
            INNER JOIN vbap AS b ON b~vbeln = a~vgbel
            AND b~posnr = a~vgpos
              INNER JOIN vbak AS c ON c~vbeln = b~vbeln
              FOR ALL ENTRIES IN it_table
                WHERE a~vgbel = it_table-vgbel.
    I then tried to write the select statement by takign out "into corresponding fields" like the following but it gives my a syntax error on (it_table-so_vbeln , it_table-erdat).
    * Get Sales order info from VBAK
      SELECT c~vbeln c~erdat INTO (it_table-so_vbeln , it_table-erdat)
        FROM lips AS a
          INNER JOIN vbap AS b ON b~vbeln = a~vgbel
          AND b~posnr = a~vgpos
            INNER JOIN vbak AS c ON c~vbeln = b~vbeln
            FOR ALL ENTRIES IN it_table
              WHERE a~vgbel = it_table-vgbel.
    Is there a way to write the second select (the select that is pulling data from VBAK) so that it will not clear the other entries in the table?  Also, is there a way to combine the two selects into one join? 
    Regards,
    Davis

    Following is the entire report, if that makes it easier:
    *& Report  Z_TRANS_EVAL
    REPORT  z_trans_eval.
    TABLES: likp, lips, adrc, vbpa.
    TYPE-POOLS: truxs.
    TYPES: BEGIN OF ty_table,
        vbeln TYPE likp-vbeln,
        lfdat TYPE likp-lfdat,
        lfart TYPE likp-lfart,
        wadat_ist TYPE likp-wadat_ist,
        vstel TYPE likp-vstel,
        route TYPE likp-route,
        vsbed TYPE likp-vsbed,
        inco1 TYPE likp-inco1,
        inco2 TYPE likp-inco2,
        kunnr TYPE likp-kunnr,
        kunag TYPE likp-kunag,
        brgew TYPE lips-brgew,
        matnr TYPE lips-matnr,
        arktx TYPE lips-arktx,
        meins TYPE lips-meins,
        lgmng TYPE lips-lgmng,
        vgbel TYPE lips-vgbel,
        name2 TYPE adrc-name2,
        street TYPE adrc-street,
        city1 TYPE adrc-city1,
        region TYPE adrc-region,
        post_code1 TYPE adrc-post_code1,
        lifnr  TYPE lfa1-lifnr,
        name1 TYPE adrc-name1,
        so_vbeln TYPE vbak-vbeln,
        erdat TYPE vbak-erdat,
      END OF ty_table.
    DATA: it_table TYPE TABLE OF ty_table,
          wa_table LIKE LINE OF it_table,
          it_table_csv TYPE truxs_t_text_data.
    DATA : lv_tabix LIKE sy-tabix.
    DATA temp_string TYPE string.
    DATA w_adrnr TYPE vbpa-adrnr.
    DATA: w_adrnr_sp TYPE vbpa-adrnr.
    * Selction criteria
    SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-004.
    SELECT-OPTIONS: so_vstel FOR likp-vstel,
                    so_wadat FOR likp-wadat_ist,
                    so_mtart FOR lips-mtart.
    SELECTION-SCREEN END OF BLOCK b2.
    * CSV output filename
    SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-003.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT (16) text-002 FOR FIELD p_file.
    PARAMETERS: p_file(30) TYPE c.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN END OF BLOCK b3.
    START-OF-SELECTION.
    * Get data from LIKP and LIPS.
      SELECT a~vbeln a~lfdat a~lfart a~wadat_ist a~vstel a~route a~vsbed b~brgew a~inco1 a~inco2 a~kunnr a~kunag
               b~matnr b~meins b~lgmng b~vgbel b~arktx INTO CORRESPONDING FIELDS OF TABLE it_table
               FROM likp AS a
                INNER JOIN lips AS b ON b~vbeln = a~vbeln
                WHERE a~vstel IN so_vstel
                AND a~wadat_ist IN so_wadat
                AND b~mtart IN so_mtart.
    * Get Sales order info from VBAK
      SELECT c~vbeln c~erdat INTO CORRESPONDING FIELDS OF TABLE it_table
          FROM lips AS a
            INNER JOIN vbap AS b ON b~vbeln = a~vgbel
            AND b~posnr = a~vgpos
              INNER JOIN vbak AS c ON c~vbeln = b~vbeln
              FOR ALL ENTRIES IN it_table
                WHERE a~vgbel = it_table-vgbel.
    * Get internal address number for ship to party
      LOOP AT it_table INTO wa_table.
        lv_tabix = sy-tabix.    "update counter.
        SELECT SINGLE adrnr FROM vbpa INTO w_adrnr WHERE
        vbeln = wa_table-vgbel
        AND parvw = 'WE'.
        SELECT SINGLE lifnr FROM vbpa INTO wa_table-lifnr WHERE
          vbeln = wa_table-vbeln
          AND parvw = 'SP'
          AND  posnr = '000000'.
        SELECT SINGLE name1 FROM lfa1 INTO wa_table-name1 WHERE
          lifnr = wa_table-lifnr.
    * Get address data from VBPA
        SELECT city1 region post_code1 INTO CORRESPONDING FIELDS OF wa_table
        FROM adrc WHERE
        addrnumber = w_adrnr.
        ENDSELECT.
    * Update the internal table
        MODIFY  it_table INDEX lv_tabix FROM wa_table.
      ENDLOOP.
    END-OF-SELECTION.
    * Call correct display procedure(s).
      IF p_file <> ''.
    * Save a CSV file
        PERFORM display_csv.
      ELSE.
    *Display in ALV
        PERFORM display_alv.
      ENDIF.
    *&      Form  display_alv
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM display_alv .
      DATA: gr_alv TYPE REF TO cl_salv_table,
            gr_func TYPE REF TO cl_salv_functions,
            gr_columns TYPE REF TO cl_salv_columns_table,
            gr_column TYPE REF TO cl_salv_column_table,
            gr_error TYPE REF TO cx_salv_error.
      TRY.
          CALL METHOD cl_salv_table=>factory
            IMPORTING
              r_salv_table = gr_alv
            CHANGING
              t_table      = it_table.
        CATCH cx_salv_msg INTO gr_error.
      ENDTRY.
      gr_func = gr_alv->get_functions( ).
      gr_func->set_all( abap_true ).
      gr_alv->display( ).
    ENDFORM.                    " display_alv
    *&      Form  display_csv
    *       text
    *  -->  p1        text
    *  <--  p2        text
    FORM display_csv .
      DATA: w_filename TYPE string.
      CONCATENATE p_file temp_string INTO w_filename.
      CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
        EXPORTING
          i_field_seperator    = ';'
        TABLES
          i_tab_sap_data       = it_table
        CHANGING
          i_tab_converted_data = it_table_csv
        EXCEPTIONS
          conversion_failed    = 1
          OTHERS               = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          filename = w_filename
        TABLES
          data_tab = it_table_csv
        EXCEPTIONS
          OTHERS   = 1.
      PERFORM display_alv.
    ENDFORM.                    " display_csv

  • Need to write with single select statement

    NAME
    SAL
    GENDER
    X    10000    M
    X    9000    F
    Y    8000    M
    Y    7000    M
    Z    6000    F
    Z    5000    F
    A    4000    M
    A    4000    F
    1) How to write a query that displays the name that exist in both male and female gender. using single select statement
    I tried this one
    SELECT NAME FROM EMPL WHERE NAME IN (SELECT NAME FROM EMPL WHERE GENDER='F') AND GENDER='M';
    It worked fine
    But when i converted to single select statement it didn't work . You can find the query below. can i rewrite in any possible ways(using single select statement).
    Select name from table_name where gender='M' and gender='F'
    thanks in advance

    If a Name does not exist with same gender
    i.e
    Name     Gender
    X              M
    X              M    
    if above sceniorio not exist in ur data than following query will work
    select name from table_name
    group by name
    having count(name) > 1

Maybe you are looking for

  • Adobe Photoshop CS2

    Topic Photoshop CS2 and Nikon D200 Chuck Dell - 09:20am Dec 19, 2005 Pacific Just purchased new Nikon D200. RAW Images will not open in Photoshop CS2. Receive Adobe Photoshop CS2 error message "Could not complete your request because of a program err

  • I got a new computer..how do I get my library to new iTunes

    I got a new computer for Christmas. I clicked on home sharing so I can see my library from the old computer and after we finish getting info from that computer to this one, it will be garbage. So I just imported a CD into my iTunes library on this ne

  • Links within a textbox

    Does anybody know how to handle URL links within a textbox in Jdeveloper 3.1? Thx. null

  • Can possible write as a insert without update

    Hi, I'm inserting values of first 5 coulmns like below INSERT INTO SS_ITEM_STAT_OPN(         REP_ID, ITEM_ID, REPORT_COLUMN_DISPLAY, REPORT_COLUMN_SEQ_NO, BEGIN_VALUE, END_VALUE)         SELECT         LN_SEQ_REP_ID, ITEM_ID, REPORT_COLUMN_DISPLAY, R

  • All Files preference is not really all files?

    Time machine must not really back up all files as it says in the preferences, as a significant difference exists in my set-up between file number counts on the drives (both individually and collectively) and what is reported by Time Machine as the co