2017-08-15 19 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 
相關問題