我正在尝试获取一个取消了所有订单的客户,但我无法做到。

huangapple go评论118阅读模式
英文:

I am trying to get a customer who has cancelled all orders , I am not able to do so

问题

Here is the translated SQL query:

SELECT
   oc.customer_id
  ,concat(oc.customer_fname,' ',oc.customer_lname) as Full_name
  ,oc.customer_email
  ,oc.customer_phone
  ,a.country
FROM online_customer oc
JOIN address a ON oc.address_id = a.address_id
WHERE oc.customer_id = (
  SELECT
    oh.customer_id
 FROM order_header oh
 WHERE oh.order_status = 'Cancelled'
)
GROUP BY oc.customer_id, concat(oc.customer_fname,' ', oc.customer_lname), oc.customer_email, oc.customer_phone, a.country;

The SQL query retrieves customer information for customers who have all their orders with an "Order_status" of "Cancelled" in the "order_header" table.

英文:
SELECT
   oc.customer_id
  ,concat(oc.customer_fname,' ',oc.customer_lname) as Full_name
  ,oc.customer_email
  ,oc.customer_phone
  ,a.country
FROM online_customer oc
JOIN address a ON oc.address_id = a.address_id
WHERE oc.customer_id = (
  SELECT
    oh.customer_id
 FROM order_header oh
 WHERE oh.order_status = 'Cancelled'
)
GROUP BY oc.customer_id, concat(oc.customer_fname,' ', oc.customer_lname), oc.customer_email, oc.customer_phone, a.country;

There is a column in order_header table called Order_status which has values "Shipped" ,"In process" and "Cancelled". Now I want a customer_id who has all orders as "Cancelled" in order_status. I am not able to get the logic right

答案1

得分: 1

以下是您要翻译的代码部分:

select oc.customer_id,concat(oc.customer_fname,' ',oc.customer_lname) as Full_name,oc.customer_email,oc.customer_phone,
a.country
from online_customer oc join address a on oc.address_id = a.address_id
where oc.customer_id=(select oh.customer_id
from order_header oh 
group by customer_id 
having count(distinct order_status) = 1 and max(order_status) = 'Cancelled')
group by oc.customer_id,concat(oc.customer_fname,' ',oc.customer_lname),oc.customer_email,oc.customer_phone,a.country;
英文:

You can have this logic by grouping the orders table by the customer_id then checking it has only one distinct order_status, and thats the single status (using max/min) is 'Canceled'

select oc.customer_id,concat(oc.customer_fname,' ',oc.customer_lname) as Full_name,oc.customer_email,oc.customer_phone,
a.country
from online_customer oc join address a on oc.address_id = a.address_id
where oc.customer_id=(select oh.customer_id
from order_header oh 
group by customer_id 
having count(distinct order_status) = 1 and max(order_status) = 'Cancelled')
group by oc.customer_id,concat(oc.customer_fname,' ',oc.customer_lname),oc.customer_email,oc.customer_phone,a.country;

huangapple
  • 本文由 发表于 2023年6月27日 19:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定