group by または同等のものを使用して実行中の合計を実行する良い方法を見つけるのに苦労しています。以下のカーソルベースの現在の合計は完全なテーブルで機能しますが、これを拡張して「クライアント」ディメンションを追加したいと思います。したがって、以下が作成するように現在の合計を取得しますが、1 つのテーブル内の各会社 (つまり、会社 A、会社 B、会社 C など) についてです。
CREATE TABLE test (tag int, Checks float, AVG_COST float, Check_total float, Check_amount float, Amount_total float, RunningTotal_Check float,
RunningTotal_Amount float)
DECLARE @tag int,
@Checks float,
@AVG_COST float,
@check_total float,
@Check_amount float,
@amount_total float,
@RunningTotal_Check float ,
@RunningTotal_Check_PCT float,
@RunningTotal_Amount float
SET @RunningTotal_Check = 0
SET @RunningTotal_Check_PCT = 0
SET @RunningTotal_Amount = 0
DECLARE aa_cursor CURSOR fast_forward
FOR
SELECT tag, Checks, AVG_COST, check_total, check_amount, amount_total
FROM test_3
OPEN aa_cursor
FETCH NEXT FROM aa_cursor INTO @tag, @Checks, @AVG_COST, @check_total, @Check_amount, @amount_total
WHILE @@FETCH_STATUS = 0
BEGIN
SET @RunningTotal_CHeck = @RunningTotal_CHeck + @checks
set @RunningTotal_Amount = @RunningTotal_Amount + @Check_amount
INSERT test VALUES (@tag, @Checks, @AVG_COST, @check_total, @Check_amount, @amount_total, @RunningTotal_check, @RunningTotal_Amount )
FETCH NEXT FROM aa_cursor INTO @tag, @Checks, @AVG_COST, @check_total, @Check_amount, @amount_total
END
CLOSE aa_cursor
DEALLOCATE aa_cursor
SELECT *, RunningTotal_Check/Check_total as CHECK_RUN_PCT, round((RunningTotal_Check/Check_total *100),0) as CHECK_PCT_BIN, RunningTotal_Amount/Amount_total as Amount_RUN_PCT, round((RunningTotal_Amount/Amount_total * 100),0) as Amount_PCT_BIN
into test_4
FROM test ORDER BY tag
create clustered index IX_TESTsdsdds3 on test_4(tag)
DROP TABLE test
----------------------------------
任意の 1 つの会社の現在の合計を計算できますが、以下の結果のようなものを生成するために、複数の会社に対して実行したいと考えています。
CLIENT COUNT Running Total
Company A 1 6.7%
Company A 2 20.0%
Company A 3 40.0%
Company A 4 66.7%
Company A 5 100.0%
Company B 1 3.6%
Company B 2 10.7%
Company B 3 21.4%
Company B 4 35.7%
Company B 5 53.6%
Company B 6 75.0%
Company B 7 100.0%
Company C 1 3.6%
Company C 2 10.7%
Company C 3 21.4%
Company C 4 35.7%
Company C 5 53.6%
Company C 6 75.0%
Company C 7 100.0%