Solved

query returning everything

Posted on 2016-11-07
11
87 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
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 48

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
 

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 76

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 76

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 76

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database connection opened on a machine 8 54
data lookup in Oracle - need suggestions 55 102
How can I rollback insert statements after commit in oracle? 7 58
Queries 15 34
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

911 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

19 Experts available now in Live!

Get 1:1 Help Now