[英]Calculating average price of items purchased by customers
I have three tables: customer, order and line items. 我有三个表:客户,订单和订单项。 They are set up as follows:
它们的设置如下:
CREATE TABLE cust_account(
cust_id DECIMAL(10) NOT NULL,
first VARCHAR(30),
last VARCHAR(30),
address VARCHAR(50),
PRIMARY KEY (cust_id));
CREATE TABLE orders(
order_num DECIMAL(10) NOT NULL,
cust_id DECIMAL(10) NOT NULL,
order_date DATE,
PRIMARY KEY (order_num));
CREATE TABLE lines(
order_num DECIMAL(10) NOT NULL,
line_id DECIMAL(10) NOT NULL,
item_num DECIMAL(10) NOT NULL,
price DECIMAL(10),
PRIMARY KEY (order_id, line_id),
FOREIGN KEY (item_id) REFERENCES products);
Using Oracle, I need to write a query that presents the average item price for for those customers that made more than 5 or more purchases. 使用Oracle,我需要编写一个查询,为那些购买次数超过5次或以上的客户提供平均商品价格。 This is what I've been working with:
这就是我一直在与的工作:
SELECT DISTINCT cust_account.cust_id,cust_account.first, cust_account.last, lines.AVG(price) AS average_price
FROM cust_account
JOIN orders
ON cust_account.cust_id = orders.cust_id
JOIN lines
ON lines.order_num = orders.order_num
WHERE lines.item_num IN (SELECT lines.item_num
FROM lines
JOIN orders
ON lines.order_num = orders.order_num
GROUP BY lines.order_num
HAVING COUNT(DISTINCT orders.cust_id) >= 5
);
INNER JOIN
all your tables together INNER JOIN
所有桌子 GROUP BY
customer and compute the average price of each customer's lines GROUP BY
并计算每个客户线的平均价格 HAVING
clause to limit the results to groups having 5 or more purchases HAVING
子句将结果限制为购买5次或以上的组 Since this smells like homework, I'll post the full answer after a long pause... 由于这听起来像是作业,所以我会在长时间的停顿后发布完整答案...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
SELECT ca.first, ca.last, avg(l.price) avg_price
FROM cust_account ca
INNER JOIN orders o ON o.cust_id = ca.cust_id
INNER JOIN lines l ON l.order_num = o.order_number
GROUP BY ca.first, ca.last
HAVING COUNT(distinct l.line_id) >=5
-- OR, maybe your requirement is ...
-- HAVING COUNT(distinct o.order_num) >= 5
-- ... the question was a bit unclear on this point
I think this is it. 我觉得这就是。 I don't think it will work right away (I know nothing about oracle) but I think you will get the idea:
我认为它不会立即起作用(我对oracle一无所知),但我认为您会明白的:
SELECT orders.cust_id,
AVG(lines.price) AS average_price
FROM lines
JOIN orders ON orders.order_num = orders.order_num
WHERE orders.cust_id IN (SELECT orders.cust_id
FROM orders
GROUP BY orders.cust_id
HAVING COUNT(*) >= 5)
GROUP BY orders.cust_id;
Subquery selects customers that have more than 5 orders. 子查询选择具有5个以上订单的客户。 And main query just gets all lines from all orders made by this customers.
而主查询只是从该客户的所有订单中获取所有行。
I guess you can eliminate subquery by using HAVING DISTINCT ...
. 我想您可以使用
HAVING DISTINCT ...
消除子查询。 Anyways, one with subquery should work just fine. 无论如何,带有子查询的应该可以正常工作。
something like this 像这样的东西
SELECT orders.cust_id,
AVG(lines.price) AS average_price
JOIN orders ON orders.order_num = orders.order_num
GROUP BY orders.cust_id
HAVING COUNT(DISTINCT orders.id) >= 5;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.