1

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.

DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
DoinMyBest
  • 13
  • 3

2 Answers2

3

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
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
0

Try this

SELECT datecolumn,SUM(number) AS total FROM tablename GROUP BY DATE(datecolumn)
Keval Pithva
  • 600
  • 2
  • 5
  • 21