私は最近転職したため、オラクルからSQLサーバーに切り替えました。オンデマンドでレポートを生成するストアド プロシージャを作成しようとしています。レポートには、毎月の金額とこれらの金額の合計が表示されます。私は主にこれの合計部分に問題があります。完全なクエリは、この投稿の一番下にあります。私の非常に長いクエリからわかるように、Total_Current_Plus_Archived 列の合計金額を取得するために、基本的に 2 回書きました。これにはローカル変数 (@totalReportSum) を使用することを考えていました。合計列が計算されるたびにこの変数を更新したかったのですが、使用する構文に関係なくエラーが発生しました。ここに私が試したことのいくつかがあります:
試行 1。
--March total
(SELECT @totalReportSum =@totalReportSum + Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-03-01' )
AND ( ai.crash_date <= @year + '-03-31' ))
AS March_Total_Reports,
試行 2。
--March total
@totalReportSum = (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-03-01' )
AND ( ai.crash_date <= @year + '-03-31' ))
AS March_Total_Reports,
可能であれば、うるう年をどのように説明できるかについての提案もお願いします。
ヘルプやヒントは大歓迎です。ありがとう。
CREATE PROCEDURE Sp_get_ram_report @year NCHAR(4)
AS
BEGIN--declare variable to hold total sum of reports
DECLARE @totalReportSum INT;
--initialize sum
SET @totalReportSum = 0;
END
SELECT TOP (100) PERCENT r.ramid
AS Ram_ID,
r.ram_fname
AS RAM_First_Name,
r.ram_lname
AS RAM_Last_Name,
Count(*)
AS Number_of_Agencies,
Isnull(Sum(m.yearly_avg_reports), 0)
AS Yearly_Avg#_of_reports,
Round(Isnull(Sum(m.yearly_avg_reports), 0) / 12, 2)
AS Monthly_Reports_Expected,
--January
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-01-01' )
AND ( ai.crash_date <= @year + '-01-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS January,
--January total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-01-01' )
AND ( ai.crash_date <= @year + '-01-31' ))
AS January_Total_Reports,
--February
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-02-01' )
AND ( ai.crash_date <= @year + '-02-28' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS February,
--February total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-02-01' )
AND ( ai.crash_date <= @year + '-02-28' ))
AS February_Total_Reports,
--March
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-03-01' )
AND ( ai.crash_date <= @year + '-03-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS March,
--March total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-03-01' )
AND ( ai.crash_date <= @year + '-03-31' ))
AS March_Total_Reports,
--April
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-04-01' )
AND ( ai.crash_date <= @year + '-04-30' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS April,
--April total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-04-01' )
AND ( ai.crash_date <= @year + '-04-30' ))
AS April_Total_Reports,
--May
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-05-01' )
AND ( ai.crash_date <= @year + '-05-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS May,
--May total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-05-01' )
AND ( ai.crash_date <= @year + '-05-31' ))
AS May_Total_Reports,
--June
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-06-01' )
AND ( ai.crash_date <= @year + '-06-30' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS June,
--June total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-06-01' )
AND ( ai.crash_date <= @year + '-06-30' ))
AS June_Total_Reports,
--July
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-07-01' )
AND ( ai.crash_date <= @year + '-07-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS July,
--July total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-07-01' )
AND ( ai.crash_date <= @year + '-07-31' ))
AS July_Total_Reports,
--august
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-08-01' )
AND ( ai.crash_date <= @year + '-08-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS August,
--august total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-08-01' )
AND ( ai.crash_date <= @year + '-08-31' )),
--september
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-09-01' )
AND ( ai.crash_date <= @year + '-09-30' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS September,
--September Total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-09-01' )
AND ( ai.crash_date <= @year + '-09-30' ))
AS September_Total_Reports,
--October
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-10-01' )
AND ( ai.crash_date <= @year + '-10-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS October,
--october total
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-10-01' )
AND ( ai.crash_date <= @year + '-10-31' ))
AS October_Total_Reports,
--november
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-11-01' )
AND ( ai.crash_date <= @year + '-11-30' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS November,
--november total
(SELECT Count(*)
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-11-01' )
AND ( ai.crash_date <= @year + '-11-30' ))
AS November_Total_Reports,
--December
(SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-12-01' )
AND ( ai.crash_date <= @year + '-12-31' )
AND ( ai.insert_datetime - ai.crash_date < 35 )
) AS December,
--December Total
(SELECT Count(*)
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-12-01' )
AND ( ai.crash_date <= @year + '-12-31' ))
AS December_Total_Reports,
--Total Current +archived: Would like to replace this huge chunk w/ something a lot smaller
( (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-01-01' )
AND ( ai.crash_date <= @year + '-01-31' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-02-01' )
AND ( ai.crash_date <= @year + '-02-28' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-03-01' )
AND ( ai.crash_date <= @year + '-03-31' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-04-01' )
AND ( ai.crash_date <= @year + '-04-30' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-05-01' )
AND ( ai.crash_date <= @year + '-05-31' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-06-01' )
AND ( ai.crash_date <= @year + '-06-30' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-07-01' )
AND ( ai.crash_date <= @year + '-07-31' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-08-01' )
AND ( ai.crash_date <= @year + '-08-31' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-09-01' )
AND ( ai.crash_date <= @year + '-09-30' ))
+ (SELECT Count(*) AS Expr1
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-10-01' )
AND ( ai.crash_date <= @year + '-10-31' ))
+ (SELECT Count(*)
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-11-01' )
AND ( ai.crash_date <= @year + '-11-30' ))
+ (SELECT Count(*)
FROM dbo.accident_information AS ai
INNER JOIN dbo.municipality AS mx
ON mx.agency_ori = ai.agency_ori
WHERE ( mx.ram = m.ram )
AND ( ai.crash_date >= @year + '-12-01' )
AND ( ai.crash_date <= @year + '-12-31' ))
) AS
Total_Current_Plus_Archived
FROM dbo.municipality AS m
INNER JOIN dbo.ram AS r
ON m.ram = r.ramid
GROUP BY m.ram,
r.ramid,
r.ram_fname,
r.ram_lname
ORDER BY ram_last_name