2

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.

MuhammadHani
  • 7,729
  • 4
  • 26
  • 44
Matt
  • 23
  • 5

3 Answers3

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

UNION

SELECT acct, date, bal
FROM T
WHERE acct <> 123
MuhammadHani
  • 7,729
  • 4
  • 26
  • 44
  • and Alex whatever you said is true. That was one ridiculous piece of code... +1, simplicity pays off most of the time as the opposite likely to miss the obvious ;) – bonCodigo Jan 16 '13 at 07:03
3

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

Aleksandr Fedorenko
  • 15,209
  • 6
  • 35
  • 42
2
 select acct, date, sum(bal) from table where acct = 123
 union
 select acct, date bal from table where acct <> 123
Satish
  • 693
  • 1
  • 5
  • 16