简体   繁体   中英

How can I get a count from a table joined to a Left Outer Join?

I have three tables. A table of tests (AdminTest), a table of tests that belong to a user (UserTest) and a table of questions that belongs to each of the user tests (UserTestQuestion):

AdminTest

CREATE TABLE [dbo].[AdminTest] (
    [AdminTestId]  INT            IDENTITY (1, 1) NOT NULL,
    [Title]        NVARCHAR (100) NOT NULL
    CONSTRAINT [PK_AdminTest] PRIMARY KEY CLUSTERED ([AdminTestId] ASC));

UserTest

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

UserTestQuestion

CREATE TABLE [dbo].[UserTestQuestion] (
    [UserTestQuestionId]  INT              IDENTITY (1, 1) NOT NULL,
    [UserTestId]          INT              NOT NULL,
    [Answered]            BIT              DEFAULT ((0)) NOT NULL
    CONSTRAINT [PK_UQ] PRIMARY KEY CLUSTERED ([UserTestQuestionId] ASC)
);
  • An AdminTest may or may not have a UserTest
  • A UserTest will always have UserTestQuestions

I created this SQL to get data from AdminTest and UserTest:

SELECT  userTest.StartedDate,
        temp.AdminTestId 
        -- AnsweredCount
        -- I want to get a count of the number of rows
        -- from the table UserTestQuestions that have
        -- the column 'Answered' set to 1 here.             
FROM
( SELECT AdminTest.AdminTestId
  FROM   AdminTest  
  JOIN   AdminTestQuestion ON  AdminTest.AdminTestId = AdminTestQuestion.AdminTestId     
GROUP BY 
  AdminTest.AdminTestId
) temp
LEFT OUTER JOIN UserTest ON  temp.AdminTestId = UserTest.AdminTestId
-- I want the above join to only join those UserTest tables that 
-- have a value of UserId set to for example 25

But now I am stuck and there are two things I need help with.

  • I need to be able to show only the UserTests that belong to a given UserId
  • I need to report a count of the rows in the UserTests that have Answered set to 1.

Can someone give me advice on how to add this functionality to my SQL?

Here's an example of what I need:

AdminTestId   UserTestStartedData  AnsweredCount

1             1/1/2001             25
2             2/2/2002             10
3                
4             4/4/2004             10

Join the UserTestQuestion table and use Conditional Aggregate to count only when answered = 1

SELECT userTest.StartedDate,
       temp.AdminTestId,
       Count(CASE
               WHEN UT.answered = 1 THEN 1
             END) cnt
FROM   (SELECT AdminTest.AdminTestId
        FROM   AdminTest
               JOIN AdminTestQuestion
                 ON AdminTest.AdminTestId = AdminTestQuestion.AdminTestId
        GROUP  BY AdminTest.AdminTestId) temp
       INNER JOIN UserTest
                    ON temp.AdminTestId = UserTest.AdminTestId
       INNER JOIN [UserTestQuestion] UT
               ON UserTest.UserTestId = UT.UserTest 
       Where  UserTest.UserTestId = 25

Also If you want join to only join those UserTest tables that have a value of UserId=25 then left join will be converted to Inner join

Update :

SELECT A.StartedDate,
       temp.AdminTestId,
       Count(CASE
               WHEN A.answered = 1 THEN 1
             END) cnt
FROM   (SELECT AdminTest.AdminTestId
        FROM   AdminTest
               JOIN AdminTestQuestion
                 ON AdminTest.AdminTestId = AdminTestQuestion.AdminTestId
        GROUP  BY AdminTest.AdminTestId) temp
       LEFT OUTER JOIN (SELECT userTest.StartedDate,
                               UT.answered,
                               UserTest.AdminTestId
                        FROM   UserTest
                               INNER JOIN [UserTestQuestion] UT
                                       ON UserTest.UserTestId = UT.UserTest
                        WHERE  UserTest.UserTestId = 25) A
                    ON temp.AdminTestId = A.AdminTestId 
Group by A.StartedDate,temp.AdminTestId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM