To start off i have a database table that consists of "profiles", another
that consists of "users" and finally one that consists of "exclusions ",
these are defined in the DDL below. I am trying to select all the profiles
minus any exclusions that are set up for that user but i keep getting
duplicate entries for profiles that aren't excluded and single entries for
profiles that are excluded.
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'Users') DROP TABLE Users
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'User_Profile_E xclusions') DROP TABLE User_Profile_Ex clusions
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'Profiles') DROP TABLE Profiles
CREATE TABLE Users
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
NAME VARCHAR(50) NOT NULL
)
CREATE TABLE User_Profile_Ex clusions
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
User_ID INT NOT NULL,
Profile_ID INT NOT NULL
)
CREATE TABLE Profiles
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
Name VARCHAR(50) NOT NULL
)
INSERT INTO Users (Name) VALUES ('Bob')
INSERT INTO Profiles (Name) VALUES ('Microsoft')
INSERT INTO Profiles (Name) VALUES ('Intel')
INSERT INTO Profiles (Name) VALUES ('IBM')
INSERT INTO Profiles (Name) VALUES ('Sony')
INSERT INTO User_Profile_Ex clusions (User_ID,Profil e_ID) VALUES (1,3)
INSERT INTO User_Profile_Ex clusions (User_ID,Profil e_ID) VALUES (1,4)
SELECT P.*
FROM Profiles as P
JOIN User_Profile_Ex clusions AS UPE ON UPE.Profile_ID <> P.ID
WHERE UPE.User_ID = 1
The results i'm expecting to get back should look something like this:
ID Name
-----------------
1 Microsoft
2 Intel
Instead i'm getting back:
ID Name
-----------------
1 Microsoft
2 Intel
4 Sony
1 Microsoft
2 Intel
3 IBM
that consists of "users" and finally one that consists of "exclusions ",
these are defined in the DDL below. I am trying to select all the profiles
minus any exclusions that are set up for that user but i keep getting
duplicate entries for profiles that aren't excluded and single entries for
profiles that are excluded.
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'Users') DROP TABLE Users
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'User_Profile_E xclusions') DROP TABLE User_Profile_Ex clusions
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCH EMA.TABLES WHERE TABLE_NAME
= 'Profiles') DROP TABLE Profiles
CREATE TABLE Users
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
NAME VARCHAR(50) NOT NULL
)
CREATE TABLE User_Profile_Ex clusions
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
User_ID INT NOT NULL,
Profile_ID INT NOT NULL
)
CREATE TABLE Profiles
(
ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
Name VARCHAR(50) NOT NULL
)
INSERT INTO Users (Name) VALUES ('Bob')
INSERT INTO Profiles (Name) VALUES ('Microsoft')
INSERT INTO Profiles (Name) VALUES ('Intel')
INSERT INTO Profiles (Name) VALUES ('IBM')
INSERT INTO Profiles (Name) VALUES ('Sony')
INSERT INTO User_Profile_Ex clusions (User_ID,Profil e_ID) VALUES (1,3)
INSERT INTO User_Profile_Ex clusions (User_ID,Profil e_ID) VALUES (1,4)
SELECT P.*
FROM Profiles as P
JOIN User_Profile_Ex clusions AS UPE ON UPE.Profile_ID <> P.ID
WHERE UPE.User_ID = 1
The results i'm expecting to get back should look something like this:
ID Name
-----------------
1 Microsoft
2 Intel
Instead i'm getting back:
ID Name
-----------------
1 Microsoft
2 Intel
4 Sony
1 Microsoft
2 Intel
3 IBM
Comment