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 9271535

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:38:35+00:00 2026-06-18T15:38:35+00:00

UPDATE Houses SET lStatus = U.codesum FROM Houses H JOIN (SELECT ref, SUM(code) AS

  • 0
UPDATE Houses 
SET    lStatus = U.codesum
FROM   Houses H
JOIN   (SELECT ref, SUM(code) AS codesum
    FROM   Users
    GROUP  BY ref) AS U ON U.ref = H.ref

The above code gets all users for every house (Houses table). SUMs the code column (Users table) for all users. And finally updates the result in lstatus column of the houses table.

My question is:
I need to rewrite the query which is NOT to sum the code column. Rather I want to create case statements. for example:

tempvar = 0    //local variable might be required

For each user {

If code == 1 then tempvar += 5

else if code == 2 then tempvar += 10

etc

tempvar = 0;
}

Once we have looped through all the users for each house we can now set lStatus = tempvar.
The tempvar should then be reset to 0 for the next house.

  • 0 0 Answers
  • 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-18T15:38:36+00:00Added an answer on June 18, 2026 at 3:38 pm

    You should try to avoid loops and other procedural constructs when coding SQL. A relational database can’t easily optimize such things and they often perform orders of magnitude slower than their declarative counterparts. In this case, it seems simple enough to replace your SUM(code) with the CASE statement that you describe:

    UPDATE Houses 
    SET    lStatus = U.codesum
    FROM   Houses H
    JOIN   (SELECT ref, SUM(CASE code WHEN 1 THEN 5 WHEN 2 THEN 10 ELSE 0 END) AS codesum
        FROM   Users
        GROUP  BY ref) AS U ON U.ref = H.ref
    

    In this way, SUM can still handle the duty that you imagine your temp variable would be doing.

    Also, if you have many cases, you might think about putting those in a table and simply joining on that to get your sum. This might be better to maintain. I’m using a table variable here, but it could look like the following:

    DECLARE @codes TABLE (
        code INT NOT NULL PRIMARY KEY,
        value INT NOT NULL
    )
    INSERT INTO @codes SELECT 1, 5
    INSERT INTO @codes SELECT 2, 10
    
    UPDATE Houses 
    SET    lStatus = U.codesum
    FROM   Houses H
    JOIN   (SELECT a.ref, SUM(b.value) AS codesum
        FROM   Users a
        JOIN   @codes b on a.code = b.code -- Here we get the values dynamically
        GROUP  BY a.ref) AS U ON U.ref = H.ref
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE UPI_ATTRIBUTE SET SITE_INC ='0' WHERE USER_PROFILING_NAME IN ('CAR_IMPLICIT','CAR_EXPLICIT') Above is my query that
Update : This is no longer an issue from C# 6, which has introduced
If a given web server can only read cookies from those set within its
I have a form in which i use javascript to set the select element
Update: I have tried Sheldon's code below but I have struck a couple of
I have a Class Library project which houses some shared code between other projects
Update: Just need to return a key from a dictionary, the dictionary is created
Update: Is there a way to achieve what I'm trying to do in an
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Update: I reported this as a bug to Apple and they fixed it! All

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.