'Simple SQL select sum one time and values of same column
I have create this query:
SELECT table1.id, b.sum
FROM table1
CROSS JOIN (SELECT SUM(id) sum
FROM table1) b
ORDER BY id DESC;
But this produces results like this:
| id | sum |
|---|---|
| 3 | 6 |
| 2 | 6 |
| 1 | 6 |
Sum value print only one time. Can you help me.
But I want this result :
| id | sum |
|---|---|
| 3 | 6 |
| 2 | |
| 1 |
Solution 1:[1]
This should do it:
select
id,
CASE WHEN id=(max(id) over())
THEN sum(id) over (order by id) END as 'sum'
from cte1
order by id desc;
more info see: Window Function Concepts and Syntax
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Luuk |
