2017-11-03 13 views
0

私は同じフィールドで4つの異なる計算を返すスクリプトを作成しようとしていますが、実際に結果を単一の行に戻したいと思っています。mssql select文結果の単一行

現在、結果があるときに結果ごとに新しい行が返されます。

SELECT DISTINCT 
    sp.PartNumber, 
    (StandardUnitMaterialCost+StandardUnitRunCost+StandardUnitSetCost+StandardUnitSubcontractCost+StandardLandedCost1+StandardLandedCost2+StandardLandedCost3+StandardLandedCost4+StandardLandedCost5) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) < MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND slp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND slp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND slp.TransactionCodeID IN (1,2,13) AND slp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND slp.TransactionCodeID IN (3,7) AND slp.PartID = Stores.StockLogPart.PartID) 


FROM Stores.StockLogPart slp 
JOIN Structure.Parts sp ON sp.PartID = slp.PartID 
JOIN Stores.ProductGroups pg ON pg.ProductGroupID = sp.ProductGroupID 
WHERE pg.ProductGroupCode = 'XX' 
+0

しかし、複数の行があると思われます.pg.ProductGroupCode = 'XX'。あなたは同じ行に複数のTransactionDateを取得するつもりはありません。 – Paparazzi

答えて

1

あなたの結合の1つを削除する必要があると思います。コストのフィールドがどのテーブルにあるのか不明なため、少し難しいです。これを試してみてください:

SELECT DISTINCT 
    sp.PartNumber, 
    (StandardUnitMaterialCost+StandardUnitRunCost+StandardUnitSetCost+StandardUnitSubcontractCost+StandardLandedCost1+StandardLandedCost2+StandardLandedCost3+StandardLandedCost4+StandardLandedCost5) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) < MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND sp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND sp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND stores.StockLogPart.TransactionCodeID IN (1,2,13) AND sp.PartID = Stores.StockLogPart.PartID) 
    , 
    (select ISNULL(sum(quantity),0) from stores.StockLogPart where month(TransactionDate) = MONTH(getdate()) AND year(TransactionDate) <= year(getdate()) AND stores.StockLogPart.TransactionCodeID IN (3,7) AND sp.PartID = Stores.StockLogPart.PartID) 

FROM Structure.Parts sp 
JOIN Stores.ProductGroups pg ON pg.ProductGroupID = sp.ProductGroupID 
WHERE pg.ProductGroupCode = 'XX' 
+0

ありがとうRik D(返信遅れて申し訳ありません)。修正されたスクリプトは完全に機能しました。あなたがどのように知っているので簡単です。ありがとう – Leighbee

関連する問題