SQL服务器中的分层总和

时间:2018-03-08 06:51:52

标签: sql sql-server

我有Attribute Table

+-------------+--------------+----------------+-------------------+ 
| AttributeId |  Attribute   | AttributeLevel | ParentAttributeId |
+-------------+--------------+----------------+-------------------+ 
|           1 |   Ice Creams |              1 | NULL              | 
|           2 | Allegr       |              1 | NULL              | 
|           3 | 110 mlS k    |              2 | 1                 | 
|           4 | 125 mlC p    |              2 | 1                 | 
|           5 | 4750 ml b    |              2 | 1                 | 
|          12 | Extra Virgin |              2 | 2                 | 
|          13 | Extra Lite   |              2 | 2                 | 
|          14 | Pomace       |              2 | 2                 |
+-------------+--------------+----------------+-------------------+

另一个包含属性

下的每个产品的表
+--------------------+---------------------------------+-------------+
| ProductAttributeid |           ProductCode           | attributeid |
+--------------------+---------------------------------+-------------+
|                 16 | CHOCOLAT I TE LITE 500 ml Tub   |           6 |
|                 20 | VANIL ITE 500 ml Tub            |           6 |
|                 21 | CAR R 75 ml                     |           7 |
|                 22 | PEANU RPRISE BAR 75 ml          |           7 |
|                 23 | BERRIE EAM 100 ml               |          10 |
|                 24 | MANGO SORBET 100 ml Stick       |          10 |
|                 25 | RASPBERRY SORBET 100 ml Stick   |          10 |
|                 26 | ALMOND 110 ml Stick             |           3 |
|                 27 | BELGIAN CHOCOLATE 110 ml Stick  |           3 |
|                 28 | HAZELN tick                     |           3 |
|                 29 | COOKIE  ml Cup                  |           4 |
|                 30 | DOU TE 125   Cup                |           4 |
|                 31 | NATUR ERRY 125   Cup            |           4 |
|                 32 | PRAL AM 125   Cup               |           4 |
|                 33 | PREMI LA 125 ml Cup             |           4 |
|                 34 | TIRAM 5 ml Cup                  |           4 |
|                 35 | CHOCOLA WNIE DELIGHT 500 ml Tub |           6 |
|                 36 | COOK AM 500 ml Tub              |           6 |
|                 37 | DOUBL COLATE 500                |           6 |
|                 38 | MANG BET 500 ml                 |           6 |
|                 39 | NATUR WBERRY 500 ml Tub         |           6 |
|                 40 | PRALI AM 500 ml Tub             |           6 |
|                 41 | PREMIU ANILLA 500 ml Tub        |           6 |
|                 42 | STRAWB ECAKE 500 ml Tub         |           6 |
|                 43 | CARAM ISCUIT 500                |           6 |
|                 44 | LEM EESE CAKE 50                |           6 |
|                 45 | TIRAMI 00 ml Tub                |           6 |
|                 46 | CHOCOL ZELNUT 500 ml Tub        |           6 |
|                 47 | COOKIE REAM 1000 ml Tub         |          11 |
|                 48 | DOUB OCOLATE 1000 ml Tub        |          11 |
|                 49 | MOCH LMOND FUDGE 1000 ml Tub    |          11 |
|                 50 | NATUR BERRY   ml Tub            |          11 |
|                 51 | PREMIU ANILLA   ml Tub          |          11 |
|                 52 | CARAME RUNCH   ml Tub           |           5 |
|                 53 | CHEER OO   ml Tub               |           5 |
|                 54 | CHOCO OWNIE DELIGHT 4750 ml Tub |           5 |
|                 55 | COOKIE EAM 4750 ml Tub          |           5 |
|                 56 | DOUBL COLATE 4750 ml Tub        |           5 |
|                 57 | MANG RBET 4750 ml Tub           |           5 |
|                 58 | RASPB ET 4750 ml Tub            |           5 |
|                 59 | MOCHA LMON 750 ml Tub           |           5 |
|                 60 | NATUR RY 4750 ml Tub            |           5 |
|                 61 | PRALINE  CREAM 4750 ml Tub      |           5 |
|                 62 | PRE VANILLA 4750 ml Tub         |           5 |
|                 63 | TIRAM 50 ml Tub                 |           5 |
+--------------------+---------------------------------+-------------+

我需要在父属性的每个属性下找到产品数量 例如,属性1具有childAttribute 3,4等。因此,针对属性1的产品计数应该是3,4等(其所有子项)的总和。 3没有孩子 - 所以它的产品数量应该等于它的实际产品数量本身(因为没有孩子)

输出表格应为

格式
+-------------+----------------+
| AttributeId | Count_Product  |
+-------------+----------------+
|             |                |
+-------------+----------------+

2 个答案:

答案 0 :(得分:1)

使用CTE构建属性层次结构,然后使用COUNTGROUP BY

DECLARE @AttributeId    INT = 14 --Use use Attribute value here

;WITH CTE_AT
AS  (
    SELECT  AttributeId,ParentAttributeId
    FROM    @Attribute
    WHERE   AttributeId =   @AttributeId
    UNION ALL
    SELECT  A.AttributeId,A.ParentAttributeId
    FROM    @Attribute  AS  A
        INNER JOIN CTE_AT   AS  C   ON  C.ParentAttributeId =   A.AttributeId
)
SELECT  P.attributeid,COUNT(*) AS Count_Product
FROM    CTE_AT  AS  C
    INNER JOIN ProductAttribute AS  P   
            ON  P.attributeid  = C.AttributeId
GROUP BY P.attributeid
OPTION ( MAXRECURSION 0 );

答案 1 :(得分:1)

试试这个。

希望它可以帮到你:

TABLE SCHEMA:

DECLARE @Attribute TABLE(AttributeId INT,  Attribute   VARCHAR(50), AttributeLevel INT, ParentAttributeId INT)

INSERT INTO @Attribute  VALUES

(1 ,' Ice Creams',1 ,NULL )
,( 2,'Allegr     ', 1, NULL) 
,( 3,'110 mlS k  ', 2, 1   ) 
,( 4,'125 mlC p  ', 2, 1   ) 
,( 5,'4750 ml b  ', 2, 1   ) 
,(12,'Extra Virgi', 2, 2   ) 
,(13,'Extra Lite ', 2, 2   ) 
,(14,'Pomace     ', 2, 2   )


DECLARE @Product TABLE( ProductAttributeid INT,           ProductCode  VARCHAR(100)         , attributeid INT)
INSERT INTO @Product VALUES
(16 , 'CHOCOLAT I TE LITE 500 ml Tub   ',           6)
,(20 , 'VANIL ITE 500 ml Tub            ',           6)
,(21 , 'CAR R 75 ml                     ',           7)
,(22 , 'PEANU RPRISE BAR 75 ml          ',           7)
,(23 , 'BERRIE EAM 100 ml               ',          10)
,(24 , 'MANGO SORBET 100 ml Stick       ',          10)
,(25 , 'RASPBERRY SORBET 100 ml Stick   ',          10)
,(26 , 'ALMOND 110 ml Stick             ',           3)
,(27 , 'BELGIAN CHOCOLATE 110 ml Stick  ',           3)
,(28 , 'HAZELN tick                     ',           3)
,(29 , 'COOKIE  ml Cup                  ',           4)
,(30 , 'DOU TE 125   Cup                ',           4)
,(31 , 'NATUR ERRY 125   Cup            ',           4)
,(32 , 'PRAL AM 125   Cup               ',           4)
,(33 , 'PREMI LA 125 ml Cup             ',           4)
,(34 , 'TIRAM 5 ml Cup                  ',           4)
,(35 , 'CHOCOLA WNIE DELIGHT 500 ml Tub ',           6)
,(36 , 'COOK AM 500 ml Tub              ',           6)
,(37 , 'DOUBL COLATE 500                ',           6)
,(38 , 'MANG BET 500 ml                 ',           6)
,(39 , 'NATUR WBERRY 500 ml Tub         ',           6)
,(40 , 'PRALI AM 500 ml Tub             ',           6)
,(41 , 'PREMIU ANILLA 500 ml Tub        ',           6)
,(42 , 'STRAWB ECAKE 500 ml Tub         ',           6)
,(43 , 'CARAM ISCUIT 500                ',           6)
,(44 , 'LEM EESE CAKE 50                ',           6)
,(45 , 'TIRAMI 00 ml Tub                ',           6)
,(46 , 'CHOCOL ZELNUT 500 ml Tub        ',           6)
,(47 , 'COOKIE REAM 1000 ml Tub         ',          11)
,(48 , 'DOUB OCOLATE 1000 ml Tub        ',          11)
,(49 , 'MOCH LMOND FUDGE 1000 ml Tub    ',          11)
,(50 , 'NATUR BERRY   ml Tub            ',          11)
,(51 , 'PREMIU ANILLA   ml Tub          ',          11)
,(52 , 'CARAME RUNCH   ml Tub           ',           5)
,(53 , 'CHEER OO   ml Tub               ',           5)
,(54 , 'CHOCO OWNIE DELIGHT 4750 ml Tub ',           5)
,(55 , 'COOKIE EAM 4750 ml Tub          ',           5)
,(56 , 'DOUBL COLATE 4750 ml Tub        ',           5)
,(57 , 'MANG RBET 4750 ml Tub           ',           5)
,(58 , 'RASPB ET 4750 ml Tub            ',           5)
,(59 , 'MOCHA LMON 750 ml Tub           ',           5)
,(60 , 'NATUR RY 4750 ml Tub            ',           5)
,(61 , 'PRALINE  CREAM 4750 ml Tub      ',           5)
,(62 , 'PRE VANILLA 4750 ml Tub         ',           5)
,(63 , 'TIRAM 50 ml Tub                 ',           5)

此处查询开始:

SELECT A.AttributeId, ISNULL(B.AttributeId ,A.AttributeId)Child
INTO #Temp
FROM @Attribute A
LEFT JOIN(
SELECT A.AttributeId, B.AttributeId  ParentAttributeId
FROM @Attribute A
JOIN @Attribute B ON A.ParentAttributeId=B.AttributeId)B ON A.AttributeId=B.ParentAttributeId
UNION
SELECT ParentAttributeId, ParentAttributeId 
FROM @Attribute
WHERE ParentAttributeId IS NOT NULL


SELECT T.Attributeid,COUNT(1)Count_Product  
FROM #Temp T
INNER JOIN @Product P ON T.Child=P.attributeid
GROUP BY T.attributeid

DROP TABLE #Temp

<强>输出:

attributeid Count_Product
1           21
3           3
4           6
5           12