简体   繁体   English

如何使用来自两个不同表的列创建 SQL 对象?

[英]How do I create a SQL object using columns from two different tables?

I have an 'Orders' table and a 'Records' table.我有一个“订单”表和一个“记录”表。

Orders table has the following columns:订单表包含以下列:

order_id       
order_date        
seller        
order_price

Records table has the following columns:记录表具有以下列:

order_id        
record_created_at         
record_log

The record_log shows whether the order was 'approved' or 'declined'. record_log 显示订单是“批准”还是“拒绝”。

I'm trying to create a new table with the last two columns showing 'order_approved_rate_sum', which is the rate of number of orders approved out of number of orders placed.我正在尝试创建一个新表,其中最后两列显示“order_approved_rate_sum”,这是已下订单数量中批准的订单数量的比率。

MY current query is:我当前的查询是:

SELECT seller
     , o.order_id
     , COUNT(DISTINCT o.order_id) AS approved
  FROM records r
     , orders o
 WHERE l.order_id IN (
SELECT order_id
    FROM records
    WHERE record_log = 'approved')
 GROUP 
    BY merchant_name
;

I'm trying to start by getting the total number of approved orders but this query fetches the wrong numbers.我试图从获取已批准订单的总数开始,但此查询获取了错误的数字。

What am I doing wrong?我究竟做错了什么?

try the below -试试下面的 -

SELECT seller, COUNT(DISTINCT o.order_id) AS approved 
FROM records r inner join orders o 
on r.order_id=o.order_id
where record_log = 'approved'
GROUP BY seller

First, if you insist on doing the query this way instead of using INNER JOIN, you need to remember to include o.order_id=r.order_id in your WHERE clause.首先,如果您坚持以这种方式进行查询而不是使用 INNER JOIN,您需要记住在 WHERE 子句中包含 o.order_id=r.order_id。 Otherwise, you should use the INNER JOIN syntax.否则,您应该使用 INNER JOIN 语法。

Second, your GROUP BY clause should include all fields from the SELECT clause that are not part of an aggregate.其次,您的 GROUP BY 子句应包括 SELECT 子句中不属于聚合的所有字段。 I am going to assume when you say merchant_name you meant seller.我假设当您说 Merchant_name 时,您的意思是卖家。 So in your query the GROUP BY should be GROUP BY seller,o.order_id所以在你的查询中 GROUP BY 应该是 GROUP BY Seller,o.order_id

Third,having the order_id as both a field and an aggregate is going to give you the count of each order_id individually, which is most likely going to be 1 each time.第三,将 order_id 作为字段和聚合将分别为您提供每个 order_id 的计数,每次很可能为 1。

Something close to what you were doing that should work is the following:接近你正在做的事情应该工作如下:

SELECT o.seller AS merchant,
      COUNT(DISTINCT o.order_is) AS approved
FROM records r, orders o
WHERE r.order_id = o.order_id
AND
o.order_id IN (SELECT order_id FROM records WHERE record_log='approved')
GROUP BY o.seller

However, it would be better to do:但是,最好这样做:

SELECT o.seller AS merchant,
       COUNT(DISTINCT o.order_id) AS approved
FROM records r INNER JOIN orders o
ON r.order_id=o.order_id
WHERE r.record_log = 'approved'
GROUP BY o.seller;

If you're trying to get the approval rate by seller, then your query could look like this...如果您想获得卖家的认可率,那么您的查询可能如下所示...

    with cte1 as
    (select o.seller, count(distinct o.order_id) as approved_orders
    from orders o
    join records r
        on r.order_id = o.order_id
    where r.record_log = 'approved'
    group by o.seller
    ), 
    cte2 as
    (
    select o.seller, count(distinct o.order_id) as all_orders
    from orders o
    join records r
        on r.order_id = o.order_id
    group by o.seller
    )
    select cte2.seller, coalesce((cte1.approved_orders / cte2.all_orders),0) as approval_rate
    from cte2
    left join cte1
        on cte1.seller = cte2.seller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM