Search code examples
sqljoinmergessms

SQL: How do I combine similar value rows into one, not affecting the rest


Is there a way to merge similar values in the same column and not affect the rest, for example: 1

I want to sum Amount by Company and ID too.


Solution

  • You cannot get the data you want to display. You will be getting company name being repeated. If you want to dispaly data in the way, where company name is not repeating for subsequent rows, you have to use EXCEL or some other presentation layer tool.

    SELECT Company, ID, SUM(Amount)
    FROM Table1
    GROUP BY Company,ID
    
    
    +---------+-----+--------+
    | Company | ID  | Amount |
    +---------+-----+--------+
    | ABC     | 001 |      3 |
    | ABC     | 002 |      3 |
    | DEF     | 002 |     10 |
    | DEF     | 003 |      5 |
    +---------+-----+--------+