ich eine Abfrage, wo, in denen ich meine dynamischen Spaltenwerte in eine sinle ReiheWie mehrere Spalt einzelne Zeilen in SQL Server konvertieren
Dies ist die Abfrage
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
Hier werden die konvertieren Shopcodes sind dynamisch und das ist auch die Produkt-ID.
Das Ergebnis wird in 5 Zeilen mit dem gleichen Produkt und mit verschiedenen Ergebnissen der Unterabfrage sein. Was ich will, ist, dass es in einer einzigen Zeile und Ergebnisse meiner Sub-Abfragen sollte man nach dem anderen
Stromausgang
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
SOLL OUTPUT
verketten sein sollte Can Sie posten das aktuelle und gewünschte Ausgabebild oder Screenshots – singhswat
Keine Erfahrung selbst, aber das scheint der Weg zu gehen: http://sqlhints.com/2014/03/18/dyna MHK-Schwenk-in-SQL-Server/ –
3289, \t Der Schmetterlings-Baum \t, Genähter \t, Shirts - ohne Stickerei , BGD \t, 34 \t, NULL \t, NULL 3289, \t Der Schmetterlings-Baum \t, genähte \t, Shirts - ohne Stickerei \t, 2600 \t, CGD \t, NULL \t, NULL \t, NULL 3289, \t Der Schmetterlings-Baum \t, genähte \t, Shirts - ohne Stickerei \t, 2600 \t, DMC \t, 184 \t, 35 \t, 9 3289, \t Der Schmetterlings-Baum \t, Genähtes \t, Shirts - ohne Stickerei \t, 2600 \t, DOL \t, 187 \t, 24 \t, 6 3289, \t Der Schmetterlings-Baum \t, Genähtes \t, Shirts - Ohne Stickerei \t, 2600 \t, GUL \t, 242 \t, 73 \t, 23 Dies ist die aktuelle Ausgabe –