Question
Write a query that will return sales details of all customers and products. The query should return all customers, even customers without invoices and also
Write a query that will return sales details of all customers and products. The query should return all customers, even customers without invoices and also all products, even those products that were not sold. Print "N/A" for a null customer or product name, and 0 for a null quantity.For each row return customer name, product name, and the' quantity of the product sold. Order the result ascending by customer id, product id and invoice item id.
Tables:
Customer
idcustomer_namecity_idcustomer_addresscontact_personemailphone
Product
idskuproduct_nameproduct_descriptioncurrent_pricequantity_in_stock
Invoice
idinvoice_numbercustomer_iduser_account_idtotal_pricetime_issuedtime_duetime_paidtime_canceledtime_refunded
Invoice_Item
idinvoice_idproduct_idquantitypriceline_total_price
I have tried this, but it didn't work:
SELECT
COALESCE(c.name, 'N/A') AS customer_name,
COALESCE(p.name, 'N/A') AS product_name,
COALESCE(ii.quantity, 0) AS quantity
FROM
customer c
CROSS JOIN
product p
LEFT JOIN
invoice i ON c.id = i.customer_id
LEFT JOIN
invoice_item ii ON i.id = ii.invoice_id AND p.id = ii.product_id
ORDER BY
c.id, p.id, ii.id;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Sure Lets break down the problem and formulate an appropriate query step by step The first thing we ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started