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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:35:16+00:00 2026-06-12T19:35:16+00:00

I have to do sum of some columns basis on where clause for better

  • 0

I have to do sum of some columns basis on where clause for better understanding i am implementing a temporary table here

declare @tbl table(a int ,b int,c int)
insert into @tbl values(1,2,3)
insert into @tbl values(2,2,3)
insert into @tbl values(1,3,1)
insert into @tbl values(1,2,3)
insert into @tbl values(1,2,3)

and for finding sum of a,b,c ob basis of value of a,b,c ; i am using following query

 SELECT (
         SELECT SUM(a) from @tbl where a=1         
         )AS a ,          
          (SELECT SUM(b) from @tbl where b=2

         )AS b ,         
          (SELECT SUM(c) from @tbl where c=3

         )AS c

I ask one of my friend to make a single line query for this work and he suggest me following lines

select sum((case  when a=1 then a  else null end)),
        sum((case  when b=2 then b  else null end)),
        sum((case  when c=3 then c  else null end))
         from @tbl

Now i am thinking about performance which will work faster if i have 27 columns and millions of records ?

or any other method to achive this which will improve performance much better than these two

  • 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-12T19:35:17+00:00Added an answer on June 12, 2026 at 7:35 pm

    Expanding on Martin’s answer – it depends on what indexes you have and how populated the column is (nullable or not). Consider this example.

    create table tbl (id int identity primary key, a int ,b int,c int, d int)
    insert tbl values(1,2,3,null)
    insert tbl values(2,null,3,1)
    insert tbl values(1,null,1,4)
    insert tbl values(1,null,3,5)
    insert tbl values(1,null,3,6)
    insert tbl select a,b,c,d from tbl --10
    insert tbl select a,b,c,d from tbl --20
    insert tbl select a,b,c,d from tbl --40
    insert tbl select a,b,c,d from tbl --80
    insert tbl select a,b,c,d from tbl --160
    insert tbl select a,b,c,d from tbl --320
    insert tbl select a,b,c,d from tbl --640
    insert tbl select a,b,c,d from tbl --1280
    insert tbl select a,b,c,d from tbl --2560
    insert tbl select a,b,c,d from tbl --5120
    insert tbl select a,b,c,d from tbl --10240
    

    Column b is created nullable and is only 20% non-null. Now, run your queries against the table (with no indexes). Before you run it, make sure to press Ctrl-M (show actual execution plan). Run both queries in the same batch, i.e. highlight the text of both queries and execute.

    SELECT (SELECT SUM(a) from tbl where a=1) AS a ,          
           (SELECT SUM(b) from tbl where b=2) AS b ,         
           (SELECT SUM(c) from tbl where c=3) AS c
    
    select sum((case  when a=1 then a  else null end)),
           sum((case  when b=2 then b  else null end)),
           sum((case  when c=3 then c  else null end))
    from tbl
    

    I won’t bore you with images here but look at the plan which will show a cost of about 75% against the top query and 25% against the bottom. That’s expected, 75%:25% = 3:1 which is due to the first query passing through the table 3 times exactly. Now create these three indexes:

    create index ix_tbl_a on tbl(a)
    create index ix_tbl_b on tbl(b)
    create index ix_tbl_c on tbl(c)
    

    Then, rerun the query batch (both together). This time, you’ll see a cost of about 51% to 49%. Quite close. The reason is because the (b) column being sparsely populated is very easy to SUM from the index pages alone. Even the other 2 columns are helped by retrieving more rows per index page than the clustered index on the data pages (which will contain all columns).

    When you expand this to 27 columns, the first form could run faster if each column is sparsely populated and if you have an index on each of the 27 columns. A big ask, and even then, it will probably only be very marginally faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have table of some items with these two columns: ItemName Price ....where
I have two table where two column are fixed. Some columns are identical and
Ok I have a table on a worksheet which has some columns for some
i have a table with 3 columns and i need to get the sum
I have a table with lots of columns, some of them with names beginning
Here's what I have: decimal sum = _myDB.Products.Sum(p => p.Price).GetValueOrDefault(); I also have two
I have a groupby and sum table that I use to display current stock
I have a table that looks like this: CREATE TABLE [dbo].[StudentTime] ([StudentTimeID] [int] NOT
I have a large R data.table with a multi column key, where some value
I have a dataframe which contains some duplicates. I want to sum rows of

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.