我有这段代码
SELECT
StockItem.ItemTitle,
StockItem.ItemNumber,
ProductCategories.CategoryName,
StockItem.ItemDescription,
PurchaseItem.Quantity AS QuantityOrdered,
Purchase.DateOfPurchase,
Purchase.DateOfDelivery,
Purchase.ExternalInvoiceNumber as PONumber,
Purchase.QuotedDeliveryDate,
StockLevel.Quantity AS CurrentQuantity,
StockLevel.OnOrder,
StockLevel.MinimumLevel
FROM dbo.PurchaseItem
INNER JOIN dbo.Purchase
ON PurchaseItem.fkPurchasId = Purchase.pkPurchaseID
RIGHT OUTER JOIN dbo.StockItem
ON PurchaseItem.fkStockItemId = StockItem.pkStockItemID
LEFT OUTER JOIN dbo.StockLevel
ON StockLevel.fkStockItemId = StockItem.pkStockItemID
LEFT OUTER JOIN dbo.ProductCategories
ON ProductCategories.CategoryId = StockItem.CategoryId
where ProductCategories.CategoryName = 'default'
将返回值
ItemTitle ItemNumber CategoryName ItemDescription QuantityOrdered DateOfPurchase DateOfDelivery PONumber QuotedDeliveryDate CurrentQuantity OnOrder MinimumLevel
FBIM01 FBIM01 Default Default 10 5/19/2011 0:00 7/6/2011 15:19 POA90949 6/24/2011 0:00 20 160 50
FBIM01 FBIM01 Default Default 20 5/27/2011 0:00 6/23/2011 0:00 POA90950 6/24/2011 0:00 20 160 50
FBIM01 FBIM01 Default Default 30 6/20/2011 0:00 6/23/2011 0:00 POA90951 6/24/2011 0:00 20 160 50
FBIM01 FBIM01 Default Default 100 6/15/2011 0:00 6/23/2011 0:00 POA90952 6/24/2011 0:00 20 160 50
然而,我一直在试图让它显示为
ItemTitle ItemNumber CategoryName ItemDescription QuantityOrdered QuantityOrdered2 QuantityOrdered3 QuantityOrdered4 PONumber1 PONumber2 PONumber3 PONumber4 CurrentQuantity OnOrder MinimumLevel
FBIM01 FBIM01 Default Default 10 20 30 100 POA90949 POA90950 POA90951 POA90952 20 160 50
我的技能不在那里。请问大师请帮我解决这个问题? 谢谢
答案 0 :(得分:0)
;with CTE AS ( SELECT StockItem.ItemTitle, StockItem.ItemNumber, ProductCategories.CategoryName, StockItem.ItemDescription, PurchaseItem.Quantity AS QuantityOrdered, Purchase.DateOfPurchase, Purchase.DateOfDelivery, Purchase.ExternalInvoiceNumber as PONumber, Purchase.QuotedDeliveryDate, StockLevel.Quantity AS CurrentQuantity, StockLevel.OnOrder, StockLevel.MinimumLevel, ROW_NUMBER()Over(Partition by StockItem.ItemTitle Order by StockItem.ItemTitle) As Rn FROM dbo.PurchaseItem INNER JOIN dbo.Purchase ON PurchaseItem.fkPurchasId = Purchase.pkPurchaseID RIGHT OUTER JOIN dbo.StockItem ON PurchaseItem.fkStockItemId = StockItem.pkStockItemID LEFT OUTER JOIN dbo.StockLevel ON StockLevel.fkStockItemId = StockItem.pkStockItemID LEFT OUTER JOIN dbo.ProductCategories ON ProductCategories.CategoryId = StockItem.CategoryId where ProductCategories.CategoryName = 'default' ) Select ItemTitle,ItemNumber, CategoryName, ItemDescription, Quantity AS QuantityOrdered, DateOfPurchase, DateOfDelivery, ExternalInvoiceNumber as PONumber, QuotedDeliveryDate, Quantity AS CurrentQuantity, OnOrder, MinimumLevel, From CTE where rn=1