Thursday, January 30, 2014

FIll in missing months

try this query

alter PROCEDURE dbo.UM_getmonthlymiles
-- Add the parameters for the stored procedure here
@YEAR INT , @RIDER INT

AS
BEGIN
declare @months table
(
monthnum int
--, mnthname varchar(3)
)
insert into @months

select MonthInt
from (
select 1 as MonthInt union all select 2 union all select 3 union all select 4 union all
select 5 as MonthInt union all select 6 union all select 7 union all select 8 union all
select 9 as MonthInt union all select 10 union all select 11 union all select 12
)M

--select* from @months

declare @mnthmiles table
(
distance float
, riderid int
--, mnthname varchar(10)
, monthnumber int
)


select isnull(sum(mi.distancemiles),0), @RIDER, isnull(month(mi.ridedate),m.monthnum) MonthNum
from mileagelog mi
RIGHT JOIN @months M
ON M.monthnum = month(ridedate)
and isnull(riderid ,@rider)= @rider

group by DATEPART(mm,ridedate), riderid, DATENAME(mm,ridedate), DATEPART(mm,ridedate), M.monthnum
order by DATEPART(mm,ridedate), M.monthnum
end








Satheesh

My Blog



No comments:

Post a Comment