我在SQL Server数据库中有这个SQL:
SELECT E.CreatedDate AS CreatedDate,
E.ExamId AS ExamId,
E.SubjectId As SubjectId,
E.Title AS Name,
E.Text AS Text,
E.ExamTypeId AS ExamTypeId
FROM AdminTest A,
Exam E
WHERE A.ExamId = E.ExamId
AND A.TestStatusId = 3
有人可以告诉我,我可以这样做,以便显示每次考试的AdminTests数量。现在它返回:
2014-09-27 06:08:43.673 2322 1 Custom Exam 1 asfdsadfsadf 3
2013-08-22 15:16:00.000 1 1 Exam 1 sdfsdf 1
2013-08-22 15:16:00.000 1 1 Exam 1 sdfsdf 1
2013-08-22 15:16:00.000 1 1 Exam 1 sdfsdf 1
我想要的是在每个行的末尾看到两行和一列,这些数据显示为1,然后是3。
答案 0 :(得分:2)
您应该使用group by
和count
汇总功能:
SELECT E.CreatedDate AS CreatedDate,
E.ExamId AS ExamId,
E.SubjectId As SubjectId,
E.Title AS Name,
E.Text AS Text,
E.ExamTypeId AS ExamTypeId,
COUNT(*) AS TotalCount
FROM AdminTest A
JOIN Exam E ON A.ExamId = E.ExamId
WHERE A.TestStatusId = 3
GROUP BY E.CreatedDate,
E.ExamId,
E.SubjectId,
E.Title,
E.Text,
E.ExamTypeId
答案 1 :(得分:1)
我认为您正在寻找GROUP BY
条款:
SELECT
COUNT(*),
E.ExamId AS ExamId
FROM AdminTest A,
Exam E
WHERE A.ExamId = E.ExamId
AND A.TestStatusId = 3
GROUP BY ExamId;