Solved

query returning everything

Posted on 2016-11-07
11
106 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 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
'G_F01' is not a procedure or is undefined 3 25
Documenting Data flow 4 40
ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword 2 47
Merging spreadsheets 8 42
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

803 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