-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery07.sql
More file actions
30 lines (29 loc) · 998 Bytes
/
query07.sql
File metadata and controls
30 lines (29 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
★ Para cada usuario, muestra el número total de publicaciones (Posts), comentarios
(Comments) y medallas (Badges) que han realizado. Utiliza uniones (JOIN) para combinar
la información de las tablas Posts, Comments y Badges por usuario. Presenta los
resultados en una tabla mostrando el DisplayName del usuario junto con el total de
publicaciones, comentarios y medallas.
*/
-- * 07
SELECT
u.DisplayName,
COALESCE(p.TotalPosts, 0) AS TotalPosts,
COALESCE(c.TotalComments, 0) AS TotalComments,
COALESCE(b.TotalBadges, 0) AS TotalBadges
FROM
Users u
LEFT JOIN
(SELECT OwnerUserId, COUNT(DISTINCT Id) AS TotalPosts
FROM Posts
GROUP BY OwnerUserId) p ON u.Id = p.OwnerUserId
LEFT JOIN
(SELECT UserId, COUNT(DISTINCT Id) AS TotalComments
FROM Comments
GROUP BY UserId) c ON u.Id = c.UserId
LEFT JOIN
(SELECT UserId, COUNT(DISTINCT Id) AS TotalBadges
FROM Badges
GROUP BY UserId) b ON u.Id = b.UserId
ORDER BY
u.DisplayName;