I have a query which calculate values per year/months from table "mesure" like this
select Annee, Mois
from
(
select annee, Mois
-- computing her...
round(cast(SUM(nbDates) * 100 as float) / sum(NbDatesTheoriques),2) as DispoBrute ie....
from (
select
DATEPART(YEAR, DateHeureMesure) as annee,
DATEPART(month, dateheuremesure) as Mois
--TypeMesure,HauteurMesure, count(dateheuremesure) as nbDates,
--SUM(CASE WHEN ValideeMesure = 2 THEN 1 ELSE 0 END) AS NbDatesValides
from mesure
where Id = 378
group by DATEPART(YEAR, DateHeureMesure), DATEPART(month, dateheuremesure)
) req
) req2
group by annee, mois--, mo.MonthNumber
order by annee, mois --min(datedebut)
I simplify the query (remove part of subquery req2 / req...) and the result is :
Year Month DispoBrute
2013 8 156
2013 9 1254
2013 10 121
2013 11 2121
2013 12 4500
2014 1 155
2014 2 200
2014 3
2014 4
2014 5
Ok but how can i have all the months on the current year like this ?
Year Month DispoBrute
2013 1 NULL
2013 2 NULL
2013 3 NULL
2013 4 NULL
2013 5 NULL
2013 6 NULL
2013 7 NULL
2013 8 156
2013 9 1254
2013 10 121
2013 11 ... ...2121
2013 12 .... ie
2014 1
2014 2
2014 3
2014 4
2014 5
Thanks a lot !
right join
(orleft join
if you start from the tally table) with, the tally table will only have numbers 1-12 (for the months). You can join on month number.