如何从连接到左外部连接的 table 中获取计数?

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

我有三个 table。 table 个测试 (AdminTest),table 个属于用户的测试 (UserTest) 和 table 个属于每个用户测试 (UserTestQuestion) 的问题:

管理测试

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

用户测试

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));

用户测试问题

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)
);

我创建这个 SQL 是为了从 AdminTest 和 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

但现在我被困住了,有两件事我需要帮助。

有人可以就如何将此功能添加到我的 SQL 给我建议吗?

这是我需要的示例:

AdminTestId   UserTestStartedData  AnsweredCount

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

加入UserTestQuestiontable并使用Conditional Aggregate只在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

此外,如果您只想加入那些 UserTest table 值为 UserId=25 那么 left join 将被转换为 Inner join

更新:

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