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
 General SQL Server Forums
 New to SQL Server Programming
 To view entries dependent if data is present?

Author  Topic 

LongList
Starting Member

3 Posts

Posted - 2012-04-03 : 14:52:56
If I have these tables:


table "claimers"
user_number | claim_key
-------------+----------
1 | 1
2 | 2
2 | 3
(3 rows)

table "items"
item_id | pkey
-----------+------
3 | 1
3 | 2
3 | 3
4 | 5
4 | 6
4 | 7
(6 rows)


How does one does view all entries in items such that when the pkey is claimed in claimers the corresponding user_number is displayed and when not claimed (used), some default value is shown?

(A select statement with "claim_key=pkey" will fail for unclaimed entries or vice-versa, and a statement without such conditional will miss any available info at all)

Such that a table that looks like this becomes the result?

item_id | pkey | user_number
-----------+------+------------
3 | 1 | 1
3 | 2 | 2
3 | 3 | 2
4 | 5 | -
4 | 6 | -
4 | 7 | -
(6 rows)

X002548
Not Just a Number

15586 Posts

Posted - 2012-04-03 : 15:09:44
SELECT * FROM Items i LEFT JOIN Claimers c ON i.pkey = c.claim_key

???

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

LongList
Starting Member

3 Posts

Posted - 2012-04-03 : 15:36:33
Yeah "SELECT * FROM Items i LEFT JOIN Claimers c ON i.pkey = c.claim_key" does it! Thanks! ;-)
Go to Top of Page

LongList
Starting Member

3 Posts

Posted - 2012-04-03 : 16:46:06
Btw,, is there any way to set the default value "0" to something else?
(to avoid collision with user_number=0 etc)
Go to Top of Page
   

- Advertisement -