英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论