Is a SQL function exists to SUM some numbers in a column but only if they are different ?
Asked
Active
Viewed 58 times
0
-
No, but you can use the `case` command. Checkout this answer http://stackoverflow.com/a/5487936/1542307 – Karan Shah Jan 15 '17 at 03:16
-
Like `sum(distinct foo)`? – melpomene Jan 15 '17 at 03:17
-
1`in a row`? do you mean you want to add different columns? Can you show us some sample data and expected output? – Gurwinder Singh Jan 15 '17 at 03:41
-
You can improve this question by adding sample data, expected result and the query you have tried so far. Importantly tag the `RDBMS` you are using – Pரதீப் Jan 15 '17 at 04:31
2 Answers
4
You can use subqueries to achive this:
Select Sum(TBL.column) From
( Select distinct column From Table) as TBL

Hadi
- 36,233
- 13
- 65
- 124
1
SELECT SUM(DISTINCT(COLUMN_NAME)) FROM TABLE
-
Can I make : `SELECT *, SUM(DISTINCT COALESCE(STX_Amount, 0))) AS ApplicableTaxes FROM...`? – Jan 15 '17 at 04:13
-
@user7347588 - Can you add sample data and expected result. Also the `RDBMS` you are using – Pரதீப் Jan 15 '17 at 04:23
-
The parentheses around the column_name are meaningless and are not related in any way to the DISTINCT keyword – David דודו Markovitz Jan 15 '17 at 07:24