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 8268769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:00:29+00:00 2026-06-08T06:00:29+00:00

I’m using SQL Server 2008 R2 and I have a database which contains two

  • 0

I’m using SQL Server 2008 R2 and I have a database which contains two tables.

T1 contains raw data, however only the column Cost, Date and AccId is interesting for me.

T2 contains a few columns like Jan, Feb, Mar, Apr, … and a column AccId

I need now a stored procedure which sets the values in the appropriate column in T2 whenever I update T1. It should sum up the Cost for every AccId in the given month. If I set @year = 2012 and @month = 3, so it should update the column Mar with the sum of Cost in T2 for every AccId, which is in T1 and T2.

I have now a stored procedure which would work for one constant column. I want now CASE which selects the column depending on the given month. Here is a stored procedure how I think it should work, but it doesn’t work:

CREATE PROCEDURE [dbo].[SP_Calc]
    @year int,
    @month int
AS

    MERGE INTO T2
    USING (
            SELECT AccId, SUM(Cost) AS CostSum
            FROM T1
            WHERE YEAR(Date) = @year
            AND MONTH(Date) = @month
            GROUP BY AccId
         ) AS source
      ON T2.AccID = source.AccId
    WHEN MATCHED THEN
    UPDATE SET 
     SELECT 
       CASE @month 
        WHEN 1 THEN Jan = source.Cost
        WHEN 2 THEN Feb = source.Cost
        WHEN 3 THEN Mar = source.Cost
        WHEN 4 THEN Apr = source.Cost
        WHEN 5 THEN Mai = source.Cost
        WHEN 6 THEN Jun = source.Cost
        WHEN 7 THEN Jul = source.Cost
        WHEN 8 THEN Aug = source.Cost
        WHEN 9 THEN Sep = source.Cost
        WHEN 10 THEN Oct = source.Cost
        WHEN 11 THEN Nov = source.Cost
        WHEN 12 THEN Dez = source.Cost
        ELSE RAISERROR ('Month is out of range', 18, 1); 
    END
  • 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-08T06:00:33+00:00Added an answer on June 8, 2026 at 6:00 am

    You don’t have a Cost in your Source, just CostSum

    Also you need to change your setting to

    SET
       Jan = CASE @month  WHEN 1 THEN source.CostSum else T2.Jan end,
       Feb = CASE @month  WHEN 2 THEN source.CostSum else T2.Feb end,
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.