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)
 Compare Records with Mutliple P_ID

Author  Topic 

wildatom
Starting Member

2 Posts

Posted - 2009-06-10 : 15:22:43
My table:

ID | LOCATION
__ |___________
1f | 4th_Floor
1f | 4th_Floor
1f | 3rd_Floor
1f | 1st_Floor
2f | 4th_Floor
2f | 4th_Floor
3f | 6rd_Floor
4f | 4th_Floor
4f | 4th_Floor
4f | 3rd_Floor
4f | 1st_Floor


I need to find out what Unit_ID's are on 3rd_Floor but are not on the 4th_Floor. As you can see 3f is not on the 4th_Floor. So query would list a result of 3f. I'm not sure how to takle this, I've tried different table views, but was looking for a simple query to do the job.

Thank You

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-06-10 : 15:40:13
Try this:

SELECT ID
FROM YourTable
WHERE LOCATION = '3rd_Floor'
AND ID NOT IN (SELECT ID FROM YourTable WHERE LOCATION = '4th_Floor')


Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

wildatom
Starting Member

2 Posts

Posted - 2009-06-10 : 15:51:16
Worked like a charm. I write queries all the time, and yet I couldn't figure this out. Thank you soooooo much! Glad I joined, maybe I can help someone out.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-10 : 15:53:36
See http://weblogs.sqlteam.com/peterl/archive/2009/05/29/Get-this-group-but-not-that-group.aspx


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-10 : 15:56:34
[code]SELECT ID
FROM Table1
GROUP BY ID
HAVING MAX(CASE Location WHEN '4th_Floor' THEN 2 WHEN '3rd_Floor' THEN 1 ELSE 0 END) = 1[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-06-10 : 15:56:40
You're welcome :)

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page
   

- Advertisement -