Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Use "where" clause if exists only

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-31 : 11:22:18
I have a query and wonder if it is possible to have the "where" clause do the test only if record exists.


select           distinct
case
WHEN dbo.EventStatusType.KeyName = 'sync' then 'Level 1'
WHEN dbo.EventStatusType.KeyName = 'approval' and dbo.EventApprovalLevel.Level = 1 THEN 'Level 2'
WHEN dbo.EventStatusType.KeyName = 'approval' THEN 'Level 2'
WHEN dbo.EventStatusType.KeyName = 'backofficeadd' THEN 'Rollback'
else Null
end as approvalneeded,
goLabor30.dbo.Job.CompanyJobID as jobnumber
from goLabor30.dbo.Batch
inner join dbo.Event ON dbo.Event.BatchGuid = dbo.Batch.BatchGuid
left join dbo.EventApprovalLevel ON dbo.EventApprovalLevel.EventApprovalLevelGuid = dbo.Event.EventApprovalLevelGuid
where (dbo.EventApprovalLevel.Level IS NOT Null then dbo.EventApprovalLevel.Level <> 2) ?????????????

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-08-31 : 11:33:44
Yes, search BOL for "Exists"

This

(dbo.EventApprovalLevel.Level IS NOT Null then dbo.EventApprovalLevel.Level <> 2)

Is not syntactically correct. What are you trying to do?

An infinite universe is the ultimate cartesian product.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-31 : 11:38:26
On the left join and if record exists then I need to check if dbo.EventApprovalLevel.Level <> 2
Go to Top of Page

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-08-31 : 11:41:30
If I understand you correctly, change then to AND.



An infinite universe is the ultimate cartesian product.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-31 : 11:58:21
where (dbo.EventApprovalLevel.Level IS NOT Null then and dbo.EventApprovalLevel.Level <> 2)
is the way cat_jesus told you.

It is also possible in this case to do it in the ON-clause.
ON dbo.EventApprovalLevel.EventApprovalLevelGuid = dbo.Event.EventApprovalLevelGuid AND dbo.EventApprovalLevel.Level <> 2


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-31 : 11:59:02
Not sure if I explain myself correctly. If there is no success on my left join then I do not really want to execute the "where" clause. Using an "AND' will only return records if sucess on the left join.

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-31 : 12:02:32
So your original WHERE looks different to what you have posted?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-08-31 : 12:11:03
quote:
Originally posted by snufse

Not sure if I explain myself correctly. If there is no success on my left join then I do not really want to execute the "where" clause. Using an "AND' will only return records if sucess on the left join.





I think what you mean is that your WHERE clause is eliminating rows that don't have a match to your LEFT JOIN? (and you don't want them to be removed)


If that's what you mean then just move the logic in the WHERE into the the JOIN.

Try replacing your join with this:

LEFT JOIN dbo.EventApprovalLevel ON
dbo.EventApprovalLevel.EventApprovalLevelGuid = dbo.EVENT.EventApprovalLevelGuid
AND dbo.EventApprovalLevel.LEVEL <> 2

and ditch the WHERE clause completely.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-31 : 12:33:02
Here is my complete query:

select           distinct
goLabor30.dbo.Event.BatchGuid as batch_guid,
goLabor30.dbo.Batch.ReportDate as batchreportdate,
substring(goLabor30.dbo.Batch.Name, 1, 30) as batchname,
substring(goLabor30.dbo.Region.CompanyRegionId, 1, 20) as batchcompany,
substring(goLabor30.dbo.Employee.Name, 1, 30) as batchowner,
substring(goLabor30.dbo.EventStatusType.Name, 1, 30) as batchstatus,
CASE
WHEN dbo.EventStatusType.KeyName = 'sync' then 'Level 1'
WHEN dbo.EventStatusType.KeyName = 'approval' and dbo.EventApprovalLevel.Level = 1 THEN 'Level 2'
WHEN dbo.EventStatusType.KeyName = 'backofficeadd' THEN 'Rollback'
ELSE Null
end as approvalneeded,
goLabor30.dbo.Job.CompanyJobID as jobnumber
from goLabor30.dbo.Batch
inner join dbo.Event ON dbo.Event.BatchGuid = dbo.Batch.BatchGuid
join dbo.EventStatusType ON dbo.EventStatusType.EventStatusTypeGuid = dbo.Event.EventStatus
join dbo.Account ON dbo.Account.AccountGuid = dbo.Batch.AccountGuid
join dbo.Employee ON dbo.Employee.EmployeeGuid = dbo.Account.EmployeeGuid
join dbo.Region ON dbo.Region.RegionGuid = dbo.Batch.RegionGuid
join dbo.Job ON dbo.Job.Jobguid = dbo.Event.JobGuid
join dbo.EventType ON dbo.EventType.EventTypeGuid = dbo.Event.EventTypeGuid
left join dbo.EventApprovalLevel ON dbo.EventApprovalLevel.EventApprovalLevelGuid = dbo.Event.EventApprovalLevelGuid
where goLabor30.dbo.Region.CompanyRegionID = '00022' and
goLabor30.dbo.Batch.Reportdate >= '2009-08-24'and
goLabor30.dbo.Batch.ReportDate <= '2009-08-25' and
dbo.EventStatusType.Name <> 'Loaded to ERP' and
(dbo.EventApprovalLevel.Level IS NOT Null and dbo.EventApprovalLevel.Level <> 2)
order by goLabor30.dbo.Event.BatchGuid


There are at moment only 2 "sync" records in the table. The "sync" records does not have corresponding record in the left join. Now when I run I get 0 records returned.

If I comment out the line "(dbo.EventApprovalLevel.Level IS NOT Null and dbo.EventApprovalLevel.Level <> 2)" I get 2 "sync" records returned which is correct.
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-08-31 : 12:53:27
Purrrfect, that did the trick. Thank you.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-31 : 15:08:30
quote:
Originally posted by Transact Charlie

quote:
Originally posted by snufse

Not sure if I explain myself correctly. If there is no success on my left join then I do not really want to execute the "where" clause. Using an "AND' will only return records if sucess on the left join.





I think what you mean is that your WHERE clause is eliminating rows that don't have a match to your LEFT JOIN? (and you don't want them to be removed)


If that's what you mean then just move the logic in the WHERE into the the JOIN.

Try replacing your join with this:

LEFT JOIN dbo.EventApprovalLevel ON
dbo.EventApprovalLevel.EventApprovalLevelGuid = dbo.EVENT.EventApprovalLevelGuid
AND dbo.EventApprovalLevel.LEVEL <> 2

and ditch the WHERE clause completely.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION



That was my solution 3 posts before


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-09-01 : 04:27:51
quote:

That was my solution 3 posts before



Yes -- I saw but I didn't think OP had quite understood what you meant so I explained it in a little more detail.

Kudos.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -