値を保持している変数をループで合計したいのですが、
ここに私のテーブルがあります:
name | pkey | cinema1~cinema10 | total_QTy | total_sales
aaa | 1 | 100.00 ~ 200.00 | 2 | 300.00
bbb | 2 | 150.00 ~ 145.00 | 3 | 295.00
total_Qty は、シネマ 1 からシネマ 10 で購入した名前 aaa のチケットの合計ですが、これを行う方法がわかりません。名前 bbb cinema10 にあるカウントされたチケットの最後の値のみを取得しています。
お願い助けて。私はここで立ち往生しています。
これが金額と数量を取得するための私のコードです
WHILE ISNULL(@cinema_key, 0) <> 0
BEGIN
SET @amount = 0
SELECT @amount =(SUM(t1.price)), @qty = (count(t1.cnt))
FROM(
SELECT
(ISNULL(SUM(mctrs.price),0) - (ISNULL(SUM(mctrs.lgu_tax_amount),0) + ISNULL(SUM(mctrs.ordinance_1),0) + ISNULL(SUM(mctrs.ordinance_2),0)+ ISNULL(SUM(mctrs.ordinance_3),0) + ISNULL(SUM(mctrs.ordinance_4),0) )) AS 'price',
count(mctrs.[key]) as 'cnt', mctrs.[key] as ckey
FROM
movie_calendar mc
INNER JOIN
movie_database md
ON
mc.movie_key = md.[key] AND mc.screening_start_date = @tdate
INNER JOIN
cinemas c
ON
mc.cinema_key = c.[key] AND c.[key] = @cinema_key
INNER JOIN
movie_calendar_times mct
ON
mct.movie_calendar_key = mc.[key]
LEFT OUTER JOIN
movie_calendar_time_reserved_seats mctrs
ON
mctrs.movie_calendar_time_key = mct.[key]
INNER JOIN
patrons p
ON
mctrs.patron_key = p.[key]
WHERE mctrs.status = 1 AND mctrs.[committed]=1
GROUP BY p.patron_tag,mctrs.price,P.APPLY_LGU_TAX ,P.LGU_TAX_AMOUNT, mctrs.[key]
)t1
SET @buffer = 'UPDATE #temptable SET cinema'+CAST(@count AS VARCHAR)+' = '+CAST(ISNULL(sum(@amount),0) AS VARCHAR)+' WHERE [date] = '''+CAST (@tdate AS VARCHAR)+''''
PRINT (@buffer)
EXEC(@buffer)
insert into #tqty(qty) values(@qty)
DELETE FROM #cinema_list WHERE cinema_key = @cinema_key
SET @cinema_key = NULL
SET @cinema_code = NULL
SELECT TOP 1 @cinema_key = cinema_key, @cinema_code = cinema_code FROM #cinema_list ORDER BY display_order
SET @count = @count + 1
END /*WHILE CINEMA KEY*/
total_Qty と total_sales を更新するコードは次のとおりです。
WHILE @count < @cinema_count
BEGIN
SET @count = @count+1.
SET @select = @select+'cinema'+CAST(@count AS VARCHAR)+'+'
END
SET @select = SUBSTRING(@select, 1, LEN(@select) - 1)
SET @buffer = 'UPDATE #temptable SET total_quantity = ' +cast(@qty as varchar) + ', ' + 'total_sales = '+@select
PRINT @buffer
EXEC( @buffer)
助けてください:3ありがとう:)