sql server - SQL Over partition by -
i have case statement displays sum of profit , month date total each person. idea want display daily figure of person whole month total altogether.
my issue when limit results yesterday (supposed daily figure) effects calculation of month value (just calculates sum day rather whole month).
this because total month values out of scope of query. there anyway calculate whole month value each person correctly without having limits of effecting result.
e.g. result:
08/09/17: 25 09/09/17: 25 10/09/17: 25 11/09/17: 25 <<<< display 1 day , month total overall month total: 100 can includes nulls too? think im looking @ dynamically stored month date value isn't effected clauses.
select sum(figure) 'daily figure', case when month([date]) = month(getdate()) , year([date]) = year(getdate()) sum(figure) on (partition [name], month([date])) else 0 end [month date total] dateadd(day,datediff(day,1,getdate()),0)
if want month-to-date , current amount, use conditional aggregation:
select name, sum(case when day(date) = day(getdate()) - 1 figure else 0 end) dailyfigure, sum(figure) monthtodate month([date]) = month(getdate()) , year([date]) = year(getdate()) group name; this works on first day of month.
Comments
Post a Comment