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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:49:46+00:00 2026-05-27T15:49:46+00:00

I need to sum some rows applying on each row a formula and after

  • 0

I need to sum some rows applying on each row a formula and after all sum in a last column all the results. I’d like that in the total column I didn’t have to especify the previous formulas but use the name of the column, I mean something like the following code (which doesn’t work because total_1, total_2 and total_3 are not recognized)

SELECT 
    t.id, 
    t.dpt, 
    sum(t1.coefficient * t1.value) AS total_1,
    sum(t2.coefficient * t2.value) AS total_2,
    sum(t3.coefficient * t3.value) AS total_3,
    (total_1 + total_2 + total_3) AS total -- Not recognized
FROM my table t
JOIN table1 t1 ON...
JOIN table2 t2 ON...
JOIN table3 t3 ON...
group by t.id, t.dpt

I know I could do something like thefollowing, but it emplies some redundancy in the definitions of the sums.

SELECT 
    t.id, 
    t.dpt, 
    sum(t1.coefficient * t1.value) AS total_1,
    sum(t2.coefficient * t2.value) AS total_2,
    sum(t3.coefficient * t3.value) AS total_3,
    sum(t1.coefficient * t1.value + t2.coefficient * t2.value + t3.coefficient * t3.value) AS total
FROM my table t
JOIN table1 t1 ON...
JOIN table2 t2 ON...
JOIN table3 t3 ON...
group by t.id, t.dpt

Is there any efficient query which can be helpful to avoid redefining the columns sums in the total column?

Thanks

  • 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-05-27T15:49:46+00:00Added an answer on May 27, 2026 at 3:49 pm

    The simplest way I think of is:

    SELECT *, (total_1 + total_2 + total_3) AS total FROM (
        SELECT 
            t.id, 
            t.dpt, 
            sum(t1.coefficient * t1.value) AS total_1,
            sum(t2.coefficient * t2.value) AS total_2,
            sum(t3.coefficient * t3.value) AS total_3
        FROM my table t
        JOIN table1 t1 ON...
        JOIN table2 t2 ON...
        JOIN table3 t3 ON...
        GROUP BY t.id, t.dpt
    ) t
    

    Added:

    I’ve tested queries like bellow (I know that they differ, but you should test in on your own it’s the best way to spot the difference). Before each run I’ve executed DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE. 2nd run were executed after few minutes and the difference is due to the server load probably.

    SET STATISTICS TIME ON
    
    DBCC DROPCLEANBUFFERS
    DBCC FREEPROCCACHE
    go
    
    select t1.col1, sum(i.col2) as s, sum(i.col2) * 4 as smult
    from tab1 t1
    join tab2 t2 on t1.col1 = t2.col1
    group by t1.col1
    
    DBCC DROPCLEANBUFFERS
    DBCC FREEPROCCACHE
    go
    
    select *, s * 4 as smult from (
        select t1.col1, sum(i.col2) as s
        from tab1 t1
        join tab2 t2 on t1.col1 = t2.col1
        group by t1.col1
    ) t
    

    1st query 1st run:

    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 516 ms.
    SQL Server Execution Times:
       CPU time = 63 ms,  elapsed time = 3958 ms.
    

    1st query 2nd run:

    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 374 ms.
    SQL Server Execution Times:
       CPU time = 31 ms,  elapsed time = 4152 ms.
    

    2nd query 1st run:

    SQL Server parse and compile time: 
       CPU time = 0 ms, elapsed time = 513 ms.
    SQL Server Execution Times:
       CPU time = 94 ms,  elapsed time = 3445 ms.
    

    2nd query 2nd run:

    SQL Server parse and compile time: 
       CPU time = 16 ms, elapsed time = 363 ms.
    SQL Server Execution Times:
       CPU time = 125 ms,  elapsed time = 3146 ms.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data with start and stop date that I need to sum.
I found some really good sum row, sum column of drop down examples, but
I'm trying to display all rows from one table and also SUM/AVG the results
I have one field that I need to sum lets say named items However
I need sphinx to sort the results by the sum of an attribute. I
I've a need to add method that will calculate a weighted sum of worker
I need an algorithm that identifies all possible combinations of a set of numbers
I need some help to build SQL Query. I have table having data like:
I have some configuration data that I'd like to model in code as so:
I have some data that looks like this: +---+--------+-------------+---------------+--------------+ | | A | B

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.