对内连接和左连接感到困惑- 在 sql 中构建报告
Confused between inner join and left join- Building report in sql
表格:
问题: 使用这些 table,生成一个 table 显示 2020 年 3 月的每一天(包括周末)和客户(每一天在他们的入职日期或之后),有多少用户在产品上处于活跃状态(查看建筑物或创建注释),查看了多少建筑物以及总共创建了多少注释。请注意,即使客户在该日期不活跃,也应该出现。
期望输出:
我的部分代码:
- Notes_report
select date(n.created_at) "created_at", c.customer_name, count(n.user_id) "total_notes",count(distinct n.user_id) "active_user"
from customer c left join notes n on c.customer_id=n.customer_id
group by customer_name, date(created_at);
输出:
- Views_report
select date(v.created_at) "created_at", c.customer_name, count(v.user_id) "total_views",count(distinct v.user_id) "active_user"
from customer c left join building_views v on c.customer_id=v.customer_id
group by customer_name, date(created_at);
输出:
3) 日期和 customer_name:
select d.date, c.customer_name
from date_spine d left join customer c on d.date>=c.onboarding_date
where d.date between '2020-03-01' and '2020-03-31';
输出:
我卡在哪里:
- 如何将我的第一个代码与第二个代码组合在一起,然后将结果 table 与我的第三个代码组合起来以获得所需的输出。如果这个方法不好。请提出更好的方法。
请写代码。
添加 LEFT JOIN
子查询,从每个其他表中获取您想要的计数。
select d.date, c.customer_name, IFNULL(n.total_notes, 0) total_notes, IFNULL(n.active_user, 0) active_user, IFNULL(v.total_views, 0) total_views
from date_spine d
left join customer c on d.date>=c.onboarding_date
left join (
select date(n.created_at) date, customer_id, count(*) total_notes,count(distinct n.user_id) active_user
from notes
group by customer_id, date
) AS n ON n.customer_id = c.customer_id AND n.date = d.date
LEFT JOIN (
select date(v.created_at) date, customer_id, count(*) total_views
from building_views v
group by customer_id, date
) AS v ON v.customer_id = c.customer_id AND v.date = d.date
where d.date between '2020-03-01' and '2020-03-31'
子查询不需要与 customer
连接,因为只有主查询需要这样做才能获取名称。子查询只使用客户 ID。
表格:
问题: 使用这些 table,生成一个 table 显示 2020 年 3 月的每一天(包括周末)和客户(每一天在他们的入职日期或之后),有多少用户在产品上处于活跃状态(查看建筑物或创建注释),查看了多少建筑物以及总共创建了多少注释。请注意,即使客户在该日期不活跃,也应该出现。
期望输出:
我的部分代码:
- Notes_report
select date(n.created_at) "created_at", c.customer_name, count(n.user_id) "total_notes",count(distinct n.user_id) "active_user"
from customer c left join notes n on c.customer_id=n.customer_id
group by customer_name, date(created_at);
输出:
- Views_report
select date(v.created_at) "created_at", c.customer_name, count(v.user_id) "total_views",count(distinct v.user_id) "active_user"
from customer c left join building_views v on c.customer_id=v.customer_id
group by customer_name, date(created_at);
输出:
3) 日期和 customer_name:
select d.date, c.customer_name
from date_spine d left join customer c on d.date>=c.onboarding_date
where d.date between '2020-03-01' and '2020-03-31';
输出:
我卡在哪里:
- 如何将我的第一个代码与第二个代码组合在一起,然后将结果 table 与我的第三个代码组合起来以获得所需的输出。如果这个方法不好。请提出更好的方法。 请写代码。
添加 LEFT JOIN
子查询,从每个其他表中获取您想要的计数。
select d.date, c.customer_name, IFNULL(n.total_notes, 0) total_notes, IFNULL(n.active_user, 0) active_user, IFNULL(v.total_views, 0) total_views
from date_spine d
left join customer c on d.date>=c.onboarding_date
left join (
select date(n.created_at) date, customer_id, count(*) total_notes,count(distinct n.user_id) active_user
from notes
group by customer_id, date
) AS n ON n.customer_id = c.customer_id AND n.date = d.date
LEFT JOIN (
select date(v.created_at) date, customer_id, count(*) total_views
from building_views v
group by customer_id, date
) AS v ON v.customer_id = c.customer_id AND v.date = d.date
where d.date between '2020-03-01' and '2020-03-31'
子查询不需要与 customer
连接,因为只有主查询需要这样做才能获取名称。子查询只使用客户 ID。