Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with stored procedure

Status
Not open for further replies.

nc297

Programmer
Apr 7, 2010
162
US
How do I add this line to the below statement?

and cossn is null from table t16pendmvt

I want to check to make sure the cossn in the table t16pendall is not in the table t16pendmvt



select t.fo, Isnull(COUNT(t.cossn), 0)AS PendinFO1


FROM t16pendall t

left join t16pendmvt m

on t.cossn = m.cossn

where mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')

GROUP BY t.fo

order by t.fo
 
Try
Code:
select t.fo, Isnull(COUNT(t.cossn), 0)AS PendinFO1 


FROM t16pendall t 


where (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')) AND not exists (select 1 from 
t16pendmvt m where m.CossN = t.CossN) 

GROUP BY t.fo

order by t.fo


PluralSight Learning Library
 
Thank you that worked. I want to make sure I have it right... you have select 1...

where you have AND not exists (select 1 from
t16pendmvt m where m.CossN = t.CossN)

but I placed cossn in there is that right I'm getting data back.



select t.fo, Isnull(COUNT(t.cossn), 0)AS PendinFO1


FROM t16pendall t


where (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y'))
AND not exists (select cossn from
t16pendmvt m where m.CossN = t.CossN)

GROUP BY t.fo

order by t.fo
 
When you use EXISTS subquery, the fields that come after SELECT clause do not matter, as EXISTS (select ...) only checks the condition, but doesn't return rows. So, you can do
IF EXISTS (select 1 from ....)

IF EXISTS (select * from ....)

IF EXISTS (select ExistingField(s) from ...)

I think I need to check what will happen if you put non-existing fields after SELECT.

I have a habit of writing

IF EXISTS (select 1 ...)

but most people use

IF EXISTS (select * ...) form.

PluralSight Learning Library
 
Okay great thanks so much for your help. I will keep as I have it and add the cossn field in the subquery. Thanks for the explanation too!
 
I'm back I tried to add this:

and (( Datediff(DAY, t.flg_cdt, Getdate()) > 300 ))


but I'm getting the same results. Where do I add this to get records that are > 300 from getdate where I have it gives me the same numbers with it in or out. Thanks.


SELECT t.fo,

Isnull(COUNT(t.cossn), 0) AS PendFOover1

from t16pendall t


where mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')

AND not exists (select cossn from

t16pendmvt m where m.CossN = t.CossN)

and (( Datediff(DAY, t.flg_cdt, Getdate()) > 300 ))


group by t.fo


order by t.fo











 
Try
Code:
ELECT t.fo, 

Isnull(COUNT(t.cossn), 0) AS PendFOover1 

from t16pendall t 


where mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y') 

AND not exists (select cossn from 

t16pendmvt m where m.CossN = t.CossN) 

and t.flg_cdt <= dateadd(day, -300,getdate())

This is assuming you want records which are 300 and more days old.

PluralSight Learning Library
 
Yes that's exactly what I want so I'm guessing there's none in there. Thanks that one worked as well.
 
Actually, I just noticed a possible problem. You have mixed AND and OR conditions in your WHERE clause. I always add () when I have OR as otherwise for me it's hard to figure out what is going to be an outcome. I suggest you to logically group conditions to make sure OR does not stand alone with multiple ANDs.

PluralSight Learning Library
 
So could you show me how to group them and with the ()?
 
Most likely you need
Code:
WHERE (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')) 

AND not exists (select cossn from 

t16pendmvt m where m.CossN = t.CossN) 

and t.flg_cdt <= dateadd(day, -300,getdate())

PluralSight Learning Library
 
Could you explain why you put more parenthesis around this?

WHERE (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y'))

Does this make it be read first and together?
 
Yes, it means that either of these conditions should be true, but two other conditions (date condition and not exists condition) should always be evaluated in addition.

If you don't put these conditions in (), then there is a possibility that OR condition will be always evaluated and the rest will not, e.g. as long as records satisfy this condition
(MFT_POSN1_CD='a' and aged_alien_rsw='y') they will be added to the final result regardless of date and other check.

That's how I understand it. It may be that it still will work as intended (e.g. still check all conditions), but I don't want to take a chance. So, as long as you have OR in the conditions, I always do a logical grouping.

PluralSight Learning Library
 
Thanks so much for the explanation!!!!
 
To enlarge on markros' explanation, the SSMS help says about AND:
"Combines two Boolean expressions and returns TRUE when both expressions are TRUE. When more than one logical operator is used in a statement, the AND operators are evaluated first. You can change the order of evaluation by using parentheses."
So the extra parentheses are necessary to force evaluation of the OR before the result is combined with the AND. You can see the effect with these two demos:
Code:
select 1
where 1 = 1 or 0 = 0 and 1 = 2
and
Code:
select 1
where (1 = 1 or 0 = 0) and 1 = 2
 
This is great Simon! I have a new project which entails dealing with complex queries which I have never done before. You guys have helped me a lot to understand it all. I have a few more queries which I will try to tackle first then come to you guys for help later today. ;-)

Thanks again!
 
Okay I have another one. I'm getting results are my parethesis correct where I have them? Am I joining the table the correct way too?

SELECT t.fo, Isnull(COUNT(m.cossn), 0) AS pendinfo2
FROM t16pendmvt m
JOIN t16pendall t
ON m.cossn = t.cossn
where ( mvt_typ = 'R' ) AND not ((mvt_loc LIKE 'R%'
or mvt_loc LIKE 'S%' OR mvt_loc LIKE 'V%' ))
GROUP BY t.fo
 
Seems correct to me except that now you're a bit heavy on parenthesis:

Code:
SELECT t.fo, Isnull(COUNT(m.cossn), 0) AS pendinfo2 
       FROM t16pendmvt m 
       JOIN t16pendall t 
       ON m.cossn = t.cossn 
       where  mvt_typ = 'R'  AND not (mvt_loc LIKE 'R%'
       or mvt_loc LIKE 'S%' OR mvt_loc LIKE 'V%' )
       GROUP  BY t.fo

You can keep yours, of course, but to me they are redundant.


PluralSight Learning Library
 
Thanks again! I have another question...

My boss said this is incorrect. I shouldn't have to join the natdocfile twice in both queries only one time. If I don't join it at all then I don't get all of the records in the natdocfile.


SELECT a.doc, pendinfo1, pendinfo2

INTO #temppd


FROM

(select n.doc,Isnull(COUNT(t.cossn), 0)AS PendinFO1

FROM t16pendall t

right join natdocfile n on n.doc=t.fo

and (mft_POSN1_CD in('b','d') or (MFT_POSN1_CD='a' and aged_alien_rsw='y')) and t.cossn not in(select cossn from t16pendmvt)
GROUP BY n.doc)a

join

(SELECT n.doc, Isnull(COUNT(m.cossn), 0) AS pendinfo2
FROM t16pendmvt m
JOIN t16pendall t
ON m.cossn = t.cossn
right join natdocfile n on n.doc=t.fo
and ( mvt_typ = 'R' ) AND not (mvt_loc LIKE 'R%' or mvt_loc LIKE 'S%' OR mvt_loc LIKE 'V%' )
GROUP BY n.doc)b
on a.doc = b.doc

I've joined the natdocfile because I want all records returned in that file even it's a 0 after the count is done. Is there another way to join the natdocfile at the end of the two queries or does it matter that I have it listed twice.

Another thing she doesn't like that I'm not using a where clause and using "and" instead. She said I'm doing everything on the join. Does that matter?







 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top