如何在SQL Server中将多列转换为单行

时间:2016-11-29 11:06:48

标签: sql sql-server sql-server-2008

我有一个查询,其中我必须将我的动态列值转换为sinle行

这是查询

select
    tblDefProducts.product_id AS Product_id, 
    tblDefProducts.item_name AS Product_name, 
    tblDefLineItems.field_name AS Line_name, 
    tblDefCategory.field_name As Category_name,
    DimRetailPrice.product_price AS Product_price,
    tblDefShops.shop_code AS Shop_code,
    (select SUM(NetQuantityMoved) 
     from StockMovmentFactTableNew 
     where StoreCode in ('whs') 
       and ReceiveShop = tblDefShops.shop_code 
       and ProductCode in (select ProductCode 
                           from DimCode 
                           where Product_Item_ID in (select Product_Item_ID 
                                                     from tblProductItem
                                                     where Product_ID = tblDefProducts.product_id))
    ) AS DISPATCH,
    (select SUM(NetQuantityMoved) 
     from StockMovmentFactTableNew 
     where TransType in ('SalesReturn', 'Sales') 
       and TimeStamp < '2016-10-09' 
       and StoreCode = tblDefShops.shop_code 
       and ProductCode in (select ProductCode 
                           from DimCode 
                           where Product_Item_ID in (select Product_Item_ID 
                                                     from tblProductItem 
                                                     where Product_ID = tblDefProducts.product_id))
    ) AS SALES,
    (select SUM(NetQuantityMoved)  
     from StockMovmentFactTableNew 
     where ProductCode in (select ProductCode 
                           from DimCode 
                           where Product_Item_ID in (select Product_Item_ID 
                                                     from tblProductItem 
                                                     where Product_ID = tblDefProducts.product_id)) 
       and TransType in ('SalesReturn', 'Sales') 
       and TimeStamp between DATEADD(day, -7, '2016-10-09') and  '2016-10-09' 
       and StoreCode = tblDefShops.shop_code) AS LAST_WEEK_SALE
from
    tblDefProducts, tblDefLineItems, tblDefCategory,
    DimRetailPrice, tblProductItem 
cross join 
    tblDefShops
where 
    tblDefProducts.line_item_id = tblDefLineItems.line_item_id
    and tblDefCategory.line_item_id = tblDefLineItems.line_item_id
    and tblProductItem.Product_ID = tblDefProducts.product_id
    and tblProductItem.Product_Item_ID = DimRetailPrice.product_item_id
    and tblDefCategory.category_id in (40)
    and tblDefProducts.product_id in( 3289 )
    and tblDefLineItems.line_item_id = 2
    and tblDefShops.shop_code in ('BGD' , 'DOL' , 'DMC' , 'GUL' ,'CGD')
Group by 
    tblDefProducts.product_id, tblDefProducts.item_name, 
    tblDefLineItems.field_name, tblDefCategory.field_name ,
    DimRetailPrice.product_price, Shop_code

这里的商店代码是动态的,product_id也是如此。

结果将是5行,具有相同的产品和不同的子查询结果。我想要的是它应该在一行中,我的子查询的结果应该一个接一个地连接

当前输出

   Product_id   Product_name        Line_name   Category_name               Product_price   Shop_code   DISPATCH    SALES   LAST_WEEK_SALE
    3289       The Butterfly Tree   Stitched    Shirts - Without Embroidery     2600        BGD             34      NULL        NULL
    3289       The Butterfly Tree   Stitched    Shirts - Without Embroidery     2600        CGD             NULL    NULL        NULL
    3289       The Butterfly Tree   Stitched    Shirts - Without Embroidery     2600        DMC             184     35          9
    3289       The Butterfly Tree   Stitched    Shirts - Without Embroidery     2600        DOL             187     24          6
    3289       The Butterfly Tree   Stitched    Shirts - Without Embroidery     2600        GUL             242     73          23

期望的输出

enter image description here

1 个答案:

答案 0 :(得分:0)

param(
    [Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91]$MyParameter
)