SQL 查询解释

SQL query interpretation

我已经给出了家庭作业来解释以下查询:

select
    date,
    country,
    count(*) as `result`
from (
    select
        user_id,
        event_date as date,
        any_value(geo.country) as country
    from `xxxx-android.events.all`
    where event_date between current_date - interval 2 day and current_date - interval 1 day
    group by user_id, date
)
group by date, country
order by date, count(*) desc

并且我尝试使用以下代码在 MySql 中使用此 online editor 复制它:

CREATE TABLE events (
  user_id INTEGER,
  event_date DATE,
  country TEXT -- NOT NULL
);
-- insert some values
INSERT INTO events VALUES (1, '2022-1-9', 'USA');
INSERT INTO events VALUES (1, '2022-1-9', 'USA');
INSERT INTO events VALUES (1, '2022-1-10', 'USA');
INSERT INTO events VALUES (2, '2022-1-9', 'UK');
INSERT INTO events VALUES (2, '2022-1-10', 'UK');
INSERT INTO events VALUES (2, '2022-1-9', 'UK');
INSERT INTO events VALUES (3, '2022-1-9', 'USA');
INSERT INTO events VALUES (3, '2022-1-10', 'USA');
INSERT INTO events VALUES (3, '2022-1-10', 'USA');
INSERT INTO events VALUES (4, '2022-1-9', 'AUT');
INSERT INTO events VALUES (4, '2022-1-10', 'AUT');
INSERT INTO events VALUES (5, '2022-1-10', 'AUT');
INSERT INTO events VALUES (5, '2022-1-10', 'AUT');
INSERT INTO events VALUES (6, '2022-1-9', NULL);
INSERT INTO events VALUES (6, '2022-1-9', NULL);

-- fetch some values
-- SELECT * FROM events;

-- select curdate() - interval 2 day;
-- select curdate() - interval 1 day;


select
    event_date,
    country,
    count(*) as 'result'
from (

-- begin internal query
select
    user_id as user_id,
    event_date as event_date,
    any_value(country) as country
from events
    where event_date between curdate() - interval 2 day and curdate() - interval 1 day
group by user_id, event_date
-- end begin internal query
 
) as whatever
group by event_date, country
order by event_date, count(*) desc

我得到以下 table :

Output:

event_date  country result
2022-01-09  USA       2
2022-01-09  UK        1
2022-01-09  AUT       1
2022-01-09  NULL      1
2022-01-10  USA       2
2022-01-10  AUT       2
2022-01-10  UK        1

如果用户一天输入多次,结果不应该计算在内吗?例如,id 为 1 的用户在 '2022-01-09' 中输入了 2 次,但在输出中仅计为一次。

SELECT

中的Group BY中的术语隐含

您按用户 ID 和事件分组

group by user_id, event_date

但是

user_id as user_id,
event_date as event_date,
any_value(country) as country

不会显示任何重复值,请参阅 any_value

的手册
select
    user_id as user_id,
    event_date as event_date,
    any_value(country) as country
from events
    where event_date between curdate() - interval 2 day and curdate() - interval 1 day
group by user_id, event_date
user_id | event_date | country
------: | :--------- | :------
      1 | 2022-01-09 | USA    
      1 | 2022-01-10 | USA    
      2 | 2022-01-09 | UK     
      2 | 2022-01-10 | UK     
      3 | 2022-01-09 | USA    
      3 | 2022-01-10 | USA    
      4 | 2022-01-09 | AUT    
      4 | 2022-01-10 | AUT    
      5 | 2022-01-10 | AUT    
      6 | 2022-01-09 | null   

db<>fiddle here

这就是 2022 年 9 月 9 日的计数仅为 2 而不是 3 的原因