-3

ここに画像の説明を入力

スクリーンショット テーブルを使用して各 productCode の期首残高と期末残高を取得する方法を教えてください。sql server 2008.
ショットから、製品コード 1 は 2 回発行され、最後の残高は現在の残高を反映しています....すべての製品コードの最後の残高を取得する必要があります。

  select TOP 1 Balance as OpeningBalance 
  from StockTransfer where ProductCode = 1 and TransferType = 'Product' 
  and TransactionDate between '2013-03-17' and '2013-03-22' 

  select TOP 1 Balance as ClosingBalance 
  from StockTransfer where ProductCode = 1 and TransferType = 'Product' 
  and TransactionDate between '2013-03-17' and '2013-03-22' 
  order by TransactionDate desc 

ただし、これは 1 つの製品に対してのみ機能します。

4

3 に答える 3

0

これを試して

SELECT
  (select TOP 1 Balance as OpeningBalance 
   from StockTransfer 
   where ProductCode = ST.ProductCode and TransferType = 'Product' 
   and TransactionDate between '2013-03-17' and '2013-03-22' 
  ) As opening balance,

  (select TOP 1 Balance as ClosingBalance 
   from StockTransfer 
   where ProductCode = ST.ProductCode and TransferType = 'Product' 
   and TransactionDate between '2013-03-17' and '2013-03-22' 
   order by TransactionDate desc
   ) as colosing balance

FROM 
 StockTransfer  ST 
WHERE  TransactionDate between '2013-03-17' and '2013-03-22' 

GROUP BY ProductCode 

これは、製品全体に対して同じ結果を返します

于 2013-07-19T09:30:48.383 に答える