Home » SQL & PL/SQL » SQL & PL/SQL » IF Condition in SQL Query?
IF Condition in SQL Query? [message #39664] Wed, 31 July 2002 11:45 Go to next message
tt
Messages: 16
Registered: September 2000
Junior Member
I am trying to create a query that should work with the following set of data:

Name Date
AA 1-1-02
AA 12-30-99
AA 5-7-02
BB 10-5-97

If a name e.g. AA has at least one date between 1-1-02 and 12-31-02 all dates for this name should be displayed. If a name e.g. BB has no date in this time period then no dates should be displayed for this name. The query should therefore retrieve all 3 AA/ date combinations but not the BB one. How would I wright such a query?

Thanks,
TT
Re: IF Condition in SQL Query? [message #39665 is a reply to message #39664] Wed, 31 July 2002 12:13 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Here are a couple of options. Which one is best for you depends on factors like data volume, indexing, etc.

select name, the_date
  from t t1
 where exists (select null
                 from t t2
                where t2.name = t1.name
                  and to_char(the_date, 'yyyy') = 2002);
 
select name, the_date
  from t
 where name in (select name
                  from t
                 where to_char(the_date, 'yyyy') = 2002);
Re: IF Condition in SQL Query? [message #39668 is a reply to message #39664] Thu, 01 August 2002 00:56 Go to previous messageGo to next message
shyampaliyath
Messages: 22
Registered: May 2002
Junior Member
u can use the CASE in sql i f ur using 8.1.6 and above.
i just give u a sample coding

select (case when sal>2000 then 'High Salary' else
'Low Salary' end) from emp
/
u can use the same technique for the above said problem.
Re: IF Condition in SQL Query? [message #39677 is a reply to message #39664] Thu, 01 August 2002 08:30 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
No, CASE is not applicable in this situation. We are dealing here with the inclusion or exclusion of rows, not the manipulation of column data.
Re: IF Condition in SQL Query? [message #39688 is a reply to message #39664] Fri, 02 August 2002 04:35 Go to previous messageGo to next message
shyampaliyath
Messages: 22
Registered: May 2002
Junior Member
hello Mr.Todd Barry

The question that TT has asked is how to implement the
if condition in SQL. i just given an example how the
case statement works. it can be modified for the need of TT.

did u understand!!
Re: IF Condition in SQL Query? [message #39698 is a reply to message #39664] Fri, 02 August 2002 08:40 Go to previous message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
The confusion is due to the fact that the subject ("IF Condition in SQL Query?") does not correlate to the actual question asked. Yes, CASE is an excellent way to handle IF logic, but the real question asked in the body of the message dealt with the inclusion and exclusion of rows based on certain conditions - not how to format columns in the rows.
Previous Topic: Duplicating Long Raw Datatype Record
Next Topic: Referencing an entire row in a trigger
Goto Forum:
  


Current Time: Tue Apr 23 03:49:27 CDT 2024