我有一个查询其中,我有我的动态列值转换成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这是当前输出 –