2017-08-15 83 views
0

我有两个表,有没有办法在SQL Server中通过来自一对多关系的列表来执行GROUP BY?

tb_product

Id  typeProduct 
1   Type1 
2   Type1 
3   Type1 

产品1,2和3是由于typeProduct相等,但各产品已连结的可区分它们,以及属性。

tb_product_attribute

idProduct typeAttribute valueAttribute 
    1   Attr1   Value1 
    1   Attr2   Value3 
    2   Attr1   Value1 
    2   Attr2   Value3 
    3   Attr1   Value2 
    3   Attr3   Value3 

因此,产品1和2将是由于属性的属性和值,因为3将具有与所述第二属性不同的值,因为它的第二属性的是不同的其他人。

理想的结果是这样,因为我会知道它们之间的平等细节,但我不知道这种类型的结果是否可能。

total_rows typeProduct typeAttribute  valueAttribute 
    2   Type1  Attr1, Attr2  Value1, Value3 
    1   Type1  Attr1, Attr3  Value2, Value3 
+1

你的问题不太清楚(我)..请更新您的问题,加上预期的结果..或更好地解释你的目标 – scaisEdge

+0

你如何确定如果一个产品被复制? idTipoProduto与某事有关吗?我假定Product.Id与ProductAttributes.idPododuto有关。这是真的? –

+0

有了您提供的样本数据,您能向我们展示您的预期结果吗? –

回答

0

或者不CTE

select count(valueattribute) total_rows, 
     typeProduct, 
     typeAttribute, 
     valueAttribute 
from 
(select 
    (select top 1 tp.typeProduct 
     from tb_product as tp 
      where tp.Id = p.idProduct) as typeProduct, 
    stuff((
     SELECT ', ' + [valueAttribute] 
     FROM tb_product_attribute 
     WHERE (idproduct = p.idproduct) 
     FOR XML PATH('')), 1,2,'') AS valueAttribute, 
    stuff((
     SELECT ', ' + [typeAttribute] 
     FROM tb_product_attribute 
     WHERE (idproduct = p.idproduct) 
     FOR XML PATH('')), 1,2,'') AS typeAttribute 
    from tb_product_attribute as p 
     group by idproduct 
) as subs 
group by valueattribute, typeProduct, typeAttribute 
+0

这两个发布的结果返回了我所需要的,但其代码运行得更快。非常感谢你。 –

0

你要组属性和值第一再算上每行

WITH cte AS (
SELECT p.typeproduct, 
     t.idproduct, 
     STUFF((SELECT ', ' + typeattribute 
       FROM tb_product_attribute 
       WHERE idproduct = t.idproduct 
       FOR XML PATH(''),TYPE) 
       .value('.','NVARCHAR(MAX)'),1,2,'') AS typeattribute, 
     STUFF((SELECT ', ' + typevalue 
       FROM tb_product_attribute 
       WHERE idproduct = t.idproduct 
       FOR XML PATH(''),TYPE) 
       .value('.','NVARCHAR(MAX)'),1,2,'') AS typevalue  
    FROM tb_product_attribute t 
INNER JOIN tb_product p 
    ON t.idproduct = p.id 
GROUP BY p.typeproduct, 
      t.idproduct 
) 

SELECT COUNT(*) total_rows, 
     typeproduct, 
     typeattribute, 
     typevalue 
    FROM cte 
GROUP BY typeproduct, 
      typeattribute, 
      typevalue 

结果

total_rows typeproduct typeattribute typevalue 
2   Type1  Attr1, Attr2 Value1, Value3 
1   Type1  Attr1, Attr3 Value2, Value3 
+0

它工作得很好。起初我使用了STUFF,但是我没有得到任何附近的信息,而我的查询使用40k注册花了很长时间,使用该代码,它只花了2秒钟,共计43492个。 而且使用ASpirin代码,它运行得更快仍然呈现相同的结果。 非常感谢你 –

0

试试吧

create table tb_product 
    ( 
    id   INT 
    ,typeproduct CHAR(5) 
) 
create table tb_product_attribute (
idProduct int, typeAttribute char(5), valueAttribute char(6) 
) 
GO 


CREATE Function dbo.TypeAttribute2Comma(@idProduct int) 
returns varchar(max) 
BEGIN 
    declare @tmp varchar(max) 
    SET @tmp = '' 
    select @tmp = @tmp + typeAttribute + ', ' from tb_product_attribute where idProduct = @idProduct 
    RETURN (select SUBSTRING(@tmp, 0, LEN(@tmp))) 

END 
GO 

CREATE Function dbo.ValueAttribute2Comma(@idProduct int) 
returns varchar(max) 
BEGIN 
    declare @tmp varchar(max) 
    SET @tmp = '' 
    select @tmp = @tmp + typeAttribute + ', ' from tb_product_attribute where idProduct = @idProduct 
    RETURN (select SUBSTRING(@tmp, 0, LEN(@tmp))) 

END 
GO 

INSERT tb_product 
     (id ,typeproduct) 
VALUES (1 ,'Type1'), 
     (2 ,'Type1'), 
     (3 ,'Type1') 

insert tb_product_attribute(idProduct ,typeAttribute ,valueAttribute) values 
    (1, 'Attr1', 'Value1'), 
    (1, 'Attr2', 'Value3'), 
    (2, 'Attr1', 'Value1'), 
    (2, 'Attr2', 'Value3'), 
    (3, 'Attr1', 'Value2'), 
    (3, 'Attr3', 'Value3') 

GO 

select 
* 
,dbo.TypeAttribute2Comma(id) TypeAttribute2 
,dbo.ValueAttribute2Comma(id) ValueAttribute2 
from tb_product 

结果

id   typeproduct TypeAttribute2 ValueAttribute2 
----------- ----------- -------------- ---------------- 
1   Type1  Attr1, Attr2 Attr1, Attr2 
2   Type1  Attr1, Attr2 Attr1, Attr2 
3   Type1  Attr1, Attr3 Attr1, Attr3 
相关问题