Consolidating Two or More Identical Rows into One

  • Hi,

    Can you please help to solve this?

    I have a table with many identical rows which has different numerical value only in once column.

    I want these two or more identical rows to be selected as only one row with the sum of all different numerical values.

    Example:-

    Row1: A B 4 C D

    Row2: A B 5 C D

    Output: A B 9 C D

    Thanks,

    Sandeep

  • ssandeepksh (12/5/2014)


    Hi,

    Can you please help to solve this?

    I have a table with many identical rows which has different numerical value only in once column.

    I want these two or more identical rows to be selected as only one row with the sum of all different numerical values.

    Example:-

    Row1: A B 4 C D

    Row2: A B 5 C D

    Output: A B 9 C D

    Thanks,

    Sandeep

    This has to be homework since you would learn about aggregates in your first few weeks or even days of using SQL. So here's a suggestion to help you, as a full answer would teach you nothing at all.

    Try GROUP BY and SUM().

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • SELECT column_A,

    column_B,

    SUM(Column_4)

    column_C,

    column_D

    FROM dbo.Test_Table

    GROUP BY column_A,

    column_B,

    column_C,

    column_D

  • I have been moron last week and asked such a silly question. Thanks for your help ๐Ÿ™‚

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply