2016-11-29 56 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查詢 –