We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
编写一个 SQL 查询,按产品 product_id 来统计每个产品的销售总量。
product_id
sales
sale_id
product
输出字段 product_id 和 total_quantity
total_quantity
CREATE TABLE sales ( sale_id INT, product_id INT, year INT, quantity INT, price INT ); INSERT INTO sales ( sale_id, product_id, year, quantity, price ) VALUES ( 1, 100, 2008, 10, 5000 ), ( 2, 100, 2009, 12, 5000 ), ( 7, 200, 2011, 15, 9000 ); CREATE TABLE product ( product_id INT, product_name VARCHAR ( 10 ) ); INSERT INTO product ( product_id, product_name ) VALUES ( 100, 'Nokia' ), ( 200, 'Apple' ), ( 300, 'Samsung' );
sales 表中就有每个产品的销量 quantity ,而且输出的字段也都在 sales 表中,所以这里 product 表没有作用。
quantity
select product_id, sum(quantity) total_quantity from sales group by product_id;
group by
sum()
select distinct product_id, sum(quantity) over(partition by product_id) total_product from sales;
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
编写一个 SQL 查询,按产品
product_id
来统计每个产品的销售总量。sales
表中的主键是sale_id
外键是product_id
product
表中的主键是product_id
输出字段
product_id
和total_quantity
分析
sales
表中就有每个产品的销量quantity
,而且输出的字段也都在sales
表中,所以这里product
表没有作用。SQL:方法一
解析
group by
对product_id
分组sum()
计算quantity
SQL:方法二
解析
product_id
进行分组sum()
计算quantity
The text was updated successfully, but these errors were encountered: