Solved

query returning everything

Posted on 2016-11-07
11
65 Views
Last Modified: 2016-11-08
hi i have the following query
 
select count(empid) from employees emp,dept i
    where    ((:hire_date_from IS NOT null 
   and      :hire_date_to   IS NOT null
   and      :supplier  IS NOT null
   and      emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_to + 1) 
   and      i.unt                = :supplier)
   or      (:hire_date_from IS NOT null 
   and      :hire_date_to   IS NOT null 
   and      :supplier      IS null
   and      emp.trans_dte      between (hire_date_from - 1) and (:hire_date_to + 1))
   or      (:hire_date_from     IS null
   and      :hire_date_to       IS null
   and      :supplier      IS null));
   

Open in new window

 
 
   the problem is if i pass
   
   hire_date_from    nulll
   hire_date_to      null
   supplier          15588890
   
   
   the query is retuning everything i only what to return 15588890 details
   
   
   but i still what to return everything if
   hire_date_from    null
   hire_date_to      null
   supplier          null
0
Comment
Question by:chalie001
11 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
where is the condition in your query for this type of parameters -->  hire_date_from null ;
   hire_date_to      null ;    supplier          15588890 ;  

what is the count(*) returning when you are passing these values null, null, 15588890 ?
1
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
I think you need a join predicate (or more than one) between those 2 tables (e.g. see bold below):

SELECT
      COUNT(empid)
FROM employees emp
inner join dept i on emp.dept_id = i.id

for the where clause I think it's this
WHERE  (  :hire_date_from IS NOT NULL
      AND :hire_date_to   IS NOT NULL
      AND i.unt = :supplier
      AND emp.trans_dte BETWEEN (:hire_date_from - 1) AND (:hire_date_to + 1)
       )
    OR (
          :hire_date_from IS NOT NULL
      AND :hire_date_to   IS NOT NULL
      AND :supplier       IS NULL
      AND emp.trans_dte BETWEEN (hire_date_from - 1) AND (:hire_date_to + 1)
       )
    OR (:supplier IS NULL)
;

Open in new window

0
 

Author Comment

by:chalie001
Comment Utility
hi sorry the original query was suppose to be
select count(empid) from employees emp,dept i
   and    ((:hire_date_from IS NOT null 
   and      :hire_date_from_to   IS NOT null
   and      :supplier IS NOT null
   and      emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_from_to + 1) 
   and      i.iss_unt                = :keys.scr_supplier)
   or      (:hire_date_from IS NOT null 
   and      :hire_date_from_to   IS NOT null 
   and      :supplier     IS null
   and      emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_from_to + 1))
   or      (:hire_date_from     IS null
   and      :hire_date_from_to       IS null
   and      :supplier     IS null)
   or      (:hire_date_from     IS null
   and      :hire_date_from_to       IS null  
   and      :supplier IS NOT null))

Open in new window

0
 

Author Comment

by:chalie001
Comment Utility
this is the query
select count(empid) from employees emp,dept i  
   where idept.id = emp.deptid
   and   ((:hire_date_from IS NOT null 
   and      :hire_date_from_to   IS NOT null
   and      :supplier IS NOT null
   and      emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_from_to + 1) 
   and      i.iss_unt                = :keys.scr_supplier)
   or      (:hire_date_from IS NOT null 
   and      :hire_date_from_to   IS NOT null 
   and      :supplier     IS null
   and      emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_from_to + 1))
   or      (:hire_date_from     IS null
   and      :hire_date_from_to       IS null
   and      :supplier     IS null)
   or      (:hire_date_from     IS null
   and      :hire_date_from_to       IS null  
   and      :supplier IS NOT null))

Open in new window

0
 

Author Comment

by:chalie001
Comment Utility
if  i pass eturning when you are passing these values null, null, 15588890 ? the query return everything
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>if  i pass eturning when you are passing these values null, null, 15588890 ? the query return everything

That is because the last two OR'ed statements do not alter the where clause.

If you pass null, null, 15588890  your select will be:
select count(empid) from employees emp,dept i      where idept.id = emp.deptid


You hit the last part of the where:
(:hire_date_from     IS null
   and      :hire_date_from_to       IS null  
   and      :supplier IS NOT null)

This does nothing to change the rows returned.
0
 

Author Comment

by:chalie001
Comment Utility
So I must remove the last part from my query
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
I can tell you why that query is returning all the rows.

I cannot tell you what you need change because I don't know your requirements or your data.
0
 

Author Comment

by:chalie001
Comment Utility
I what to return value for the supplie passes even if two date are null
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
I cannot say for sure but is this what you want, using the SQL you posted:

Change:
(:hire_date_from     IS null
   and      :hire_date_to       IS null
   and      :supplier      IS null
)


to:
(:hire_date_from     IS null
   and      :hire_date_to       IS null
   and      i.unt                = :supplier
)


That said, I think this will replace the entire SQL you posted:
select count(empid) from employees emp,dept i  
   where idept.id = emp.deptid
   and 
   		((:hire_date_from  is null and :hire_date_from_to is null) or 
   				emp.trans_dte      between (:hire_date_from - 1) and (:hire_date_from_to + 1))
   and    (:supplier IS null or i.iss_unt = :keys.scr_supplier)

Open in new window

0
 

Author Closing Comment

by:chalie001
Comment Utility
(:hire_date_from     IS null
   and      :hire_date_to       IS null
   and      i.unt                = :supplier
)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now