• HanShi (7/15/2013)


    This code will give you the extra column with the first day of the month. I've calculated this with DATEADD combined with the DATEPART of [TimeStamp] and the 0 timevalue (= '01-01-1900'). You probably need to alter the WHERE clause, because I now only select the last three monts from now.

    select

    MeterId

    , count(MeterID) as 'Count'

    , sum(WattHours) as 'WattHours'

    , dateadd(month, month(timestamp), dateadd(year, year(timestamp)-1900, 0)) as 'MeasuredMonth'

    from EnergyMeasure

    where

    timestamp > dateadd(month, -3, getdate())

    group by

    MeterID

    , dateadd(month, month(timestamp), dateadd(year, year(timestamp)-1900, 0))

    Thank you so much 😀