Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2006, 05:56 AM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional Join (depends on table data)

Hey all

I want to write a join condition which depends on the DATA in the joining table, I'll explain:

Let's sat my first table is the users table which holds "UserId" (unique) and other details.
My second table holds users permissions, there may be a row for each user from the first table, IF NOT then i need to get the row with UserId=0 (general permission).

I'm trying to join these two tables with a condition that gets the permission for each user, and if not exists a row with the UserId I should join to UserId=0.

Anyone got an idea how to make this conditional join within one query.
(MSSQL 2K SP4)

Thanks.

 
Old January 10th, 2006, 11:47 AM
Authorized User
 
Join Date: Sep 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Lalit_Pratihari
Default

Hi,

I have simulated your problem and here is the scenario I have created and hope this will help.

USE NORTHWIND

CREATE TABLE USERS
(USERID INT, USERNAME VARCHAR(20))

CREATE TABLE PERMISSION
(USERID INT, PERMISION_ID INT)

INSERT INTO USERS VALUES(1, 'SACHIN')
INSERT INTO USERS VALUES(2, 'UMESH')
INSERT INTO USERS VALUES(3, 'LALIT')

INSERT INTO PERMISSION VALUES(1, 5)
INSERT INTO PERMISSION VALUES(1, 6)

SELECT ISNULL(P.USERID, 0), U.USERNAME, P.PERMISION_ID FROM USERS U LEFT JOIN PERMISSION P ON U.USERID = P.USERID

Here is the Output
-------------------

UserId UserName PermisionId
----------------------------
1 SACHIN 5
1 SACHIN 6
0 UMESH NULL
0 LALIT NULL

And UserId = 0 indicates the records where there is no entry in table permission.

Thanks,
Lalit
 
Old January 10th, 2006, 12:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:
And UserId = 0 indicates the records where there is no entry in table permission.
But the OP requested that in this case, the permission value from the permissions table where the userID = 0 is to be used.

How about:
Code:
SELECT Users.UserID,
    coalesce(Permission, (SELECT Permission
                            FROM Permissions
                           WHERE Permissions.UserID=0))
FROM Users
LEFT JOIN Permissions ON Users.UserID=Permissions.UserID
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
table join debbiecoates SQL Server 2000 2 July 2nd, 2008 01:39 PM
can i make self join table seco MySQL 1 April 18th, 2007 11:17 PM
Join table puzzle... vanjamier SQL Language 1 November 26th, 2004 04:43 AM
could i join function(return table) with a table alyeng2000 SQL Server 2000 6 September 30th, 2004 07:23 AM
outer join on same table roog SQL Language 4 September 30th, 2004 05:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.