-1

私は次の表を持っています

QuotationId QuotationDetailId   DriverId    RangeFrom   RangeTo FixedAmount UnitAmount
-------------------------------------------------------------------------------------------
    10579      7                   1           1          1     1154.00      0.00
    10579      7                   2           2          2     1731.00      0.00
    10579      11                  1           0         10     0.00         88.53
    10579      11                  2           11        24     885.30       100.50
    10579      11                  3           25        34     2292.30      88.53

次のロジックを使用して、SQL Server でクエリを作成する必要があります。

  • グループ化は QuotationId + QuotationDetailId です。
  • このブロックごとに、2 行目から前の行の値を合計する必要があります。

    Amount + UnitAmount * RangeFrom + FixedAmount of the current row
    

したがって、この場合、結果の出力は次のようになります。

  QuotationId QuotationDetailId   DriverId    RangeFrom   RangeTo   FixedAmount  UnitAmount
10579             7                1           1           1        1154.00    0.00
10579             7                2           2           2        2885.00    0.00
10579             11               1           0           10       0.00       88.53
10579             11               2           11          24       1770.60    100.50
10579             11               3           25          34       7174.90    88.53

いくつかのクエリを試しましたが、成功しませんでした。誰かが私にそれを行う方法を提案できますか?

よろしくファブリツィオ

4

2 に答える 2

2

SQL Server 2012 以降では、累積合計を実行できます。ロジックが何を望んでいるのか正確にはわかりませんが、データセットを考えるとこれは妥当なようです:

select t.*,
       sum(FixedAmount*UnitAmount) over (partition by QuotationId, QuotationDetailId
                                         order by DriverId
                                        ) as running_sum
from t;
于 2016-08-12T14:33:34.843 に答える
0

サブクエリを使用できます。「金額」列は、括弧内のクエリとして列のリストに表示されます。

SELECT ...fields..., 
       (SELECT SUM(A.unitAmount * A.RangeFrom + A.fixedAmount) 
               From YourTable A
               WHERE A.QuotationId = B.QuotationId 
               AND   A.QuotationDetailId = B.QuotationDetailId
               AND   A.DriverId <= B.DriverId) AS Amount
        From YourTable B 
于 2016-08-12T15:07:17.463 に答える