Solved

query returning everything

Posted on 2016-11-07
11
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 41876920
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 49

Expert Comment

by:PortletPaul
ID: 41876951
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
ID: 41876982
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chalie001
ID: 41876993
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
ID: 41876996
if  i pass eturning when you are passing these values null, null, 15588890 ? the query return everything
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41877045
>>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
ID: 41877418
So I must remove the last part from my query
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41877421
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
ID: 41877574
I what to return value for the supplie passes even if two date are null
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41877587
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
ID: 41879990
(:hire_date_from     IS null
   and      :hire_date_to       IS null
   and      i.unt                = :supplier
)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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 Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

688 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