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

Posted

技术标签:

【中文标题】列出没有销售的客户和产品【英文标题】:List Customer And Product Without Sale 【发布时间】:2020-10-31 17:59:15 【问题描述】:

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

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

表格:

客户

身份证 客户名称 city_id 客户地址 contact_person 电子邮件 电话

产品

身份证 sku 产品名称 product_description 当前价格 quantity_in_stock

发票

身份证 invoice_number customer_id user_account_id total_price time_issued time_due time_paid time_canceled time_refunded

Invoice_Item

身份证 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

欢迎任何建议,因为我一直在试图弄清楚如何将类别显示为“产品”或“客户”。提前致谢!

【问题讨论】:

这能回答你的问题吗? Customer product sale query incorrect result in sql server 2016 【参考方案1】:

考虑到您的数据模型和要求,您应该尝试以下 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;

【讨论】:

【参考方案2】:

嗯,它已经超过 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;

【讨论】:

【参考方案3】:
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);

【讨论】:

【参考方案4】:

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

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

【讨论】:

以上是关于列出没有销售的客户和产品的主要内容,如果未能解决你的问题,请参考以下文章

CODING 渠道商招募:SaaS 产品的客户服务与销售

如何列出客户未购买的产品?

氚云CRM产品的详细介绍

Magento 1.9无法保存向上销售/相关/交叉销售产品

具有产品和库存维度的销售事实的维度建模

通过行聚合和新列创建转换表