Home » SQL & PL/SQL » SQL & PL/SQL » IF is not working right please suggest
IF is not working right please suggest [message #600972] Thu, 14 November 2013 05:18 Go to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member
if
(b.Chek_date between to_date ('Oct-15-2013','mon/dd/yyyy')and to_date ('oct-31-2013','mon/dd/yyyy') then A.pay_frequency IN('2','3','4')
when b.chek_date < to_date('oct-15-2013', 'mon/dd/yyyy') then A.pay_frequency IN('1')
End if
Re: IF is not working right please suggest [message #600973 is a reply to message #600972] Thu, 14 November 2013 05:23 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
follow the guidelines. You have now been asked several times in various posts to use code tags, to post test cases and post appropriate explanations of your problem(s). Why do you refuse to follow these requests?
Re: IF is not working right please suggest [message #600975 is a reply to message #600973] Thu, 14 November 2013 05:26 Go to previous messageGo to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member
if 
(b.Chek_date between to_date ('Oct-15-2013','mon/dd/yyyy')and to_date ('oct-31-2013','mon/dd/yyyy') then A.pay_frequency IN('2','3','4')
when b.chek_date < to_date('oct-15-2013', 'mon/dd/yyyy') then A.pay_frequency IN('1')
End if

[Updated on: Thu, 14 November 2013 05:30]

Report message to a moderator

Re: IF is not working right please suggest [message #600978 is a reply to message #600975] Thu, 14 November 2013 05:32 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
that's not valid syntax.
what are you trying to achieve?
Re: IF is not working right please suggest [message #600981 is a reply to message #600978] Thu, 14 November 2013 05:35 Go to previous messageGo to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member
where check_date is 'Oct-15-2013','mon/dd/yyyy' to 'oct-31-2013' then it will show pay frequency =2,3,5 and
when check_date is less the 'Oct-15-2013' then pay frequency =1
Re: IF is not working right please suggest [message #600985 is a reply to message #600981] Thu, 14 November 2013 05:40 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Post a test case.
Post the full code that you are trying to use.
post an explanation of what you are trying to do.
Put a bit of effort in please, we're not here to do your job for you, we are here to try top help you, but if all you do is post partial bits of code, there is nothing we can do. Your choice.
Re: IF is not working right please suggest [message #600986 is a reply to message #600981] Thu, 14 November 2013 05:41 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
sahnra wrote on Thu, 14 November 2013 12:35
where check_date is 'Oct-15-2013','mon/dd/yyyy' to 'oct-31-2013' then it will show pay frequency =2,3,5 and
when check_date is less the 'Oct-15-2013' then pay frequency =1

In fact, it is OR as the condition on CHECK_DATE are mutually exclusive.
What about simple AND/ORs?
where ( <check_date is 'Oct-15-2013','mon/dd/yyyy' to 'oct-31-2013'> AND <pay frequency =2,3,5> )
   OR ( <check_date is less the 'Oct-15-2013'> AND <pay frequency =1> )

(I will let you to construct conditions between less/greater-then brackets with valid syntax)
Re: IF is not working right please suggest [message #600990 is a reply to message #600986] Thu, 14 November 2013 05:50 Go to previous messageGo to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member

SELECT A.COMPANY, A.Process_level, C.Head_company, D.Head_proc_Lev,  A.EMPLOYEE, 
 MAX(B.CHECK_DATE) Last_check_date,Term_date, A.Pay_frequency, A.EMP_STATUS, A.date_hired, 

A.adj_hire_date, C.GBU,C.Service_Mgr STM, C.hrp, A.Fica_NBR  FROM LAWSON.EMPLOYEE A, LAWSON.PAYMASTR 

B, LAWSON.COMPROF C, LAWSON.TAXGROUP D
 
 
  WHERE 
  
   A.EMPLOYEE = B.EMPLOYEE
AND A.PROCESS_LEVEL = C.PROCESS_LEVEL
AND A.COMPANY = B.COMPANY
AND A.COMPANY = C.COMPANY
And A.company = D.company
and A.PROCESS_LEVEL= D.process_level
and (Term_date >to_date ('Oct-02-2013','mon/dd/yyyy') or Term_date = To_date('jan-01-1700','mon/dd/yyyy') )
AND C.COMPANY = '1'
and
if 
(b.Chek_date between to_date ('Oct-15-2013','mon/dd/yyyy')and to_date ('oct-31-2013','mon/dd/yyyy') then A.pay_frequency IN('2','3','4')
when b.chek_date < to_date('oct-15-2013', 'mon/dd/yyyy') then A.pay_frequency IN('1')
End if


and A.adj_hire_date < to_date ('Oct-01-2013','mon/dd/yyyy') 

Group by  A.COMPANY,A.Process_level, C.Head_company, C.National_Name, A.Department, A.EMPLOYEE, 

A.Term_date, A.EMP_STATUS, A.date_hired, A.adj_hire_date, B.CHECK_DATE ,C.GBU, C.Service_Mgr, C.hrp, 

D.Head_proc_Lev, A.Pay_frequency, A.Fica_NBR 
ORDER BY B.CHECK_DATE DESC;

Re: IF is not working right please suggest [message #600991 is a reply to message #600990] Thu, 14 November 2013 05:55 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
As has already been said, IF is not valid syntax in SQL. To add to that, you cannot conditionally create a condition i.e. the following is invalid:
where case when x = y then a in (1,2, 3)
           else y = m
      end;
 


Take a look at flyboy's suggestion.

You might also consider learning how to format your code in a much more structured manner.
Which do you think is easier to read and debug, yours or this:
select a.company
    , a.process_level
    , c.head_company
    , d.head_proc_lev
    , a.employee
    , max(b.check_date) last_check_date
    , term_date
    , a.pay_frequency
    , a.emp_status
    , a.date_hired
    , a.adj_hire_date
    , c.gbu
    , c.service_mgr stm
    , c.hrp
    , a.fica_nbr
from  lawson.employee a
    , lawson.paymastr b
    , lawson.comprof c
    , lawson.taxgroup d
where a.employee = b.employee
  and a.process_level = c.process_level
  and a.company = b.company
  and a.company = c.company
  and a.company = d.company
  and a.process_level= d.process_level
  and (term_date >to_date ('Oct-02-2013','mon/dd/yyyy')
       or term_date = to_date('jan-01-1700','mon/dd/yyyy') )
  and c.company = '1'
--  and if (b.chek_date between to_date ('Oct-15-2013','mon/dd/yyyy')and to_date
--      ('oct-31-2013','mon/dd/yyyy') then a.pay_frequency in('2','3','4') when
--      b.chek_date < to_date('oct-15-2013', 'mon/dd/yyyy') then a.pay_frequency
--      in('1')
--end if
  and a.adj_hire_date < to_date ('Oct-01-2013','mon/dd/yyyy')
group by a.company
    , a.process_level
    , c.head_company
    , d.head_proc_lev
    , a.employee
    , term_date
    , a.pay_frequency
    , a.emp_status
    , a.date_hired
    , a.adj_hire_date
    , c.gbu
    , c.service_mgr 
    , c.hrp
    , a.fica_nbr
order by b.check_date desc;

[Updated on: Thu, 14 November 2013 05:58]

Report message to a moderator

Re: IF is not working right please suggest [message #600994 is a reply to message #600991] Thu, 14 November 2013 06:01 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
@pablolee: that is invalid too as CASE expression supports only expressions in THEN/ELSE clauses (and BOOLEAN conditions are not expressions).

@sahnra:
Ok, if you did not read my previous post, I will take it another way.

IF is PL/SQL statement, so it is described in PL/SQL Language Reference, which is available with other Oracle documentation books e.g. online on http://tahiti.oracle.com/

It is NOT supported in SQL condition. If you want to use some special one, check the chapter about conditions in SQL Language Reference book. It is placed at the same place as the previous one.

Once again: what is wrong with simple AND/OR conditions???
And the last: do NOT invent your own syntax, do follow the one which Oracle supports.
Re: IF is not working right please suggest [message #600998 is a reply to message #600994] Thu, 14 November 2013 06:27 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
flyboy wrote on Thu, 14 November 2013 12:01
@pablolee: that is invalid too as CASE expression supports only expressions in THEN/ELSE clauses (and BOOLEAN conditions are not expressions).
Yes, that's why I said
Quote:
the following is invalid:
...

Re: IF is not working right please suggest [message #601001 is a reply to message #600998] Thu, 14 November 2013 06:47 Go to previous messageGo to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member
When i am using And/or It takes more time to execute that's why i thougt "IF" condition May Work.
Re: IF is not working right please suggest [message #601002 is a reply to message #601001] Thu, 14 November 2013 06:49 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Why not try flibbityfloppit then? That's just as irrelevant to SQL. Wink
Re: IF is not working right please suggest [message #601004 is a reply to message #601002] Thu, 14 November 2013 07:46 Go to previous messageGo to next message
sahnra
Messages: 39
Registered: November 2013
Location: India
Member
Smile
Re: IF is not working right please suggest [message #601005 is a reply to message #601004] Thu, 14 November 2013 08:02 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>When i am using And/or It takes more time to execute

post query & EXPLAIN PLAN for above
Re: IF is not working right please suggest [message #601027 is a reply to message #601002] Thu, 14 November 2013 17:52 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Quote:
OP had the correct syntax, but it was taking too long so they tried a different, invalid, approach.

LOL Laughing

pablolee wrote on Thu, 14 November 2013 12:49
Why not try flibbityfloppit then? That's just as irrelevant to SQL. Wink


Pablolee, don't even write these suggestions, OP may try to use them to make query faster. Razz
Re: IF is not working right please suggest [message #601039 is a reply to message #601027] Fri, 15 November 2013 00:38 Go to previous message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Smile I'm sure that the OP took the suggestion in the fun way that it was intended Smile

[Updated on: Fri, 15 November 2013 00:39]

Report message to a moderator

Previous Topic: Use of All with equal (=)
Next Topic: Oracle view creation
Goto Forum:
  


Current Time: Wed Apr 24 17:49:27 CDT 2024