2016-11-29 53 views
-1

我有一个查询其中,我有我的动态列值转换成sinle行如何将多个列转换为单列在SQL Server

这是查询

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

+0

灿你发布当前和期望的输出图像或截图 – singhswat

+0

没有经验我自己,但这似乎要走的路:http://sqlhints.com/2014/03/18/dyna MIC-枢轴式-SQL服务器/ –

+0

3289,\t蝴蝶树\t,缝合\t,衬衫 - 没有刺绣\t,2600 \t,BGD \t,34 \t,NULL \t,NULL 3289,\t蝴蝶树\t,缝合\t,衬衫 - 没有刺绣\t,2600 \t,CGD \t,NULL \t,NULL \t,NULL 3289,\t蝴蝶树\t,缝合\t,衬衫 - 没有刺绣\t,2600 \t,DMC \t,184 \t,35 \t,9 3289,\t蝴蝶树\t,缝合\t,衬衫 - 没有刺绣\t,2600 \t,DOL \t,187 \t,24 \t,6 3289,\t蝴蝶树\t,缝合\t,衬衫 - 无刺绣\t,260\t,GUL \t,242 \t,73 \t,23这是当前输出 –

回答

0
CREATE TABLE #table(RefId INT IDENTITY(1,1) ,Id INT,Name VARCHAR(100),LName 
VARCHAR(100),Cat VARCHAR(100),Price INT,Code VARCHAR(100),Dis INT  
DEFAULT(0),SAl INT DEFAULT(0),LSal INT DEFAULT(0)) 
INSERT INTO #table(Id ,Name ,LName ,Cat ,Price ,Code ,Dis ,SAl ,LSal) 
SELECT 3289,'The Butterfly Tree','Stitched','Shirts - Without  
Embroidery',2600 ,'BGD' ,34 ,0 ,0 UNION ALL 
SELECT 3289,'The Butterfly Tree','Stitched','Shirts - Without  
Embroidery',2600 ,'CGD' ,0 ,0 ,0 UNION ALL 
SELECT 3289,'The Butterfly Tree','Stitched','Shirts - Without 
Embroidery',2600 ,'DMC' ,184 ,35 ,9 UNION ALL 
SELECT 3289,'The Butterfly Tree','Stitched','Shirts - Without 
Embroidery',2600 ,'DOL' ,187 ,24 ,6 UNION ALL 
SELECT 3289,'The Butterfly Tree','Stitched','Shirts - Without 
Embroidery',2600 ,'GUL' ,242 ,73 ,23 

;WITH CTE AS 
( 
    SELECT Id , CAST(Dis AS VARCHAR) + ',' + CAST(SAl AS VARCHAR)+ ',' +  
    CAST(LSal AS VARCHAR) AS Comb 
    FROM #table 
) 
SELECT DISTINCT Id , Name ,LName ,Cat ,Price,STUFF((SELECT ',' + Comb FROM  
CTE WHERE #table.Id = CTE.Id FOR XML PATH('')),1,1,' ') 
FROM #table 
+0

请详细解释您的答案 –

+0

首先使用CTE将您的DISPATCH,SALES,LAST_WEEK_SALES值连接起来。然后用你的物理表使用CTE表,并用stuff方法得到结果 – Mansoor

+0

谢谢mansoor。但要求是,我需要结果在单个SQL查询 –