Samantha J T Star
Samantha J T Star

Reputation: 32758

How can I modify a LEFT OUTER JOIN to add a filter for the RIGHT side table

I have three tables in my database.

The data looks like this:

I am using the following SQL:

SELECT 
    AdminTest.AdminTestId,
    AdminTest.Title,
    COUNT(AdminTestQuestion.AdminTestQuestionId) Questions,
    AdminTest.Price,
    UserTest.PurchaseDate
FROM AdminTest
    LEFT OUTER JOIN UserTest
        ON AdminTest.AdminTestId = UserTest.AdminTestId
    JOIN AdminTestQuestion
        ON AdminTest.AdminTestId = AdminTestQuestion.AdminTestId
GROUP BY 
    AdminTest.AdminTestId,
    AdminTest.Title,
    UserTest.UserId

Which gives me a report like this:

AdminTestId  Title    Questions     Price     PurchaseDate
1            Test1    10            0
2            Test2    20            0         1/1/2011
3            Test3    10            10        2/2/2012

Can someone suggest how I could modify this so the SQL takes a parameter of UserId so it could correctly show the tests that have been purchased by a particular user:

This is what I would like to see when I provide a value of 1 for the UserId parameter:

AdminTestId  Title    Questions     Price     PurchaseDate
1            Test1    10            0
2            Test2    20            0         1/1/2011  
3            Test3    10            10     

This is what I would like to see when I provide a value of 2 for the UserId parameter:

AdminTestId  Title    Questions     Price     PurchaseDate
1            Test1    10            0
2            Test2    20            0         
3            Test3    10            10        2/2/2012

What I have tried so far is adding WHERE clauses with the UserId to the AdminUser part of the select. But this does not seem to work. I hope someone can point me in the right direction.

For reference here's the DDL of the UserTest table that I want to filter out with UserId somehow:

CREATE TABLE [dbo].[UserTest] (
    [UserTestId]              INT           IDENTITY (1, 1) NOT NULL,
    [AdminTestId]             INT           NOT NULL,
    [UserId]                  INT           NOT NULL,
    [PurchaseDate]            DATETIME      NOT NULL,
    CONSTRAINT [PK_UserTest] PRIMARY KEY CLUSTERED ([UserTestId] ASC)
);

Upvotes: 3

Views: 124

Answers (2)

guildsbounty
guildsbounty

Reputation: 3361

To expand on what @RichardHansell said...

You can filter a JOIN by adding things to the 'ON' clause of the script. The ON clause does not have to be only links between the two tables, you can add other filters in as well. Like so...

SELECT   AdminTest.AdminTestId,
         AdminTest.Title,
         COUNT(AdminTestQuestion.AdminTestQuestionId) Questions,
         AdminTest.Price,
         UserTest.PurchaseDate
FROM     AdminTest 
LEFT OUTER JOIN UserTest
ON       AdminTest.AdminTestId = UserTest.AdminTestId 
AND      UserTest.UserId = @FilteredUserId
JOIN AdminTestQuestion
ON       AdminTest.AdminTestId = AdminTestQuestion.AdminTestId
GROUP BY AdminTest.AdminTestId, AdminTest.Title, UserTest.UserId

Upvotes: 5

Tab Alleman
Tab Alleman

Reputation: 31775

If you put the parameter test in the ON clause of the Left Outer Join, you should get the results you're after:

...
LEFT OUTER JOIN UserTest
ON       AdminTest.AdminTestId = UserTest.AdminTestId
AND      UserTest.UserId = @UserId
...

Upvotes: 2

Related Questions