无法按正确顺序打印名称,利润总额也不正确
can't print the names in the right order and the sum of profit is wrong as well
CREATE TABLE seller_details
(
seller_id integer PRIMARY KEY,
seller_name text
);
CREATE TABLE sales_data
(
seller_id integer,
quantity integer,
price_per_item integer
);
INSERT INTO seller_details VALUES (1, 'sam');
INSERT INTO seller_details VALUES (2, 'jack');
INSERT INTO seller_details VALUES (3, 'john');
INSERT INTO sales_data VALUES (1, 4, 100);
INSERT INTO sales_data VALUES (1, 5, 200);
INSERT INTO sales_data VALUES (1, 4, 300);
INSERT INTO sales_data VALUES (2, 2, 500);
INSERT INTO sales_data VALUES (2, 9, 300);
INSERT INTO sales_data VALUES (3, 1, 500);
COMMIT;
SELECT * FROM seller_details;
SELECT * FROM sales_data;
SELECT
seller_name,
(quantity * price_per_item) AS total_sales
FROM
seller_details sd, sales_data sda
GROUP BY
seller_name
ORDER BY
total_sales DESC
您似乎在聚合中缺少 (quantity*price_per_item) as total_sales
中的 sum
,但是您真正的问题是如何在表上进行连接。您的查询正在创建交叉连接(见下文)而不是内部连接,即您正在生成更多记录而不是基于匹配的卖家 ID。我建议以后指定连接类型
下面的查询进行了这些调整
select seller_name, SUM(quantity*price_per_item) as total_sales
from seller_details sd
inner join sales_data sda ON sd.seller_id = sda.seller_id
group by seller_name
order by total_sales desc
seller_name
total_sales
jack
3700
sam
2600
john
500
我在下面提供了一个交叉连接结果演示,供您仔细阅读。求和后,这些会给您带来比预期更大的 total_sales。
select seller_name, quantity*price_per_item as total_sales
from seller_details sd,sales_data sda
order by seller_name
seller_name
total_sales
jack
400
jack
1000
jack
1200
jack
1000
jack
2700
jack
500
john
400
john
1000
john
1200
john
1000
john
2700
john
500
sam
400
sam
1000
sam
1200
sam
1000
sam
2700
sam
500
让我知道这是否适合你。
CREATE TABLE seller_details
(
seller_id integer PRIMARY KEY,
seller_name text
);
CREATE TABLE sales_data
(
seller_id integer,
quantity integer,
price_per_item integer
);
INSERT INTO seller_details VALUES (1, 'sam');
INSERT INTO seller_details VALUES (2, 'jack');
INSERT INTO seller_details VALUES (3, 'john');
INSERT INTO sales_data VALUES (1, 4, 100);
INSERT INTO sales_data VALUES (1, 5, 200);
INSERT INTO sales_data VALUES (1, 4, 300);
INSERT INTO sales_data VALUES (2, 2, 500);
INSERT INTO sales_data VALUES (2, 9, 300);
INSERT INTO sales_data VALUES (3, 1, 500);
COMMIT;
SELECT * FROM seller_details;
SELECT * FROM sales_data;
SELECT
seller_name,
(quantity * price_per_item) AS total_sales
FROM
seller_details sd, sales_data sda
GROUP BY
seller_name
ORDER BY
total_sales DESC
您似乎在聚合中缺少 (quantity*price_per_item) as total_sales
中的 sum
,但是您真正的问题是如何在表上进行连接。您的查询正在创建交叉连接(见下文)而不是内部连接,即您正在生成更多记录而不是基于匹配的卖家 ID。我建议以后指定连接类型
下面的查询进行了这些调整
select seller_name, SUM(quantity*price_per_item) as total_sales
from seller_details sd
inner join sales_data sda ON sd.seller_id = sda.seller_id
group by seller_name
order by total_sales desc
seller_name | total_sales |
---|---|
jack | 3700 |
sam | 2600 |
john | 500 |
我在下面提供了一个交叉连接结果演示,供您仔细阅读。求和后,这些会给您带来比预期更大的 total_sales。
select seller_name, quantity*price_per_item as total_sales
from seller_details sd,sales_data sda
order by seller_name
seller_name | total_sales |
---|---|
jack | 400 |
jack | 1000 |
jack | 1200 |
jack | 1000 |
jack | 2700 |
jack | 500 |
john | 400 |
john | 1000 |
john | 1200 |
john | 1000 |
john | 2700 |
john | 500 |
sam | 400 |
sam | 1000 |
sam | 1200 |
sam | 1000 |
sam | 2700 |
sam | 500 |
让我知道这是否适合你。