Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8534303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:09:47+00:00 2026-06-11T10:09:47+00:00

I recently switched jobs and therefore also switched from oracle to sql server. I’m

  • 0

I recently switched jobs and therefore also switched from oracle to sql server. I’m attempting to write a stored procedure that will generate a report on demand. The report will show monthly amounts and the sum of these amounts. I am mostly having trouble with the summing part of this. The full query is at the very bottom of this post.
As you will see from my super-long query, I’ve basically written it twice to get the sum amount in the Total_Current_Plus_Archived column. I was thinking of using a local variable for this (@totalReportSum). I wanted to update this variable each time a total column is calculated but get errors no matter what syntax I use. Here are a couple of the things I’ve tried:

Attempt 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, 

Attempt 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, 

If possible, I would also like suggestions as to how I can account for leap years.

Any help or hints are greatly appreciated. Thanks.

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:09:49+00:00Added an answer on June 11, 2026 at 10:09 am

    Just an idea, I would like query this way. Then, you don’t need care about leap years

    ;WITH monthly AS
    (
        SELECT 
            mx.ram 
            ,DATEPART(month,crash_date) AS report_month
            ,SUM(CASE WHEN ai.insert_datetime - ai.crash_date < 35 THEN 1 ELSE 0 END) AS Expr1
            ,Count(*) AS Expr1_total
        FROM   dbo.accident_information AS ai 
        INNER JOIN dbo.municipality AS mx 
            ON mx.agency_ori = ai.agency_ori 
        WHERE DATEPART(year,crash_date) = @year             
        GROUP BY mx.ram,DATEPART(month,crash_date)
    )                 
    SELECT
        r.ramid AS Ram_ID, 
        r.ram_fname AS RAM_First_Name,
        ...
        ,(SELECT Expr1 FROM monthly mt WHERE mt.ram = m.ram AND report_month = 1) AS January
        ,(SELECT Expr1_total FROM monthly mt WHERE mt.ram = m.ram AND report_month = 1) AS  January_Total_Reports
        ,(SELECT Expr1 FROM monthly mt WHERE  mt.ram = m.ram AND report_month = 2) AS February
        ,(SELECT Expr1_total FROM monthly mt WHERE mt.ram = m.ram AND report_month = 2) AS  February_Total_Reports
        ...
        ,(SELECT SUM(Expr1_total) FROM monthly mt WHERE mt.ram = m.ram) 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         
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently switched from using Linq to Sql to the Entity Framework. One of
We recently switched from SVN to Git and re-configured our ccnet server. Everything is
I recently switched over to C# from vb.NET and within visual studio found that
We recently switched a database from MSSQL to MySQL and the queries that uses
I recently switched from eclipse and netbeans to intellij, but I have also liferay
I recently switched to a new setup that is reporting PHP Notices; my code
I recently switched from a Java based project to a C#/.net project. I previously
We recently switched from SVN to Git and we're still kind of in the
I've recently switched focus from VB.NET to C# due to some decisions at work.
Recently switched to XFBML to make our page more efficient. However, we noticed that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.