我有一個查詢其中,我有我的動態列值轉換成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
所需的輸出串聯一個
燦你發佈當前和期望的輸出圖像或截圖 – singhswat
沒有經驗我自己,但這似乎要走的路:http://sqlhints.com/2014/03/18/dyna MIC-樞軸式-SQL服務器/ –
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這是當前輸出 –