Matt
Matt

Reputation: 23

sql combine only some duplicates

I want to combine one set of duplicates from my table, but not all.

example:

acct    date     bal
--------------------
123   1/1/2013   40.00
123   1/1/2013   2.00
456   1/2/2013   50.00
456   1/1/2013   5.00
789   1/1/2013   10.00
789   1/1/2013   17.00

I would like to combine acct 123 to only one row, summing the balance of those rows, but leave the rest.

desired output:

acct    date     bal
--------------------
123   1/1/2013   42.00
456   1/2/2013   50.00
456   1/1/2013   5.00
789   1/1/2013   10.00
789   1/1/2013   17.00

Working in SQL Server 2005.

Upvotes: 2

Views: 114

Answers (3)

Oleksandr Fedorenko
Oleksandr Fedorenko

Reputation: 16904

Use CASE in GROUP BY clause

SELECT acct, date, SUM(bal) AS bal
FROM dbo.tets73
GROUP BY acct, date, CASE WHEN acct != 123 THEN bal END

Demo on SQLFiddle

Upvotes: 3

Muhammad Hani
Muhammad Hani

Reputation: 8664

SELECT acct, date, SUM(bal)
FROM T
WHERE acct = 123

UNION

SELECT acct, date, bal
FROM T
WHERE acct <> 123

Upvotes: 3

Satish
Satish

Reputation: 721

 select acct, date, sum(bal) from table where acct = 123
 union
 select acct, date bal from table where acct <> 123

Upvotes: 2

Related Questions