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

  • Home
  • SEARCH
  • 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 81593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:29:03+00:00 2026-05-10T21:29:03+00:00

My SQL is rusty — I have a simple requirement to calculate the sum

  • 0

My SQL is rusty — I have a simple requirement to calculate the sum of the greater of two column values:

CREATE TABLE [dbo].[Test] (     column1 int NOT NULL,      column2 int NOT NULL );  insert into Test (column1, column2) values (2,3) insert into Test (column1, column2) values (6,3) insert into Test (column1, column2) values (4,6) insert into Test (column1, column2) values (9,1) insert into Test (column1, column2) values (5,8) 

In the absence of the GREATEST function in SQL Server, I can get the larger of the two columns with this:

select column1, column2, (select max(c)                              from (select column1 as c                                    union all                                   select column2) as cs) Greatest   from test 

And I was hoping that I could simply sum them thus:

select sum((select max(c)                from (select column1 as c                      union all                     select column2) as cs))   from test   

But no dice:

Msg 130, Level 15, State 1, Line 7 Cannot perform an aggregate function on an expression containing an aggregate or a subquery. 

Is this possible in T-SQL without resorting to a procedure/temp table?

UPDATE: Eran, thanks – I used this approach. My final expression is a little more complicated, however, and I’m wondering about performance in this case:

SUM(CASE WHEN ABS(column1 * column2) > ABS(column3 * column4)          THEN column5 * ABS(column1 * column2) * column6          ELSE column5 * ABS(column3 * column4) * column6 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. 2026-05-10T21:29:04+00:00Added an answer on May 10, 2026 at 9:29 pm

    Try this:

     SELECT SUM(CASE WHEN column1 > column2                   THEN column1                   ELSE column2 END)   FROM test 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel totally stupid. I'm rusty with my sql. I have two tables, Message
I need help with SQL. I have an sqlite table like so; CREATE TABLE
Bit rusty on the sql side as I have not touched in a while
Sql server 2008 R2 I have two queries DECLARE @title NVARCHAR(500) = 'Finite AND
SQL Server 2008 R2: normally, we create our table, and stored procedure, and grant
I've got a problem with my SQL. I have a table (named Regions) looking
I have the following Oracle PL/SQL codes that may be rusty from you guys
Bit rusty in sql I have a situation where I need to insert a
Here is a simple situation. (My apologies my SQL is quite rusty.) (I am
My SQL is a bit rusty -- is there a SQL way to project

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.