Condition in Select Query

Hi,Please help to get the output. Thanks.
create table #Real (R1 numeric, R2 numeric, R3 char(10), R4 numeric, R5 char(10))
Insert into #real values (111,900,'PN-UFT',10, 'PN')  --R2 and R3 are same for this group
Insert into #real values (112,900,'PN-UFT',10, 'PN')
Insert into #real values (113,900,'PN-UFT',20, 'PN')
Insert into #real values (114,900,'PN-UFT',20, 'PN')
Insert into #real values (115,901,'PS-UFT',10, 'PS') --R2 and R3 are same for this group
Insert into #real values (116,901,'PS-UFT',10, 'PS')
Insert into #real values (117,901,'PS-UFT',20, 'PS')
Insert into #real values (118,901,'PS-UFT',20, 'PS')
Insert into #real values (119,902,'PT-UFT',10, 'PS') --R2 and R3 are same for this group
Insert into #real values (120,902,'PT-UFT',10, 'PS')
Select * from #real where (If R4=20 then pick these records else pick R4=10 records)
Output should be:
R1 R2 R3 R4 R5
113 928 PN-UFT     20 PN       
114 942 PN-UFT     20 PN       
117 894 PS-UFT     20 PS       
118 837 PS-UFT     20 PS       
119 823 PS-UFT     10 PS       
120 843 PS-UFT     10 PS       

I need to Ignore the rows where R4=10 because I need R4=20 but if R4=20 doesn't exist then should pick R4=10.
Eaxmple-1: Here R4 has 10 and 20 so should pick records of '20''
create table #Real (R1 numeric, R2 numeric, R3 char(10), R4 numeric, R5 char(10))
Insert into #real values (111,900,'PN-UFT',10, 'PN')  
Insert into #real values (112,900,'PN-UFT',10, 'PN')
Insert into #real values (113,900,'PN-UFT',20, 'PN')
Insert into #real values (114,900,'PN-UFT',20, 'PN')
My desired output is:
R1 R2 R3 R4 R5
113 900 PN-UFT     20 PN     
114 900 PN-UFT     20 PN     
Eaxmple-2: Here R4 has not records of ''20'' so should pick the record of ''10"
create table #Real (R1 numeric, R2 numeric, R3 char(10), R4 numeric, R5 char(10))
Insert into #real values (115,901,'PS-UFT',10, 'PS')  
Insert into #real values (116,901,'PS-UFT',10, 'PS')
My desired output for this is:
R1 R2 R3 R4 R5
115 901 PS-UFT     10 PS     
116 901 PS-UFT     10 PS 

Similar Messages

  • URGENT: To change the where condition in select query at runtime ?

    Hi,
    I have to develop a report, 4 which I have created a selection screen with 7 Input Parameters whose value is to be filled by the user while executing the report.
    On the basis of this I do the desired selection of output.
    But the problem is that how do I write my select Query(where condition) if the user enetrs only 2 Input parameters or 3 or whatever he feels like.
    Pls help me out...

    hi,
    check this sample code.
    Here i am populating where condition at runtime.
    DATA: V_WHERE TYPE STRING.
    SELECTION-SCREEN BEGIN OF BLOCK INPUT WITH FRAME TITLE TEXT-001.
    SELECT-OPTIONS : S_VBELN FOR VBAK-VBELN,
                     S_ERDAT FOR VBAK-ERDAT.
    SELECTION-SCREEN END OF BLOCK INPUT.
    START-OF-SELECTION.
      PERFORM POPULATE_WHERE.
      PERFORM GET_VBAK_DATA.
    *&      Form  POPULATE_WHERE
    *       Populate Where
    FORM POPULATE_WHERE .
      IF NOT S_ERDAT[] IS INITIAL.
        CONCATENATE 'VBELN IN S_VBELN'
                    'AND'
                    'ERDAT IN S_ERDAT'
              INTO V_WHERE
              SEPARATED BY SPACE.
      ELSE.
        V_WHERE = 'VBELN IN S_VBELN'.
      ENDIF.
    ENDFORM.                    " POPULATE_WHERE
    *&      Form  GET_VBAK_DATA
    *       GET VBAK DATA
    FORM GET_VBAK_DATA .
      SELECT VBELN
             ERDAT
             VBTYP
             NETWR
             WAERK
             VKORG
             VTWEG
             SPART
        INTO CORRESPONDING FIELDS OF TABLE IT_VBAK
        FROM VBAK
        WHERE VBELN IN S_VBELN
        AND   ERDAT IN S_ERDAT.
       WHERE (V_WHERE).
    endform.
    Regards
    Sailaja.

  • How to include conditions in select query

    i want to write a select query wherein plants have to to be fetched from table vekp
    and if it is a plant then the  set movement type to 999 else leave for exiting.please help me on this

    >
    deep shikha wrote:
    > (This is module pool programming)
    what is modulepool programming doing in form printing forum.
    Please use appropriate forum next time.
    кu03B1ятu03B9к

  • Where condition in select query

    my requirement is this. I want to fetch some records
    from DB table. The field objnr has 12 characters.
    i will supply a substring of this field. the substring
    starts at the 3rd location of field.
    SELECT objnr FROM cosp INTO TABLE gt_itab
                  WHERE objnr like srch_str.
    how to do this? could u give me a possible solution?

    Then you're going to have to go at it this way
    TABLES: cosp.
    DATA:
      BEGIN OF gt_itab OCCURS 0,
        objnr      LIKE cosp-objnr,
      END OF gt_itab.
    DATA:
      wk_objnr      LIKE cosp-objnr,
      srch_str(22) VALUE '1000',
      ss_len       TYPE i.
    START-OF-SELECTION.
      ss_len = strlen( srch_str ).
      SELECT objnr FROM cosp INTO gt_itab-objnr.
        CHECK gt_itab-objnr+2(ss_len) = srch_str.
        APPEND gt_itab.
      ENDSELECT.
    Or this way.
    TABLES: cosp.
    DATA:
      BEGIN OF gt_itab OCCURS 0,
        objnr      LIKE cosp-objnr,
      END OF gt_itab.
    DATA:
      cntlarea(22)  TYPE c  VALUE '1000',
      wk_objnr      LIKE cosp-objnr,
      srch_str(50),
      ss_len       TYPE i.
    START-OF-SELECTION.
      CONCATENATE '%' cntlarea '%' INTO srch_str.
      ss_len = strlen( cntlarea ).
      SELECT objnr FROM cosp INTO gt_itab-objnr
        WHERE objnr LIKE srch_str.
        CHECK gt_itab-objnr+2(ss_len) = cntlarea.
        APPEND gt_itab.
      ENDSELECT.
    Edited by: Paul Chapman on May 29, 2008 8:33 AM

  • In Select query how to fetch values through multiple parameters  where conditon

    Dear Experts,
    I have one table(T) with 4 fields: f1, f2, f3, f4. In that based on f2, f3, f4 and parameter condition( f2 values) how to fetch f1 values. I have attached screen shot. What is the where condition in select query?
    DATA: it_T type standard table of ty_T,
              wa_T type ty_T.
    parameters: p_f2_1 type T-f3,
                      p_f2_2 type T-f3,
                      p_f2_3 type T-f3,
                      p_f2_4 type T-f4,
                      p_f2_5 type T-f4,
                      p_f2_6 type T-f4.
    select f1  f2  f3  f4 from T into table it_T where ?
    What is the where condition ?.
    Regards,
    Abbas.

    Hi Syed,
    Do all the parameters p_f2_1 ... p_f2_6 contain values for the same field f2? And do you need all the entries from the table T which contain f2 = any of the entered value? If yes, Then you can try the following where condition -
    select f1  f2  f3  f4 from T into table it_T
      where f2 =  p_f2_1  OR
                 f2 =  p_f2_2  OR
                 f2 =  p_f2_3  OR
                 f2 =  p_f2_4  OR
                 f2 =  p_f2_5  OR
                 f2 =  p_f2_6.
    P.S: If any of the above parameters are optional as per your requirement it will be better to build a dynamic query and use it in the where condition.
    Regards,
    Rachna.

  • How should I change the following SELECT QUERY?

    Requirement:
    There are checks in the program for material group 2, z93 AND z94 which should be skipped if the sales org = 'JP40'.
    sales organisation is taken from the layout(SE51) dictionary fields (MODULE POOL PROGRAM)
    The select query is:
    select single matnr
                      into t_mvke-matnr  "dummy
                      from mvke
                      where matnr = t_9000-matnr
                        and vkorg = vbak-vkorg
                        and vtweg = vbak-vtweg
                        and mvgr2 in (c_z93,c_z94).
    How should I skip the material group c_z93, c_z94 only for JP40(vbak-vkorg) sales organisation, except JP40 material group should be there for other sales org. but for JP40 material group should be skipped.
    What would be the select query or should I give any kind of IF conditions before select query.
    Please help me out.

    Hi Rani K
    You can try like
    IF VBAK-VKORG = 'JP40'.
    select single matnr
    into t_mvke-matnr "dummy
    from mvke
    where matnr = t_9000-matnr
    and vkorg = vbak-vkorg
    and vtweg = vbak-vtweg.
    ELSE.
    select single matnr
    into t_mvke-matnr "dummy
    from mvke
    where matnr = t_9000-matnr
    and vkorg = vbak-vkorg
    and vtweg = vbak-vtweg
    and mvgr2 in (c_z93,c_z94).
    ENDIF.
    Thanks
    Gaurav

  • 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

  • Select query with variable no of conditions

    Hi,
    I want to write a select query such that the conditions mentioned under WHERE clause can be variable .like :
    Select * from TABLE where F1 = var1 and F2 = var2 and F3 = var3.
    Now on the basis of some conditons the equations in where clause should be used.Say condition is variable value >6.
    Now in this case ,if
    var1 =7 var2 =3 var3 = 8 then the query shud behave like
    select * from TABLE where F1 = var1 and F3 = var3
    if :
    var1 =3 var2 = 4 var3 =3 the :
    select * from TABLE
    But only one query is to be wrote that satisfies all of these
    thanx
    Edited by: aachal on Feb 4, 2011 11:36 AM

    Hi Aachal,
    Use Dynamic select statement.
    Ex:
    data : lv_wherecond type string.
    Select * from TABLE where ( lv_wherecond ).
    Before Select stmt, code as follows.
    if var1 > 6.
           if lv_wherecond is initial.
              F1 = var1.
           else.
              concatenate lv_wherecond ' and F1 = var1'  into lv_wherecond.
           endif.
    elseif var 2 > 6.
            if lv_wherecond is initial.
               F2 = var2
            else.
               concatenate lv_wherecond ' and F2 = var2'  into lv_wherecond.
           endif.
    elseif var3 > 6.
          same as above ..
    endif.
    Hope it helps..
    Regards,
    Sravan Guduru.

  • Select Query Based on date condition

    Hi ,
    Is it Possible.
    i want to run select query based on date condition.
    Eg...
    if the date between 01-jan-01 and 01-jan-05 then
    select * from table1;
    if the date between 02-jan-05 and 01-jan-08 then
    select * from table2;
    Becaz i have data in 2 diffrent tables , based on the date condition i wnt to run the select statement to diffrent tables.
    i dont want plsql here Just SQL needed.
    thanks,
    -R
    Edited by: infant_raj on May 5, 2009 11:48 PM

    Helo Kanish,
    this is not the one i was asking..
    wht i mean was .
    i use bind variable to get date while running the select statement , once i get the date then i want to choose any one of the table to run select query.
    EG..
    select col1,col2 from table1 where date between only if 01-jan-01 and 01-jan-05;
    select col1,col2 from table2 where date between only if 02-jan-05 and 01-jan-08;
    Run any one of the two . not all
    thanks,
    _raj                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How can replace IF condition for a Select query in my reports?

    IF s_bukrs-LOW = '4312' OR s_bukrs-LOW = '4313' OR s_bukrs-LOW = '4349'  .
    ELSEIF s_bukrs-LOW = '4310' OR s_bukrs-LOW = '4311' OR s_bukrs-LOW = '4587'.
    ENDIF.
    What if I want to use select query in place of IF condition, in my report for a Z -table in which I have made entries of ZZUSEREXIT-my progam name ,VAR1-4310,VAR2-4311,VAR3-4312,VAR4-4313,VAR5-4349,VAR6-4587?

    HI
    U can do this by two ways-
    (1) Using Two SELECT statementsie. one for IF statements and second for ELSEIF statements.
    Like--
        SELECT < field name>,
    WHERE s_bukrs-LOW = '4312' OR s_bukrs-LOW = '4313' OR s_bukrs-LOW = '4349' 
    SELECT < field name>,
    WHERE s_bukrs-LOW = '4310' OR s_bukrs-LOW = '4311' OR s_bukrs-LOW = '4587'.
    (2) U can do this in using IF with select statements.
    This will help u

  • How to write the select query with complex where condition

    Hi all,
    Can u help me in writing  following select query.
    select * from zu1cd_corr where time_stamp between firstday and lastday .
    In the above query time_stamp contains the date and time.
    where as firstday and lastday contains the dates.
    I need to compare the date in the time_stamp with the firstday and lastday.
    But time_stamp contains the time also and it is char of 14 characters length.

    Hi,
    If that is the case u can do as advait specified....
    if the firstday and secondday are select-options then declare two more variables having 14 character length and then concatenate '000000' to firstday variable and '240000' to last day variable and then write ur query.
    CLEAR : lv_firstday,
                 lv_lastday.
    concatenate firstday '000000' to lv_firstday.
    concatenate lastday '240000' to lv_lastday.
    ranges : r_Date for zu1cd_corr-time_stamp.
    r_date-sign = 'I'.
    r_date-option = 'BT'.
    r_Date-low = lv_firstday.
    r_Date-high = lv_lastday.
    append r_date.
    select * from zu1cd_corr  into table it_zu1cd_corr where time_stamp in  r_Date.
    I hope it helps.
    Regards,
    Nagaraj

  • Mind bender select query condition

    I posted a thread called very interesting select query problem. Because I am new I did not set the question as unanswered. The question is :
    I have selected some material documents from bkpf into i_bkpf. then by looping on it I broke down the AWKEY into mblnr and mjahr and modified the same internal table. Then I created a data base view YMCSKS joining MKPF and MSEG.
    I want to write a select query which does not select mblnr and mjahr from YMCSKS which are already there in i_bkpf
    sort of like this
    select.......
    from VIEW (YMCSKS)
    where MBLNR <> i_bkpf-mblnr

    Manas
    PK is correct.  You've had the same question rejected twice yesterday - do you read your email?    You've really not provided enough information, and so the problem looks very much like you are wanting other people to do your work.  This is no "mind bender".  If you can't do it, then you need to be very clear about what you've already tried - newbies are welcome here, but evidence of effort is expected.
    Rudeness is not tolerated, so please keep the conversation civil.
    If you would like to give more information about what you've tried, and where you are struggling, then I shall leave the thread open.  I'm inviting you to restate your question.
    mâtt

  • Can we use 2 Conditions as Selection Variables in a Query?

    Hi Gurus,
    According to the requirement in our report , we need to have selection variables on 2 Key figures Target Quantity and Consumption.
    So ,we have used Conditions to have Variables in the Selection screen.
    The Conditions (the Selection screen variables) are not working properly.
    Can you please give some pointers on the Conditions to be used in the reports.
    Thanks,
    Sravani

    I did not understand whats the issue is...., is it with the condition? or the variable.
    If with the Condition please provide some more details about the condition.
    If it is a variable, as the other person said, how did you create the variable on the Keyfigure? because as per i know we cannot create any variable for the Keyfigure, but if is possible please let me know, and i can dig in to that, and let you know how to fix the issue.
    Regards,
    Ram
    Edited by: Ram Pawan on Aug 14, 2008 4:06 PM

  • Select query for rows where condition 1

    Say I have a table, tb1, that has the following entries:
    FName LName
    Code
    John Doe
    xxx
    Jane Doe
    xxx
    Steve Harper
    x
    Barrak Obama
    x
    George Bush
    xxxx
    Bill Clinton
    xx
    I'd like to write a select query that only lists the rows where the count for Code is > 1, i.e not display the last two rows from the above entry. How would I go about writing that select query.
    Thanks.

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
    use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. 
    This is minimal polite behavior on SQL forums. 
    >> Say I have a table, tb1, that has the following entries: <<
    Are you this rude or really ignorant and too lazy to read the Netiquette at the start to this forum? Why do you think you do not need a key? No DDL? No constraints? And , why, why do you think that “tb1” is a precise, ISO-11179 conforming table name? 
    Let's make this a table of rude posters rated on a 4-star scale. 
    CREATE TABLE Rude_Posters
    (first_name VARCHAR(15) NOT NULL, 
     last_name VARCHAR(15) NOT NULL, 
     PRIMARY KEY (first_name, last_name), 
     rudeness_code VARCHAR (4) NOT NULL
       CHECK (rudeness_code IN ('x', 'xx', 'xxx', 'xxxx'));
    INSERT INTO Rude_Posters
    VALUES
    ('John', 'Doe', 'xxx'), 
    ('Jane', 'Doe', 'xxx'), 
    ('Steve', 'Harper', 'x'), 
    ('Barrack', 'Obama', 'x'), 
    ('George', 'Bush', 'xxxx'), 
    ('Bill', 'Clinton', 'xx');
    >> I'd like to write a SELECT query that only lists the rows where the count for rudeness_code is > 1, i.e not display the last two rows from the above entry. << 
    The rudeness scale is based on stars, so to ask for an integer value is like saying “on a scale from 1 to 10, what is the color of your favorite letter of the alphabet?”
    Next, the number of stars and your narrative do not match. Did you want to say WHERE rudeness_code IN ('xx' 'xxx', 'xxxx')? 
    Please stop programming SQL until you have gotten more education. At this point, you do not know enough to ask a question. 
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

Maybe you are looking for