Result of conditional function where clause

create the table datatest
(number of primaryid,
Identification number,
power varchar2 (4).
alias varchar2 (15))

insert into values(1,100,'CNN','NEWS') datatest.
insert into values(2,100,'BBC','SPORTS') datatest.
insert into values(3,101,'CNN','INV') datatest
insert into values(4,101,'ABC','MON') datatest
insert into values(5,102,'BBC','REAL') datatest
insert into datatest values (6 103, 'NEC', "TVL")

I am looking for a query which returns lines with power CNN
If the CNN feed isn't here then return the line with power BBC
This is the result I'm looking for.

1 100, CNN, NEWS
3 101, CNN, INV
5 102, BBC, REAL


Thnx in advance
create table datatest
(
  primaryid number,
  secondaryid number,
  feed varchar2(4),
  the_alias varchar2(15)
);
begin
insert into datatest values(1,100,'CNN','NEWS');
insert into datatest values(2,100,'BBC','SPORTS');
insert into datatest values(3,101,'CNN','INV');
insert into datatest values(4,101,'ABC','MON');
insert into datatest values(5,102,'BBC','REAL');
insert into datatest values (6,103,'NEC','TVL');
end;

select *
from
(
  select
    primaryid,
    secondaryid,
    feed,
    the_alias,
    row_number() over (partition by secondaryid order by decode(feed, 'CNN', 1, 'BBC', 2) asc) as rn
  from datatest
  where feed in ('CNN', 'BBC')
)
where rn = 1

Would be a solution.

Tags: Database

Similar Questions

  • Simple conditional SQL WHERE Clause with two Subselects

    Hi SQL Experts,
    I need some quick help with a query.
    It is a ' select ' for a data source object repository phyiscal table OBI.
    So, I can't put in PL/SQL or in stored procedures. I just need
    have two conditions / selects different according to the user's role (this information comes
    of the session).
    The above does not work:

    -ORA00905. 00000 - 'lack the key word'
    SELECT OFFICENO FROM orgunit
    WHERE

    BOX WHEN "SESSION_VARIABLE' LIKE '% globalmanager %' THEN
    ASSIGNEDOFFICES =
    (
    SELECT DISTINCT ASSIGNEDOFFICES
    FROM USERSTABLE INNER JOIN orgunit WE
    OrgUnit. KEY = USERSTABLE. OFFICE
    WHERE USERSTABLE. USERNAME ='VALUEOF (NQ_SESSION. THE USER)"
    )
    ON THE OTHER
    OFFICENO =
    (
    SELECT DISTINCT USERSTABLE OFFICENO
    INNER JOIN orgunit WE
    OrgUnit. KEY = USERSTABLE. OFFICE
    WHERE USERSTABLE. USERNAME ='VALUEOF (NQ_SESSION. THE USER)"
    )
    END;

    Can anyone help?

    Hello

    I can't tell what you want to do by looking at the code that do not do. Maybe:

    SELECT officeno

    For orgunit

    WHERE (session_variable LIKE '% globalmanager %'

    AND IN assignedofficies

    (

    SELECT assignedoffices

    UserTable U1

    JOIN orgunit o1 ON o1.officekey = u1.officekey

    WHERE u1.username = ' VALUEOF (NQ_SESSION. THE USER)"

    )

    )

    OR (NVL (session_variable

    , '?'

    ) NOT LIKE '% globalmanager % '.

    AND IN officeno

    (

    SELECT officeno

    Of u2 usertable

    JOIN orgunit o2 ON o2.officekey = u2.officekey

    WHERE u2.username = ' VALUEOF (NQ_SESSION. THE USER)"

    )

    )

    ;

    There might be a simpler and more efficient way in function your tables and your limit.

    The condition u2.username = ' VALUEOF (NQ_SESSION. The USER) "almost certainly not what you really want.

    .

    Whenever you have any questions, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).

    See the FAQ forum: https://forums.oracle.com/message/9362002#9362002

  • case of condition in where clause

    Hello

    I am a beginner in SQL, but according to my reading of the manual, this syntax should be acceptable. I'm using application express but tested also in Developer SQL and the conclusion that it fails with an error "invalid relational operator", with the line number pointing to the ELSE clause of the condition, but a number of column pointing to white space.

    This is a select statement nested, but that shouldn't matter. The fragment in question is:

    Select columns
    RES
    where res.villaid =: P605_VILLAID
    and INSTR (: P604_RES_STATES, res.states). = 0
    and (CASE: P604_EXCLUDE_ZERO)
    WHEN 'Y' THEN ' res.rate > 0'
    ELSE ' res.rate > = 0'
    END)
    and res. DEPARTDATE >: P605_STARTDATE
    and res. ARRIVEDATE < =: P605_ENDDATE.

    I hope that this puts ok shaped during the validation. The code is looking for bookings within a range of dates (which works very well when the other conditions are discussed), and should only output offset lines (nights) if the user asks for it in the variable of liaison P604_EXCLUDE_ZERO. There are probably other ways to go about this, but I'm too stubborn to renounce it. Well, not yet anyway.

    Thanks and greetings
    CS
    select some columns
      from res
     where res.villaid = :p605_villaid
           and instr (:p604_res_states, res.states) != 0
           and sign (res.rate) >= case when :p604_exclude_zero = 'Y' then 1 else 0 end
           and res.departdate > :p605_startdate
           and res.arrivedate <= :p605_enddate
    
  • How to check this condition in where clause

    WITH t1 AS
        (SELECT 1 empno, 'Sun' name,100 deptno, 'M' Gender  FROM dual
        UNION ALL
        SELECT 2,'Gun',100 deptno, 'F' FROM dual
        UNION ALL
        SELECT 3, 'Run',50,'M' FROM dual
        UNION ALL
        SELECT 4, 'Nun',75,'F' FROM dual
        UNION ALL
        SELECT 5, 'Tun',25,NULL FROM dual)
        SELECT empno,name,deptno,gender FROM t1;
    
    I want to display those dept. data which is having Genders both 'M' and 'F',

    You can use two ANDed 'exists' conditions...

    ...
    SELECT empno,name,deptno,gender FROM t1 t1o;
    where
    exists (select 'x' from t1 t1i where t1o.deptno = t1i.deptno and t1.gender = 'M')
    and
    exists (select 'x' from t1 t1i where t1o.deptno = t1i.deptno and t1.gender = 'F')
    
  • I need account condition in where clause.

    Hi all

    I have a data

    Product ID AMT

    1100A
    1100B
    1100C
    1100D
    225A
    240E
    255F
    350B
    350A
    350C
    350G
    412H
    413E
    414A
    415I have

    I need following output

    IDAMTProduct
    1100A
    1100B
    1100C
    1100D
    350B
    350A
    350C
    350

    G

    I tried after query, but it does not work

    Select

    ID,

    AMT,

    Product

    t

    Product ID, AMT Group,

    having count (distinct ID) = count (distinct AMT)

    It returns all the rows.

    Help, please

    with t as)

    SELECT id,

    AMT,

    product,

    County (separate amt) over (partition by id) cnt

    of sample_data

    )

    SELECT id,

    AMT,

    product

    t

    where cnt = 1

    order by id

    /

    AMT ID P
    ---------- ---------- -
    1 100 A
    1 100 B
    1 100 C
    1 100 D
    3 50
    3 50
    3 50
    3 50

    8 selected lines.

    SQL >

    SY.

  • where clause with reg_exp

    Hello

    My query is...

    The string is "1,2,3"...

    output must be '1', ' 2 ', ' 3',

    Please help me...!

    Concerning
    SA


    The guru gave the solution...
    select REGEXP_REPLACE('1, 2, 3', '([0-9]+)', '''\1''') from   dual;
    But, I have to use this condition in Where clause on my request. If I put in which the value of the clause does not appear. Any help on that...! Please, I beg you.

    Concerning
    SA

    Hello

    Query: SELECT "' | REPLACE ('1,2,3',',' "',"'). "' val FROM DUAL;

    will convert '1,2,3' string '1', ' 2 ', 3'.

    Run the sub queries first and then run a select statement.

    CREATE TABLE sample_table
    (
    col_a VARCHAR2 (3)
    );

    INSERT INTO sample_table
    VALUES ('1');

    INSERT INTO sample_table
    VALUES ('2');

    INSERT INTO sample_table
    VALUES ('3');

    INSERT INTO sample_table
    VALUES ('4');

    INSERT INTO sample_table
    VALUES ('5');

    COMMIT;

    SELECT col_a
    OF sample_table
    WHERE INSTR ((SELECT ''' ||)) REPLACE ('1,2,3',',' "',"'). "' val
    THE DOUBLE).
    col_a
    ) > 0;

    The select statement above will give you the desired result of "sample_table" as below.

    COL_A
    1
    2
    3

    Kind regards
    Big Boss

  • Using decode in where clause

    Hello

    I need to use decode condition in where clause in such a way that if the respective column is null then consider the corresponding column

    for example

    Fee_dateFee_refund_date
    10 OCTOBER 05
    12 NOVEMBER 05
    11 JANUARY 06
    16 FEBRUARY 06

    I have a request in place as below where p_fee_flag will be passed as either FEES or set to null, I check another condition as if p_fee_flag is null then fee_date must be set to null when comparing fee_refund_date between TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE

    SELECT 1

    BUCKET BKT

    WHERE DECODE (P_FEE_FLAG, 'FEES', TRUNC(FS.) FEE_DATE), TRUNC (FS. FEE_REFUND_DATE))

    BETWEEN TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE)

    944524 wrote:

    Hello Alberto,.

    Thank you that's what I ask but is it possible to verify the condition even using decode

    Yes, it is possible, but why?

    Just replace the query in this way:

    select *
      from fee_table f
    where exists(select 1
                    from bucket b
                  where decode(:p_fee_flag, 'FEE', f.fee_date, f.fee_refund_date) between b.act_start_date and b.act_end_date)
      and decode(:p_fee_flag, 'FEE', null, f.fee_date) is null;
    

    Kind regards.

    Alberto

  • May I ask where clause to satisfy two conditions at the same time (and)

    Hello

    I wrote a few joins and usually in the final stage where condition with certain conditions be satisfied in where clause. In all conditions where I only want one and condition fulfilled only if both are collected.

    If we look at the this funciton is I should meet (get_order_type (ogr.order_number) <>'R')) AND wh1.third_party_warehouse! = 'Y') but I'm not get rid off both that if order_type is 'R' they are removed and if tooling_group is yes then ther are removed the result. I'm trying with both below pointed out together how can check these conditions? I can do in where clause?

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    Of order_goods_received ogr

    LEFT JOIN part_master pm

    On ogr.part_number = pm.part_number

    -stock_groups sg left join

    SG stock_groups left join

    on pm.stock_group = sg.stock_group

    and ((sg.tooling_stock_group! = 'Y'))

    and (get_order_type (ogr.order_number)! = 'R'))

    -Pm.stock_group = sg.stock_group

    LEFT JOIN batch_record_1 br1

    -A FEW MORE CODE HERE-

    WHERE ((ogr.delivery_date > = trunc (date_départ)))

    AND

    ((ogr.delivery_date < = trunc (end_date)))

    AND

    OGR.goods_received_number <>'1' AND

    get_order_type (OGR.order_number) NOT IN ('L') AND

    OGR.returned_to_supplier_indicator not in('S','L') and

    -sg.tooling_stock_group <>'Y '.

    -((sg.tooling_stock_group <> 'Y') and (get_order_type (ogr.order_number) not in ('R')))

    -(get_order_type (OGR.order_number) <>'R' AND sg.tooling_stock_group <>'Y')

    ((sg.tooling_stock_group <> 'Y'))

    and

    (get_order_type (ogr.order_number) <>'R'))

    AND wh1.third_party_warehouse! = « Y »

    UNION

    (

    Thank you.

    Whenever you have a problem with such logic, you should try and draw a logic state diagram:

    In your case, you are looking to two variables, with two possible States (equal to a value or is not it)

    Then, take this sense of the sg.tooling_stock_group T and O as being the get_order_type (ogr.order_number) and create a state diagram logical based on your description of what you want.  We will use a value of "Y" to indicate we want to record and a value of "N" to indicate the folder should not be returned.

    Start with an empty logical diagram...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O = 'R '.
    |         |         |
    |         |         |


    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    First of all, you said: "If there is the result returned by the defined user function is"R"and the table that has the tooling_stock_group y then it should avoid this record."

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |         |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Then you said: "If order_type does not return" R' but the tooling_stock_group is 'Y', then I should not avoid recording ".

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |         | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |         | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now, you have not given any logic for the rest of the diagram, so I made the assumption that you do not want to avoid these records...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    Y    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    Now there are different ways to determine the location of the clause of this diagram.
    We could say that we want the records for all of the T column! = "Y" or all the records for all of the O line! " = R...

    where (sg.tooling_stock_group! = 'Y' or get_order_type (ogr.order_number)! = 'R')

    We say we want to EXCLUDE (using DO NOT) all records where T = "Y" AND O = "O" "

    If not (sg.tooling_stock_group = 'Y' and (ogr.order_number) get_order_type = 'R')

    Those are the two most logical ways to do.
    (In my previous answer, that we can see in this diagram, there is no need of the "or get_order_type (ogr.order_number).) (= 'R' "part, which is superfluous, but would not break it)

    Now let's look at your logic to see what it looks like to in the diagram...

    where ((sg.tooling_stock_group <> «Y»))
    and (get_order_type (ogr.order_number) <> 'R')
    )

    In a logic diagram that looks to...

    T = 'Y' T! = « Y »
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    N    | O = 'R '.
    |         |         |
    |         |         |
    +---------+---------+
    |         |         |
    |         |         |
    |    N    |    Y    | O! = « R »
    |         |         |
    |         |         |
    +---------+---------+

    As you can see, your logic was only targeting one of the 4 possible results for select records and excluding 3.

  • Difference-conditions (join and a Where Clause)

    Hi people,

    I need to clearly agree on what a difference exactly when we put any condition in INNER JOIN and the WHERE Clause.

    I have tried both way and found the same results. Even in the statistics Plan not much differences.  Any help would be appreciated.

    As:

    1 here, I use filter store in the join condition - Inner

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    and I.LOCATION_ID in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    Mark



    Hello

    MarkCooper wrote:

    Hi guys,.

    To reply to all - I understand.

    1. its good practice to use conditions / filter (except CLAUSE) in the WHERE Clause rather Inner join? bon ?

    2. now, in my previous example. We could use the location code in where clause as it was in the two tables.

    What is the best practice to use the code to location here ( 1 /2) ?

    1. here I use filter store in the Inner join condition ( guess the location code is not in the article table)).

    "SELECT i., Gl * Sc1.Item I.

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

      And Gl.Location_Id in (1767, 1747,202,1625)

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

    And I.Condition_Id! = 325

    2. here I use filter store in Where clause (assume that the location code is not in the article table)-

    SELECT i., Gl * Sc1.Item I

    Inner Join Sc1.Part P

    On P.Part_Id = I.Part_Id

    Inner Join Sc1.Location Gl

    On Gl.Location_Id = I.Location_Id

    Inner Join Sc1.Condition C

    On C.Condtion_Id = Gl.Condition_Id

    Where I.Inactive_Ind = 0

      And Gl.Location_Id in (1767, 1747,202,1625)

    And I.Condition_Id! = 325

    Thank you

    If location_id isn't in the item table, then the join condition

    On Gl.Location_Id = I.Location_Id

    will cause an error.

    Once more, it should not affect results or performance if a condition like

    Gl.Location_Id in (1767, 1747,202,1625)

    is in the clause or the WHERE clause.  No matter if it is be a column called location_id in any other table, or if the same column Gl.Location_Id is used in other conditions.

    As this condition only refers to a table (GI), I recommend you put it in a WHERE clause, just to make the code clearer.

    Yet once, this applies only to the inner joins, not for outer joins and not to CONNECT BY queries.

  • Query on calculated field where deletes conditions of the clause

    Hi gurus,

    Under a new requirement, we have created a new field calculated in Contact before JC, which removes the condition from which to extract the clause and made full table.

    Requirement:

    We have a field in the user interface, that has data like "J1234" or "K44335". Customer won't see the first character of the alphabet of these data.

    Solution, we have developed:

    (1) create a new calculated field 'ABC person UId Calc' as follows.

    Name: ABC person UId Calc
    Calculated: true
    Computed value: right ([nobody UId], Len([Person UId]) - 1).

    (2) exposed this field in the user interface.

    Question:

    When the user queries in the applet for this field, the generated SQL code has no condition in a Where Clause to the query on PERSON_UID.

    SQL has earlier been something like...

    (T25. EMP_FLG = 'N') AND (T25. PERSON_UID LIKE: 3)

    But after you make this change, the current SQL is trimmed of the just

    (T25. EMP_FLG = 'N')

    This means that this isn't question with PERSON_UID, resulting in an impact huge performance.

    If someone could suggest on this issue, to the difficulty.

    Concerning

    Oh sorry, I guess I missed something.

    I guess the simplest solution is to store the values of redundantly (A, B the value of the value and the value of A + B) and stop using a calculated field.

    You could script something in the PreQuery event, but I think it's an ugly hack.

  • Cannot run a «BUSINESS...» WHEN ' with a date in a WHERE CLAUSE condition

    Hello

    I need to check for a condition and date according to the date, he would check a set of host names or the other game... so I thought I could write something like this in the WHERE clause:

    AND HOST_NAME IN

    (

    CASE

    WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN

    ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ON THE OTHER

    ("SERVERNAME1", "NAMESERVER2", "SERVERNAME3")

    END

    )

    I get the following:

    ORA-00907: lack of right parenthesis

    00907 00000 - "lack the right parenthesis.

    But there is no missing parentheses!

    If I take the case... when... end, and run the host_name in ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') (separately, they run fine)...

    I also tried:

    AND

    (

    CASE

    WHEN TO_DATE (: StopDate, 'MON-DD-YYYY') > = TO_DATE ('22 - DEC - 2015 "," DD-MON-YYYY ') THEN

    HOST_NAME IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC') ELSE

    HOST_NAME IN ('SERVERNAME1","NAMESERVER2","SERVERNAME3")

    END

    )

    the error I get is:

    ORA-00905: lack of keyword

    00905 00000 - 'lack the key word'

    What keyword miss me?

    Post edited by: c75d2e42-06a0-4eb1-a576-5652edcbfbe8

    Hello

    c75d2e42-06a0-4eb1-A576-5652edcbfbe8 wrote:

    It was a typo when transferred to the Oracle forum... the version of Oracle is: PL/SQL version 10.2.0.3.0 (10 G)

    I also used IF the statement and that has not worked (in error "missing parenthesis") which I'm sure is a matter of "syntax"... I did not know that the CASE statement returns the value single only, so it's probably the problem.  Is it possible to re - write this?

    ...

    IF only works in PL/SQL.  It does not in SQL, even if that SQL is embedded in PL/SQL.

    There are many ways to re - write the condition.  And here's one:

    AND ((host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')

    AND TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")

    )

    OR (host_name IN ('servername1","Nameserver2","SERVERNAME3")

    AND TO_DATE (: StopDate, "MON-DD-YYYY")<  to_date="" ('dec-22-2015', ="">

    )

    )

    If: StopDate can be NULL, it must be a bit more complicated.

    You can also use nested, such as CASE expressions

    AND THE CASE

    WHEN TO_DATE (: StopDate, "MON-DD-YYYY") > = TO_DATE ('22-DEC-2015', "MON-DD-YYYY")

    WHILE CASE

    WHEN host_name IN ('SERVERNAMEA', 'SERVERNAMEB', 'SERVERNAMEC')

    AND THEN 'OK '.

    END

    OTHER CASES

    WHEN host_name IN ('servername1","Nameserver2","SERVERNAME3")

    AND THEN 'OK '.

    END

    END = 'OK '.

    I wonder if this in your application is the best way, however.  If SERVERNAMEA was put into service on December 22, maybe that that fact should be recorded in a table.  If you have a table with 1 row per server, you can consider adding 2 columns DATE to show when the server was first and last used.  Then, rather than use a WHERE clause to test servers appropriate, you could do an inner join.

  • Optimize query with function in where clause

    Hello

    I have a query like this:

    SELECT * FROM table_1 t
    WHERE
    (
    -- Clause A (very long clause that filters a lot of rows)
    )
    AND f(t.field) = 'Y' -- This function is heavy but it should filter few rows
    

    This query, it is very slow because I think he's trying to evaluate f() for all rows in table_1.

    Howerver, if I have database query:

    SELECT f(t.field) FROM table_1 t
    WHERE
    (
    -- very long clause that filters a lot of rows
    )
    

    It's very fast.

    How can I reference the query to filter the lines of division A, then by function?

    Thanks in advance!

    If you wrap the function in a select clause then the optimizer can use a scalar subquery caching:

    SELECT * FROM table_1 t 

    WHERE  (  -- Clause A (very long clause that filters a lot of rows)  ) 

    AND (Select f(t.field) From Dual) = 'Y' -- This function is heavy but it should filter few rows

  • Outer joins and null in the 'where' clause condition

    Hi people,

    Please help me on this.

    Here's my query.

    with x

    (select 'a' as a dual union all col1)

    Select 'b' as col1 of union double all the

    Select 'c' as double col1

    ),

    y as

    (

    Select 'b' as col2 from dual Union all the

    Select 'c' as col2 from dual Union all the

    Select would be "as col2 from dual Union all the"

    Select 'e' as col2 from dual

    )

    Select * x y right outer join

    on x.col1 = y.col2 and y.col2 is null

    Get all the lines of 'COL1' as null. Why like this?

    Just add the condition to the WHERE clause for example

    WITH x AS
    (SELECT 'a' AS col1 FROM dual UNION ALL
    SELECT 'b' AS col1 FROM dual UNION ALL
    SELECT 'c' AS col1 FROM dual
    ),
    y AS
    (
    SELECT 'b' AS col2 FROM dual UNION ALL
    SELECT 'c' AS col2 FROM dual UNION ALL
    SELECT 'd' AS col2 FROM dual UNION ALL
    SELECT 'e' AS col2 FROM dual
    )
    SELECT * FROM x LEFT OUTER JOIN y
    ON x.col1=y.col2                     ----want to add "and y.col2 is null " condition to get value "a"
    where y.col2 is null
    
  • Generating a 'WHERE' clause using a function

    Hi, I have provided,

    given the function at the bottom are input parameters.

    sk_func (salary, dept_no, Job, hire_date)

    whatever input is given these entries should be there in a statement,

    For example if I contribute as

    Select double sk_func(5000,null,null,null);

    I need output where salary = 5000;

    If I give as

    Select double sk_func(5000,null,'Programmer',null);

    I need output to where salary = 5000 and job = 'program';

    Hope am clear,

    In short that never values are non-null, those all should include in my where clause.

    I know how to do it, but in light of the considerations, am finding the best way to do it,


    Thanks for your answers,

    I could do that

    CREATE OR REPLACE FUNCTION get_where_clause_for_emp_req (
       emp_id IN NUMBER,
       salary IN NUMBER,
       job    IN VARCHAR2,
       dept   IN NUMBER
    )
       RETURN VARCHAR2
    IS
       l_where_clause   VARCHAR2 (2000) := 'WHERE 1=1 ';
    BEGIN
      IF emp_id is not null THEN
        l_where_clause := l_where_clause || 'AND emp_id = ' || emp_id || ' ';
      END IF;
    
      IF salary is not null THEN
        l_where_clause := l_where_clause || 'AND salary = ' || salary || ' ';
      END IF;
    
      IF job is not null THEN
        l_where_clause := l_where_clause || 'AND job = ' || job || ' ';
      END IF;
    
      IF dept is not null THEN
        l_where_clause := l_where_clause || 'AND dept = ' || dept || ' ';
      END IF;
    
      RETURN l_where_clause;
    END;
    
  • How to use a function in a Where Clause?

    Hello
    I have a doubt. If MY_FUNCT is a function that returns a Boolean value, can I use in a where clause clause to write a query like this? :

    Select...
    table one
    where the...
    *...*
    and MY_FUNC (a.field) = true
    *...*
    *;*

    Thank you!

    Published by: Mark1970 on 3.27 lug-2-2010

    But is it possible that the function does not return a Boolean value?

    Yes (as sysdate, substr and instr etc.) you can also use your own function in a where clause clause, as long as it does not return a BOOLEAN value.

    I mean how to use a function in a where clause clause?

    Your own pseudocode seems a good example for me.
    So, just give it a try.

Maybe you are looking for