SQL Server : how to group values by month interval with offset -
i have query groups aggregated sum values month.
this query:
declare @isbystatus bit set @isbystatus = 0 select cast((datediff(month, '2012-01-01t06:00:00', dattimestamp)) int) [index] , min(dattimestamp) [from], max(dattimestamp) [to], sum(case cast(intio_id nvarchar(100)) when n'284' value else null end) [286] [iovaluesfn](@isbystatus) iovalues dattimestamp >= '2012-01-01t06:00:00' , dattimestamp < '2013-01-01t05:59:59' , intio_id in (284) group cast ((datediff(month,'2012-01-01t06:00:00', dattimestamp)) int) order [from]
and result:
index 286 0 2012-01-07 07:00:00.000 2012-01-31 23:00:00.000 142579.898864746 1 2012-02-01 00:00:00.000 2012-02-29 23:00:00.000 139486.498001099 2 2012-03-01 00:00:00.000 2012-03-31 23:00:00.000 99516.3022232056 3 2012-04-01 00:00:00.000 2012-04-30 23:00:00.000 84597.599899292 4 2012-05-01 00:00:00.000 2012-05-31 23:00:00.000 67085.2983112335 5 2012-06-01 00:00:00.000 2012-06-30 23:00:00.000 67768.9982643127 6 2012-07-01 00:00:00.000 2012-07-31 23:00:00.000 121100.264842987 7 2012-08-01 00:00:00.000 2012-08-31 23:00:00.000 165768.90776825 8 2012-09-01 00:00:00.000 2012-09-30 23:00:00.000 97441.7333068848 9 2012-10-01 00:00:00.000 2012-10-31 23:00:00.000 153764.736312866 10 2012-11-01 00:00:00.000 2012-11-30 23:00:00.000 153601.413961411 11 2012-12-01 00:00:00.000 2012-12-31 23:00:00.000 142521.07028389 12 2013-01-01 00:00:00.000 2013-01-01 05:00:00.000 1192.32000732422
now want similar logic, insert offset in month start-end time.
e.g. first period start on january 1'st on 11:00 , end @ february 1 10:59:59 am.
same goes each subsequent month.
thanks in advance help, omer
have @ example below. trick add negative amount of offset such hour prior 11am on first day of month "pushed" prior month.
schema setup:
create function iovaluesfn(@isbystatus bit) returns table return select dattimestamp = '20130101 10:50', intio_id = 284, value = 1 union select '20130101 11:00', 284, 1 union select '20130102 11:00', 284, 2 union select '20130301 11:00', 284, 3 union select '20130401 11:00', 284, 4 union select '20120501 11:00', 284, 5 union select '20120601 11:00', 284, 6 union select '20120101 11:00', 284, 7 union select '20120102 11:00', 284, 8 union select '20120101 11:01', 284, 9 union select '20120101 10:59', 284,10 union -- ** value counted in dec 2011 select '20120101 11:00', 284,11 union select '20120101 11:01', 281,12 union select '20120101 10:59', 281,13 union select '20120101 11:00', 281,14 go
query:
declare @isbystatus bit; set @isbystatus = 0; ;with iovalues ( select dateadd(hour, -11, dattimestamp) dattimestamp, intio_id, value [iovaluesfn](@isbystatus) dattimestamp >= '2012-01-01t06:00:00' , dattimestamp < '2013-01-01t05:59:59' , intio_id in (284) ) select cast((datediff(month,'2012-01-01t06:00:00',dattimestamp)) int) [index], min(dattimestamp) [from], max(dattimestamp) [to], sum(case cast(intio_id nvarchar(100)) when n'284' value else null end) [286] iovalues group cast ((datediff(month,'2012-01-01t06:00:00',dattimestamp))as int) order [from];
results:
| index | | | 286 | ---------------------------------------------------------- | -1 | december, 31 2011 | december, 31 2011 | 10*** | | 0 | january, 01 2012 | january, 02 2012 | 35 | | 4 | may, 01 2012 | may, 01 2012 | 5 | | 5 | june, 01 2012 | june, 01 2012 | 6 |