SQL组由自引用表中的主类别组成

时间:2017-11-08 08:36:21

标签: sql sql-server

我需要获取按主要类别和Seller分组的总销售额列表。请注意,主要类别可能有销售(这是我目前可以想到的最好的例子)。

来源表

+--------------------------------------+
|ID   |Name        |Seller|Qty|ParentID|
+--------------------------------------+
|10   |Egg         |John  |5  |NULL    |
|10   |Egg         |Anna  |2  |NULL    |
|10-01|Egg - Small |John  |3  |10      |
|10-01|Egg - Small |Anna  |4  |10      |
|10-02|Egg - Medium|John  |2  |10      |
|10-02|Egg - Medium|Bob   |11 |10      |
|10-03|Egg - Large |Anna  |7  |10      |
+--------------------------------------+

所需的输出

+------------------+
|ID|Name|Seller|Qty|
+------------------+
|10|Egg |John  |10 | <- SUM of all sales John has made for any type of egg
|10|Egg |Anna  |13 |
|10|Egg |Bob   |11 |
+------------------+

我正在接近这个查询,但是如果有人没有在主要类别上进行销售,那么当我使用Name时,他们会收到错误的MIN(Name)

当前查询

SELECT 
    SUBSTRING(t1.ID, 1, 2) AS 'ID',
    MIN(t1.Name) AS 'Name', 
    t1.Seller, 
    SUM(t1.Qty) AS 'Qty'
FROM EggTest t1
GROUP BY 
    SUBSTRING(t1.ID, 1, 2),
    t1.Seller

当前输出

+--------------------------+
|ID|Name        |Seller|Qty|
+--------------------------+
|10|Egg         |Anna  |13 |
|10|Egg - Medium|Bob   |11 | <- Bob has not made sales on the main category
|10|Egg         |John  |10 |
+--------------------------+

编辑:看到多个答案已经提出SUBSTRING(Name, 1, 3),这对我不起作用。 Name并不总是以“蛋”开头。

更新

现在尝试此查询:

WITH report AS(
  SELECT 
    ID = CASE WHEN s.ParentID IS NOT NULL THEN s.ParentID ELSE s.ID END,
    Name = CASE WHEN s.ParentID IS NOT NULL THEN p.Name ELSE s.Name END,
    s.Seller,
    s.Qty
  FROM EggTest s
  LEFT JOIN EggTest p ON p.ID = s.ParentID
)

SELECT ID, Name, Seller, SUM(Qty) AS 'Total'
FROM report
GROUP BY ID, Name, Seller;

但我得到了这个奇怪的结果:

+--------------------+
|ID|Name|Seller|Total|
+--------------------+
|10|Egg |Anna  |24   | <- Wrong (Should be 13)
|10|Egg |Bob   |22   | <- Wrong (Should be 11)
|10|Egg |John  |15   | <- Correct(!!)
+--------------------+

report - 表中我得到了一些重复:

+------------------+
|ID|Name|Seller|Qty|
+------------------+
|10|Egg |John  |5  |
|10|Egg |Anna  |2  |
|10|Egg |John  |3  |
|10|Egg |John  |3  |
|10|Egg |Anna  |4  |
|10|Egg |Anna  |4  |
|10|Egg |John  |2  |
|10|Egg |John  |2  |
|10|Egg |Anna  |7  |
|10|Egg |Anna  |7  |
|10|Egg |Bob   |11 |
|10|Egg |Bob   |11 |
+------------------+

3 个答案:

答案 0 :(得分:1)

我会将源表名称视为[销售]

您可以使用以下

with report as(
   select ID = case when s.ParentID is not null then s.ParentID else s.ID end,
          Name= case when s.ParentID is not null then p.Name else s.Name end,
          s.Seller,
          s.Qty
   from Sales s
   left join Sales p on p.ID = s.ParentID and p.Seller = s.Seller
)
select ID,Name,Seller,sum(Qty) as Qty
from report
group by ID,Name,Seller

这是使用Distinct

demo

这里有一个demo,其中包含Seller in the left join,它会为卖方Bob提供NULL项的名称,如果您拥有正确的数据完整性,则左连接应该有效项目和类别的单独表格

回复您的上一条评论,demo如何使您的数据清晰

希望这会对你有所帮助

答案 1 :(得分:0)

尝试此查询。如果你需要解释,请问:)但这是一个相当简单的查询:)

InflowHealth.Common.InflowHealthErrorContextAttribute

答案 2 :(得分:0)

我不确定ID是否始终采用nn[-nn]格式,如果Name可以处理除鸡蛋之外的其他内容......

这个shoud在任何情况下都适用:

;with
m as (
    select *, nullif(charindex('-', ID), 0) div_id, nullif(charindex(' - ', name), 0) div_cat
    from EggTest 
),
c as (
    select *, 
        SUBSTRING(ID, 1, isnull(div_id-1, 1000)) main_ID, 
        SUBSTRING(name, 1, isnull(div_cat-1, 1000)) main_cat, 
        nullif(SUBSTRING(name, isnull(div_cat, 1000)+2, 1000), '') sub_cat
    from m
)
select main_ID ID, main_cat [Name], Seller, sum(qty) Qty
from c
group by main_ID, main_cat, seller

输出:

ID  Name    Seller  Qty
10  Egg     Anna    13
10  Egg     Bob     11
10  Egg     John    10