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 2008 Forums
 Transact-SQL (2008)
 [SOLVED]Querry question

Author  Topic 

nechtmarrie
Starting Member

24 Posts

Posted - 2013-05-28 : 06:38:46
Okay so i ame trying to create a querry with a subquerry.

When i run the subquerry only i get the correct output i need, this is the sub querry

SELECT * FROM Ziektes_Verlof WHERE (@Datum BETWEEN Start_Datum AND Eind_Datum) OR (@Datum >= Start_Datum AND Eind_Datum IS NULL)

Now i want to make it so that i get a list of staff that is not assinged as absent, for this i used the following querry.

SELECT Naam, Rijbewijs FROM Chauffeurs c WHERE NOT EXISTS (SELECT Naam FROM Ziektes_Verlof WHERE Naam = c.Naam AND (@Datum BETWEEN Start_Datum AND Eind_Datum) OR (@Datum >= Start_Datum AND Eind_Datum IS NULL))


This however returns an empty table. Dous anyone see what i ame doing wrong?

grts,
Necht

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-28 : 06:49:14
one thing you do... check this result and let us know the below query returns records or not?

SELECT Naam FROM Chauffeurs
EXCEPT
SELECT Naam FROM Ziektes_Verlof WHERE (@Datum BETWEEN Start_Datum AND Eind_Datum) OR (@Datum >= Start_Datum AND Eind_Datum IS NULL)


--
Chandu
Go to Top of Page

nechtmarrie
Starting Member

24 Posts

Posted - 2013-05-28 : 06:54:45
That returns a list of the names, however when i add the Collum Rijbewijs it gives an error.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-28 : 07:03:18
[code]
SELECT Naam, Rijbewijs FROM Chauffeurs c WHERE NOT EXISTS (SELECT 1 FROM Ziektes_Verlof WHERE Naam = c.Naam AND ((@Datum BETWEEN Start_Datum AND Eind_Datum) OR (@Datum >= Start_Datum AND Eind_Datum IS NULL)))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

nechtmarrie
Starting Member

24 Posts

Posted - 2013-05-28 : 07:32:28
Yup that did it, thnx visakh!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-28 : 07:45:26
welcome

the issue was OR bypassing the EXISTS check based on Naam field value

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -