Answers Questions . Answers Questions ( )
- :
SELECT TOP 10
u.Id AS "UserId",
u.CreationDate AS "Member since",
ISNULL((SELECT AVG(Score) FROM Answers WHERE OwnerUserId = u.Id), 0)
AS "Average Question Rating",
(SELECT COUNT(*) FROM Answers WHERE OwnerUserId = u.Id)
AS "N. of Questions posted by the agent",
ISNULL((SELECT AVG(Score) FROM Questions WHERE OwnerUserId = u.Id), 0)
AS "Average Answer Rating",
(SELECT COUNT(*) FROM Questions WHERE OwnerUserId = u.Id)
AS "N. of Answers posted by the agent"
FROM Users u
, :
SELECT TOP 10
u.Id AS "UserId",
u.CreationDate AS "Member since",
ISNULL(q.a, 0) AS "Average Question Rating",
ISNULL(q.c, 0) AS "N. of Questions posted by the agent",
ISNULL(a.a, 0) AS "Average Answer Rating",
ISNULL(a.c, 0) AS "N. of Answers posted by the agent"
FROM Users u
LEFT OUTER JOIN (SELECT OwnerUserId, AVG(Score) a, COUNT(*) c
FROM Questions GROUP BY OwnerUserId) q
ON q.OwnerUserId = u.Id
LEFT OUTER JOIN (SELECT OwnerUserId, AVG(Score) a, COUNT(*) c
FROM Answers GROUP BY OwnerUserId) a
ON a.OwnerUserId = u.Id
SQL Server , , . , SQL Server. .