连接表并以逗号分隔返回数据

时间:2016-12-05 07:32:04

标签: sql sql-server join sql-server-2008-r2

我有如下所述的要求。查询需要在SQL Server 2008 R2数据库上获取数据。

尽我所能获取下面提到的数据,但无法实现。真的很感谢你的帮助。

Product_table

SKU   UPC   Details  Weight  Color  
223   111   TShirt    25     White
224   114   Pants     25     Black
225   115   Abc       29     Yellow
230   116   XyX       23     Pink
226   117   AXYz      25     Red
226   118   Abdc      26     White
228   119   Abcr      20     Pink
229   120   Abcy      22     Green

Custom_tbl

SKU   Custom_name   Custom_value
223     Pickup           true
223     eligible         false
223     size             medium
223     map              red
224     pickup           false
224     eligible         false
224     map              green
225     Pickup           true
225     eligible         true
225     size             large
225     department       001
225     availability     true
226     Pickup           true
226     size             large
226     map              blue
226     availability     true
229     eligible         true

从上面提到的两个表中查询需要获取如下所述的数据。

注意:CustomValues(示例表中的第六列)以逗号分隔,我只需要按顺序排列3个值(Custom_tbl中的Pickup,符合条件的地图列,其余需要忽略。如果任何custom_name不可用,那么它应该返回空字符串(从第3行检查),如下所示)

SKU   UPC   DETAILS  WEIGHT   COLOR      CustomValues
223   111   TShirt    25      White      true,false,red
224   114   Pants     25      Black      false,false,green
225   115   Abc       29      Yellow     true,true,
226   118   Abdc      26      White      true,,blue
228   119   Abcr      20      Pink       ,,,
229   120   Abcy      22      Green      ,true,

可以帮助我修改上述相同数据的查询,但我需要排除其Custom_name符合条件且值为false的数据。我有这个查询,它得到我正在寻找原始问题,但无法添加我正在寻找的条件。感谢你的帮助。

对于相同的上述数据,我需要排除其Custom_name合格且值为false的数据。我有这个查询但无法添加上面的逻辑

SELECT productDetails.sku, Isnull(productDetails.sku, '') + ',' 
       + Isnull(productDetails.upc, '') + ',' 
       + Isnull(productDetails.details, '') 
       + ',' + CustomValues 
        FROM  (SELECT PD.*, 
               Stuff((SELECT ',' + Attributes.customval 
                     FROM   (SELECT 
                     A.sku, 
                     Isnull(Max(A.[Pickup]), '') + ',' 
                     + Isnull(Max(A.[eligible]), '') + ',' 
                     + Isnull(Max(A.[size]), '') AS customVal
                     FROM ( SELECT sku, Isnull(CASE WHEN custom_name = 'Pickup' THEN Max(custom_value) END, '') AS 'Pickup', 
                                       Isnull(CASE WHEN custom_name = 'eligible' THEN Max(custom_value) END, '') AS 'eligible', 
                                       Isnull(CASE WHEN custom_name = 'size' THEN Max(custom_value) END, '') AS 'size'
                            FROM  [product_custom_details]
                            GROUP  BY sku, custom_field_name ) AS A                                                       
                            GROUP  BY A.sku ) Attributes 
                      WHERE  Attributes.sku = PD.sku 
                      FOR xml path('')), 1, 1, '') AS CustomValues 
        FROM   [product_details] PD) AS productDetails 
ORDER  BY productDetails.sku 

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT DISTINCT P.SKU,P.UPC,Details,P.[Weight],P.[Color],
STUFF((SELECT ',' + Custom_value 
       from  Custom_tbl 
       where SKU=P.SKU FOR XML PATH('')),1,1,'') AS Customvalues
FROM Product_table P

答案 1 :(得分:0)

您可以在每GROUP BY Custom_tbl表格中使用SKU,并使用CASE WHEN获取单独的自定义值

SELECT
   SKU,
   ISNULL(CASE WHEN Custom_name = 'Pickup' THEN Custom_value END,'') + ','
   ISNULL(CASE WHEN Custom_name = 'eligible' THEN Custom_value END,'') + ','
   ISNULL(CASE WHEN Custom_name = 'map' THEN Custom_value END,'') as CustomValues
FROM Custom_tbl
WHERE Custom_name IN ('Pickup','eligible','map')
GROUP BY SKU

现在只需按照sku

的主表加入
SELECT P.SKU, UPC, DETAILS, WEIGHT, COLOR, ISNULL(C.CustomValues,'') as CustomValues 
FROM Product_table P
LEFT JOIN(
    SELECT
       SKU,
       ISNULL(CASE WHEN Custom_name = 'Pickup' THEN Custom_value END,'') + ','
       ISNULL(CASE WHEN Custom_name = 'eligible' THEN Custom_value END,'') + ','
       ISNULL(CASE WHEN Custom_name = 'map' THEN Custom_value END,'') as CustomValues
    FROM Custom_tbl
    WHERE Custom_name IN ('Pickup','eligible','map')
    GROUP BY SKU
)C ON P.sku = C.sku

答案 2 :(得分:0)

DECLARE @Product_Table AS TABLE (SKU INT, UPC INt, Details VARCHAR(100), Weight INT, Color VARCHAR(100))
DECLARE @Custom_Tbl AS TABLE (SKU INT, Custom_Name VARCHAR(100), Custom_Value VARCHAR(100))

INSERT INTO @Product_Table VALUES (223,111,'TShirt',25,'White'),(224,114,'Pants',25,'Black')
,(225,115,'Abc',29,'Yellow'),(230,116,'XyX',23,'Pink'),(226,117,'AXYz',25,'Red')
,(226,118,'Abdc',26,'White'),(228,119,'Abcr',20,'Pink'),(229,120,'Abcy',22,'Green')

INSERT INTO @Custom_Tbl VALUES (223,'Pickup','true'),(223,'eligible','false'),(223,'size','medium')
,(223,'map','red'),(224,'pickup','false'),(224,'eligible','false'),(224,'map','green')
,(225,'Pickup','true'),(225,'eligible','true'),(225,'size','large'),(225,'department','001')
,(225,'availability','true'),(226,'Pickup','true'),(226,'size','large'),(226,'map','blue')
,(226,'availability','true'),(229,'eligible','true')


;WITH cteCustomNames AS (
    SELECT *
    FROM
       (VALUES ('Pickup',1),('eligible',2),('map',3)) t(Custom_name,StringOrder)
)

, cteProductRowNum AS (
    SELECT
       p.*
       ,RowNum = ROW_NUMBER() OVER (PARTITION BY p.SKU ORDER BY Weight)
    FROM
       @Product_Table p
)

SELECT * , STUFF(
          (SELECT ',' + ISNULL(Custom_value,'')
           FROM
             cteCustomNames cn
             LEFT JOIN @Custom_Tbl c
             ON cn.Custom_name = c.Custom_Name
             AND c.SKU = p.SKU
           ORDER BY
             cn.StringOrder
            FOR XML PATH(''))

        ,1,1,'')
FROM
    cteProductRowNum p
WHERE
    p.RowNum = 1

因此,您的查询中有一些棘手的部分。首先,即使该位置未退出,您也会为每个位置显示逗号。这需要交叉或左连接来拉开。您显示的结果为每个SKU一行,但在您的产品表中,您显示多个产品SKU,因此您需要排名功能,以确定您想要的行。无论如何,这是一种实现这一切的方法。与其他人一样,我也建议STUFF()使用FOR XML进行连接。哦,你提到你想要按特定顺序排列字符串。