cancel
Showing results for 
Search instead for 
Did you mean: 

SQL

Prabhu_7
Frequent Advisor

SQL

SELECT count(prod_code)
FROM tSi_Daily_Hist S
WHERE company_code = '0102'
AND NOT EXISTS
(SELECT prod_code
FROM tproduct P);

I have couple of prod codes available in
tSi_Daily_Hist which are NOT in tproduct.
But still above query return 0.
Anything wrong with the query?

Thanks
4 REPLIES
Radim Jarosek
Regular Advisor

Re: SQL

Hi,

maybe this question should be post to another forum, for instance

http://pipetalk.quest-pipelines.com/~dba/login

Try to give us some result from subquery or some example data ....


HTH

Radim
Brian Crabtree
Honored Contributor

Re: SQL

SELECT count(prod_code)
FROM tSi_Daily_Hist S
WHERE company_code = '0102'
AND NOT EXISTS
(SELECT prod_code
FROM tproduct P
where p.prod_code = s.prod_code);

Because you are issing a not exists, you need to make sure you limit it correctly.

Thanks,

Brian
Yogeeraj_1
Honored Contributor

Re: SQL

hi,

maybe the following notes on anti- and semi- joins can also help...
==============================================
A semi-join returns rows that match an EXISTS subquery, without duplicating rows from the left side of the predicate when multiple rows on the right side satisfy the criteria of the subquery. For example:

SELECT * FROM dept
WHERE EXISTS
(SELECT * FROM emp
WHERE dept.ename = emp.ename
AND emp.bonus > 5000);


An anti-join returns rows from the left side of the predicate for which there is no corresponding row on the right side of the predicate. That is, it returns rows that fail to match (NOT IN) the subquery on the right side. For example, an anti-join can select a list of employees who are not in a particular set of departments:

SELECT * FROM emp
WHERE deptno NOT IN
(SELECT deptno FROM dept
WHERE loc = 'HEADQUARTERS');
==============================================


best regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
vasundhara
Frequent Advisor

Re: SQL

Hi,

I think you have missed prod_code in the query.

select count(S.prod_code) from tSi_Daily_Hist S WHERE company_code = '0102' AND S.prod_code NOT EXISTS ( select P.prod_code from tproduct P);

Regards
VJ.