列出没有销售的客户和产品

时间:2020-07-10 23:40:17

标签: sql union

需要显示以下使用UNION返回的列(3):

  • 所有没有发票的客户
  • 所有未售出的产品
  1. 类别:这与“客户”或“产品”有关吗?打印“客户或产品”
  2. ID:Customer.id(category =“ customer”)或product.id(category =“ product”)
  3. 名称:customer.customer_name(category =“ customer”)或product.product_name(category =“ product”)

表格:

客户

  • id
  • customer_name
  • city_id
  • customer_address
  • contact_person
  • 电子邮件
  • 电话

产品

  • id
  • sku
  • 产品名称
  • product_description
  • current_price
  • quantity_in_stock

发票

  • id
  • 发票编号
  • customer_id
  • user_account_id
  • 总价
  • 已发行时间
  • time_due
  • time_paid
  • time_canceled
  • time_refunded

发票项目

  • id
  • invoice_id
  • product_id
  • 数量
  • 价格
  • line_total_price

到目前为止有以下内容:

SELECT 
  category,
  CASE
      WHEN category = 'customer' THEN c.id
      WHEN category = 'product' THEN p.id
  END AS 'id',
  CASE
    WHEN category = 'customer' THEN c.customer_name
    WHEN category = 'product' THEN p.product_name
  END AS 'name'
FROM 
  (
    SELECT
        CASE
          WHEN c.id = c.id THEN 'customer'
          WHEN p.id = p.id THEN 'product'
        END as 'category'
    FROM
        customer as c
    LEFT Join -- Left join to show all customers even those with & without invoices
        invoice as i
    ON c.id = i.customer_id
    AND i.id IS NULL -- Gives me all customers who do not have an invoice
    JOIN invoice_item as ii
    ON i.id = ii.invoice_id
    Join product p
    ON p.id = ii.product_id
  ) tb1

UNION ALL

SELECT 
  category,
  CASE
      WHEN category = 'customer' THEN c.id
      WHEN category = 'product' THEN p.id
  END AS 'id',
  CASE
    WHEN category = 'customer' THEN c.customer_name
    WHEN category = 'product' THEN p.product_name
  END AS 'name'
FROM
  (
    SELECT 
      CASE
        WHEN c.id = c.id THEN 'customer'
        WHEN p.id = p.id THEN 'product'
      END as 'category'
    FROM
        product as p
    LEFT JOIN -- Left join to show all products even those that sold and not sold
        invoice_item as ii
    ON p.id = ii.product_id
    AND ii.invoice_id IS NULL -- Gives me products that didnt sell
    JOIN invoice as i
    ON ii.invoice_id = i.id
  ) tb2

欢迎提出任何建议,因为我一直试图找出如何将类别显示为“产品”或“客户”。预先感谢!

4 个答案:

答案 0 :(得分:0)

考虑到您的数据模型和要求,您应该尝试以下SQL。您可以轻松地对两个SQL执行UNION

第一个SQL返回此列表->所有没有发票的客户

select 'customer' as category, c.id as id, customer_name as name
     from customer c
     left join invoice i on c.id = i.customer_id
     where i.id is null 

第二条SQL返回此列表->所有未售出的产品

select 'product' as category, p.id as id, product_name as name
        from product p
        left join invoice_item ii on p.id = ii.product_id
        where ii.id is null;

答案 1 :(得分:0)

嗯,它已经超过 6 个月了,但答案仍然是:

select 'customer' as category, c.id as id, customer_name as name
from customer c
left join invoice i on c.id = i.customer_id
where i.id is null 

union

select 'product' as category, p.id as id, product_name as name
from product p
left join invoice_item ii on p.id = ii.product_id
where ii.id is null;

答案 2 :(得分:0)

实际问题是所有客户的详细信息,即使是没有发票的,所有产品,甚至是未售出的产品。

SELECT    c.customer_name,
          p.product_name,
          Coalesce((ii.quantity), 0) AS quantity
FROM      customer c
LEFT JOIN invoice i
on        c.id = i.customer_id
LEFT JOIN invoice_item ii
ON        ii.invoice_id = i.id
LEFT JOIN product p
ON        ii.product_id = p.id
ORDER BY  c.customerid,
          p.product_id,
          ii.id
UNION
SELECT 'N/A',
       p.product_name,
       0
FROM   products p 
ORDER BY p.id

答案 3 :(得分:-1)

SELECT 'customer' as category,id,customer_name FROM customer 
WHERE id NOT IN(SELECT customer_id FROM invoice) 
UNION 
SELECT 'product' as category,id,product_name FROM product 
WHERE id NOT IN(SELECT product_id FROM invoice_item);