Truncate timestamp to arbitrary intervals

Consider this demo to bring timestamps down to a resolution of 15 minutes and aggregate resulting dupes: WITH tbl(id, ts) AS ( VALUES (1::int, ‘2012-10-04 00:00:00’::timestamp) ,(2, ‘2012-10-04 18:23:01’) ,(3, ‘2012-10-04 18:30:00’) ,(4, ‘2012-10-04 18:52:33’) ,(5, ‘2012-10-04 18:55:01’) ,(6, ‘2012-10-04 18:59:59’) ,(7, ‘2012-10-05 11:01:01’) ) SELECT to_timestamp((extract(epoch FROM ts)::bigint / 900)*900)::timestamp AS lower_bound , to_timestamp(avg(extract(epoch … Read more

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

You can try the following: * Updated ** — Test Data DECLARE @YourTable TABLE(Product INT, Timestamp DATETIME, Price NUMERIC(16,4)) INSERT INTO @YourTable SELECT 5678, ‘20080101 12:00:00’, 12.34 UNION ALL SELECT 5678, ‘20080101 12:01:00’, NULL UNION ALL SELECT 5678, ‘20080101 12:02:00’, NULL UNION ALL SELECT 5678, ‘20080101 12:03:00’, 23.45 UNION ALL SELECT 5678, ‘20080101 12:04:00’, NULL … Read more

How to combine aggregates within a group with aggregates across groups within SSRS

I’ll answer my own question. From within any expression, it’s possible to perform lookups in all datasets. Through this way we’ll get the data: LookupSet(SourceFieldToCompare, TargetFieldToCompare, ResultField, DataSet) Now, let’s raise the bar for the question and say the data is grouped in yet another dimension, months – like this: Category | January | February … Read more

Using GROUP BY with FIRST_VALUE and LAST_VALUE

SELECT MIN(MinuteBar) AS MinuteBar5, Opening, MAX(High) AS High, MIN(Low) AS Low, Closing, Interval FROM ( SELECT FIRST_VALUE([Open]) OVER (PARTITION BY DATEDIFF(MINUTE, ‘2015-01-01 00:00:00’, MinuteBar) / 5 ORDER BY MinuteBar) AS Opening, FIRST_VALUE([Close]) OVER (PARTITION BY DATEDIFF(MINUTE, ‘2015-01-01 00:00:00’, MinuteBar) / 5 ORDER BY MinuteBar DESC) AS Closing, DATEDIFF(MINUTE, ‘2015-01-01 00:00:00’, MinuteBar) / 5 AS Interval, … Read more