Reputation: 13
If I have a database with two columns, Date
and a Number
, with multiple Number
entries per Date
.
How can I add a column that shows the total Number
for the Date
listed?
So if I have four entries for 05/09/2017, with values 2, 4, 3, 8, all four entries for that day should also have a column showing 17.
Upvotes: 1
Views: 93
Reputation: 610
Try this
SELECT datecolumn,SUM(number) AS total FROM tablename GROUP BY DATE(datecolumn)
Upvotes: 0
Reputation: 49260
If you are on a SQL Server version 2012 and above, use the SUM
window function.
select date,number,sum(number) over(partition by date) as total
from tablename
Otherwise use
select t1.date,t1.number,t2.total
from tablename t1
join (select date,sum(number) as total from tablename group by date) t2 on t1.date=t2.date
Upvotes: 3