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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:27:36+00:00 2026-05-26T00:27:36+00:00

I am using TSQL to select data from the database. I want to select

  • 0

I am using TSQL to select data from the database. I want to select all data in a table but if the id is a duplicate add (sum) all column ‘a’ with same ID and make it just 1 row.

There should be no duplicate IDs in the output.

    SELECT DISTINCT id,a,b FROM dbo.test WHERE 
   id not in (select id from dbo.test) CASE a WHEN a + a?? 

example:

dbo.test
========
id  a  
1   4  
1   5 
2   3
3   2

output:

1 9 <-- two ids of 1 so column 'a' is added together.
2 3
3 2
  • 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-26T00:27:37+00:00Added an answer on May 26, 2026 at 12:27 am
    SELECT ID, SUM(a) as a, SUM(b) as B
    FROM MyTable
    GROUP BY ID
    

    This is what grouping and aggregation is designed for!

    FYI, this is how aggregation works:

    When you group and aggregate fields, you are combining records into a single row.

    In your OP, you wanted to see a SUM of A for every value of ID. That’s what the original query I posted does.

    You also want to include B, which is a varchar field, according to your comments.

    In that case, you need to decide what to do with B. Since you are grouping multiple rows together, there are (potentially) multiple values of B per value of ID. You need to either:

    • Group by B as well, which will add extra rows
    • Apply an aggregation such as MAX(), MIN(), etc. to B
    • Exclude B from the results list.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a SELECT statement in MS SQL 2005 TSQL, and no source table, just
Is it possible to get data from remote server using JUST TSQL? I mean
I am selecting from a table that has an XML column using T-SQL. I
I'm using a generic system for reporting which takes data from a database view
I want to execute multiple statements from my data access layer using C# dan
I have Database mirroring setup between two SQl 2005 servers (no witness). Using Tsql,
I have a TSQL SELECT that can return a null. I tried using ISNULL
I have a table ContentHistory in a SQL Server 2008 database with a column
I have File-Stream enabled database. I have created table with column that has the
What are the advantages and disadvanges of using the Set NOCount on TSQL for

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.