连接表而不重复行

时间:2013-08-20 23:44:09

标签: sql sql-server-2005

我在导出1时正在加入另外两个表,但这会导致行重复。而不是复制行以匹配值,是否可以用逗号分隔特定行的值?

以下是我现在的样本:

id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus
"325","30","","BRB8PACK","00LDCLU131401C","17"
"325","30","","BRB8PACK","00LDDEV131401C","17"
"325","30","","BRB8PACK","00LDHEI131401C","17"
//etc

这就是我希望它:

id,optioncatid,optionsdesc_sidenote,isproductcode,applytoproductcodes,stockstatus
"325","30","","BRB8PACK","00LDCLU131401C,00LDCLU131401C,00LDHEI131401C, etc...","17"
//etc

applytoproductcodes可能有数千个值,当以XML格式导出时,文件会膨胀到200 + MB。这显然非常臃肿。 我的SQL查询:

SELECT
    Options.ID,
    Options.OptionCatID,
    Options.optionsdesc_sidenote,
    Options.IsProductCode,
    Options_ApplyTo.ProductCode AS ApplyToProductCodes,
    Products.StockStatus AS StockStatus
FROM
    Options
JOIN Options_ApplyTo ON Options.ID = Options_ApplyTo.OptionID
JOIN Products ON Options.IsProductCode = Products.ProductCode
WHERE 
    Options.IsProductCode <> ''
ORDER BY
    Options.ID

编辑:现在我做了更多研究并修改了我的代码:

SELECT
    Options.ID,
    Options.OptionCatID,
    Options.optionsdesc_sidenote,
    Options.IsProductCode,
    t.ProductCode AS ApplyToProductCodes,
    Products.StockStatus AS StockStatus
FROM
Options
LEFT JOIN 
    (
        select OptA.ProductCode as ProductCode, OptA.OptionID as OptionID
        from Options_ApplyTo AS OptA, Options
        WHERE  Options.ID = OptA.OptionID
        order by OptA.OptionID
        for xml path('')
    ) t
ON Options.ID = t.OptionID
LEFT JOIN Products 
ON Options.IsProductCode = Products.ProductCode
WHERE 
   Options.IsProductCode <> ''
ORDER BY
   Options.ID

但现在我收到错误没有为't'的第1列指定列。

3 个答案:

答案 0 :(得分:1)

将该吸盘直接放入选择中。您无法加入,因为for xml将结果转换为标量值...而不是表格....

SELECT
    Options.ID,
    Options.OptionCatID,
    Options.optionsdesc_sidenote,
    Options.IsProductCode,
    (
        select OptA.ProductCode as ProductCode, OptA.OptionID as OptionID
        from Options_ApplyTo AS OptA
        WHERE  Options.ID = OptA.OptionID
        order by OptA.OptionID
        for xml path('')
    ) as ApplyToProductCodes,
    Products.StockStatus AS StockStatus
FROM
Options
LEFT JOIN Products 
ON Options.IsProductCode = Products.ProductCode
WHERE 
   Options.IsProductCode <> ''
ORDER BY
   Options.ID

答案 1 :(得分:0)

mysql GROUP_CONCAT函数用逗号字符连接一列。因此,您的查询可以写为

SELECT
    Options.ID,
    Options.OptionCatID,
    Options.optionsdesc_sidenote,
    Options.IsProductCode,
    t.pcodes AS ApplyToProductCodes,
    Products.StockStatus AS StockStatus
FROM
OPTIONS
JOIN 
(SELECT Options_ApplyTo.OptionID, GROUP_CONCAT(Options_ApplyTo.ProductCode) pcodes       
    FROM Options_ApplyTo GROUP BY Options_ApplyTo.OptionID) t
ON Options.ID = t.OptionID
JOIN Products 
ON Options.IsProductCode = Products.ProductCode
WHERE 
   Options.IsProductCode <> ''
ORDER BY
   Options.ID

答案 2 :(得分:0)

对不起,也许我缺乏经验我觉得提供的信息不足以让我为你写测试数据,但我可以给你一个想法,使用STUFF

DECLARE @t1 TABLE
(
id INT,
optioncatid INT,
optionsdesc_sidenote NVARCHAR(255),
isproductcode NVARCHAR(255),
applytoproductcodes INT
)


INSERT INTO @t1 VALUES 
(325,30,'BRB8PACK','00LDCLU131401C',17),
(325,30,'BRB8PACK','00LDCLU131401C',17),
(325,30,'BRB8PACK','00LDCLU131401C',17)



SELECT id,optioncatid,
STUFF((SELECT ','+optionsdesc_sidenote 
FROM @t1 WHERE id=325 FOR XML PATH('')) , 1 , 1  , '' ),
optionsdesc_sidenote,
isproductcode,
applytoproductcodes FROM @t1