我怎样才能将一个 table 加入另一个并计算每天一件物品的登记数量?
How can I join one table with another and count the number of registers of an item per day?
我遇到了一个问题。我有 2 个 table:一个包含产品和仓库员工检查它们的日期 (date_checked),另一个包含销售数据,如下所示:
PRODUCTS
date_checked | product_name | category | product_id
_____________________________________________________________________
0 2021-01-01 | tv | entertainment | 100
1 2021-01-03 | laptop | business | 110
SALES
sale_date | product_name | category | product_id
_____________________________________________________________________
0 2021-01-01 | tv | entertainment | 100
1 2021-01-01 | laptop | business | 110
2 2021-01-01 | tv | entertainment | 100
3 2021-01-01 | laptop | business | 110
4 2021-01-01 | tv | entertainment | 100
5 2021-01-03 | laptop | business | 110
6 2021-01-03 | tv | entertainment | 100
7 2021-01-03 | laptop | business | 110
7 2021-01-03 | laptop | business | 110
我的目标是创建一个新的 table,其中包含产品 table 的所有数据以及 date_checked 中销售的产品数量。例如:1 月 1 日检查电视产品,当天售出 3 台电视。 1 月 3 日检查了笔记本电脑,当天售出了 3 台笔记本电脑,如您所见:
SALES_AT_CHECK_DAY
date_checked | product_name | category | product_id | sales
_____________________________________________________________________________
0 2021-01-01 | tv | entertainment | 100 | 3
1 2021-01-03 | laptop | business | 110 | 3
我知道我需要在此处使用连接,但我无法统计在给定日期售出的产品数量。你们能帮帮我吗?
非常感谢?
首先,您的数据模型似乎很差,因为您在两个表之间重复列。你应该只有 sales
中的主键,然后查找其他信息。
select p.date_checked, s.product_name, s.category, s.product_id,
count(*) as sales
from sales s join
products p
on s.product_id = p.product_id and
s.sale_date = p.date_checked
group by p.date_checked, s.product_name, s.category, s.product_id;
- 先加入数据
- Count() 将根据提供的聚合计算行数。在我们的例子中,它是整个产品列,即产品。
- 指定分组依据
中的那些列
select
products.*,
count(*) as sales
from sales
inner join products
on sales.product_id = products.product_id
and sales.sale_date = products.date_checked
group by
products.date_checked,
products.product_name,
products.category,
products.product_id
我遇到了一个问题。我有 2 个 table:一个包含产品和仓库员工检查它们的日期 (date_checked),另一个包含销售数据,如下所示:
PRODUCTS
date_checked | product_name | category | product_id
_____________________________________________________________________
0 2021-01-01 | tv | entertainment | 100
1 2021-01-03 | laptop | business | 110
SALES
sale_date | product_name | category | product_id
_____________________________________________________________________
0 2021-01-01 | tv | entertainment | 100
1 2021-01-01 | laptop | business | 110
2 2021-01-01 | tv | entertainment | 100
3 2021-01-01 | laptop | business | 110
4 2021-01-01 | tv | entertainment | 100
5 2021-01-03 | laptop | business | 110
6 2021-01-03 | tv | entertainment | 100
7 2021-01-03 | laptop | business | 110
7 2021-01-03 | laptop | business | 110
我的目标是创建一个新的 table,其中包含产品 table 的所有数据以及 date_checked 中销售的产品数量。例如:1 月 1 日检查电视产品,当天售出 3 台电视。 1 月 3 日检查了笔记本电脑,当天售出了 3 台笔记本电脑,如您所见:
SALES_AT_CHECK_DAY
date_checked | product_name | category | product_id | sales
_____________________________________________________________________________
0 2021-01-01 | tv | entertainment | 100 | 3
1 2021-01-03 | laptop | business | 110 | 3
我知道我需要在此处使用连接,但我无法统计在给定日期售出的产品数量。你们能帮帮我吗?
非常感谢?
首先,您的数据模型似乎很差,因为您在两个表之间重复列。你应该只有 sales
中的主键,然后查找其他信息。
select p.date_checked, s.product_name, s.category, s.product_id,
count(*) as sales
from sales s join
products p
on s.product_id = p.product_id and
s.sale_date = p.date_checked
group by p.date_checked, s.product_name, s.category, s.product_id;
- 先加入数据
- Count() 将根据提供的聚合计算行数。在我们的例子中,它是整个产品列,即产品。
- 指定分组依据 中的那些列
select
products.*,
count(*) as sales
from sales
inner join products
on sales.product_id = products.product_id
and sales.sale_date = products.date_checked
group by
products.date_checked,
products.product_name,
products.category,
products.product_id