Я считаю, что вы можете получить столбец Number_of_Users_Viewed_Profile
без объединения:
SELECT COUNT(DISTINCT user_id)
FROM dsv1069.Events
WHERE Event_Name = 'view_user_profile'
Так что это эквивалентно вашему запросу:
SELECT
COUNT(*) AS Number_of_Users,
(
SELECT COUNT(DISTINCT user_id)
FROM dsv1069.Events
WHERE Event_Name = 'View_User_Profile'
) AS Number_of_Users_Viewed_Profile
FROM dsv1069.Users
Вы можете получить ratio
столбец с CTE:
WITH cte AS (
SELECT
COUNT(*) AS Number_of_Users,
(
SELECT COUNT(DISTINCT user_id)
FROM Events
WHERE Event_Name = 'View_User_Profile'
) AS Number_of_Users_Viewed_Profile
FROM Users
)
SELECT *, ROUND(100.0 * Number_of_Users_Viewed_Profile / Number_of_Users, 1) AS ratio
FROM cte
См. демоверсию . Или без CTE:
SELECT
t1.Number_of_Users,
t2.Number_of_Users_Viewed_Profile,
ROUND(100.0 * t2.Number_of_Users_Viewed_Profile / t1.Number_of_Users, 1) AS ratio
FROM (SELECT COUNT(*) AS Number_of_Users FROM Users) AS t1
CROSS JOIN (
SELECT COUNT(DISTINCT user_id) AS Number_of_Users_Viewed_Profile
FROM Events
WHERE Event_Name = 'View_User_Profile'
) AS t2
См. Демоверсию . Результаты:
| Number_of_Users | Number_of_Users_Viewed_Profile | ratio |
| --------------- | ------------------------------ | ----- |
| 7 | 2 | 28.6 |