Trying to make a new table by pulling data from two tables and keep getting 'Error: Every derived table must have its own alias' on this query
Trying to make a new table by pulling data from two tables and keep getting 'Error: Every derived table must have its own alias' on this query
我有一个 'Orders' table 和一个 'Records' table。
订单 table 有以下列:
order_id order_date seller order_price
记录 table 有以下列:
order_id record_created_at record_log
我正在尝试提取和编译以下数据列表,但我不断收到错误消息:
order_week
seller
total_num_orders
under100_count --this is the number of orders that were < 0
over100_count --this is the number of order that >= 0
approved --this is the number of orders that were approved by the payment platform
这是我的查询:
SELECT order_week, seller, total_num_orders, under100_count, over100_count, approved
FROM (
SELECT
EXTRACT(WEEK FROM order_created_at) AS order_week,
merchant_name AS seller,
COUNT(merchant_name) AS total_num_orders,
SUM(DISTINCT total_order_price < 100) AS under100_count,
SUM(DISTINCT total_order_price >= 100) AS over100_count
FROM orders o
GROUP BY order_week, seller)
INNER JOIN (
SELECT
COUNT(DISTINCT o.order_id) AS approved
FROM records r
WHERE record_log = 'order approved'
GROUP BY order_id)
ON l.order_id = o.order_id;
我做错了什么?
join
中的子查询需要一个别名。它还需要 return order_id
列,以便可以加入。
inner join ( select order_id, ... from records ... group by order_id) r --> here
on l.order_id = o.order_id
我实际上会将您的查询写成:
select
extract(week from o.order_created_at) as order_week,
o.merchant_name as seller,
count(*) as total_num_orders,
sum(o.total_order_price < 100) as under100_count,
sum(o.total_order_price >= 100) as over100_count,
sum(r.approved) approved
from orders o
inner join (
select order_id, count(*) approved
from records r
where record_log = 'order approved'
group by order_id
) r on r.order_id = o.order_id;
group by order_week, seller, approved
理由:
您不需要,也不需要,distinct
在这里的聚合函数中;它效率低下,甚至可能产生错误的结果
count(*)
效率更高 count(<expression>)
- 所以,使用它,除非你知道为什么要这样做
我去掉了一层不必要的嵌套
如果有没有记录的订单,您可能需要 left join
。
我有一个 'Orders' table 和一个 'Records' table。
订单 table 有以下列:
order_id order_date seller order_price
记录 table 有以下列:
order_id record_created_at record_log
我正在尝试提取和编译以下数据列表,但我不断收到错误消息:
order_week
seller
total_num_orders
under100_count --this is the number of orders that were < 0
over100_count --this is the number of order that >= 0
approved --this is the number of orders that were approved by the payment platform
这是我的查询:
SELECT order_week, seller, total_num_orders, under100_count, over100_count, approved
FROM (
SELECT
EXTRACT(WEEK FROM order_created_at) AS order_week,
merchant_name AS seller,
COUNT(merchant_name) AS total_num_orders,
SUM(DISTINCT total_order_price < 100) AS under100_count,
SUM(DISTINCT total_order_price >= 100) AS over100_count
FROM orders o
GROUP BY order_week, seller)
INNER JOIN (
SELECT
COUNT(DISTINCT o.order_id) AS approved
FROM records r
WHERE record_log = 'order approved'
GROUP BY order_id)
ON l.order_id = o.order_id;
我做错了什么?
join
中的子查询需要一个别名。它还需要 return order_id
列,以便可以加入。
inner join ( select order_id, ... from records ... group by order_id) r --> here
on l.order_id = o.order_id
我实际上会将您的查询写成:
select
extract(week from o.order_created_at) as order_week,
o.merchant_name as seller,
count(*) as total_num_orders,
sum(o.total_order_price < 100) as under100_count,
sum(o.total_order_price >= 100) as over100_count,
sum(r.approved) approved
from orders o
inner join (
select order_id, count(*) approved
from records r
where record_log = 'order approved'
group by order_id
) r on r.order_id = o.order_id;
group by order_week, seller, approved
理由:
您不需要,也不需要,
distinct
在这里的聚合函数中;它效率低下,甚至可能产生错误的结果count(*)
效率更高count(<expression>)
- 所以,使用它,除非你知道为什么要这样做我去掉了一层不必要的嵌套
如果有没有记录的订单,您可能需要 left join
。