I want to filter rows from a single table between two datetimes and those filtered rows should come under a single date, for example
i want to get all the rows between (16 mar 2015 6AM) and (17 mar 2015 6AM) datetimes as (17 mar 2015) date and (17 mar 2015 6AM) and (18 mar 2015 6AM) datetimes as (18 mar 2015) date and so on.
this is my demo table
Id Name LogTime
1 mj 2015-03-16 01:28:03.257
2 mj 2015-03-16 05:28:03.257
3 mj 2015-03-16 06:28:03.257
4 mj 2015-03-16 18:28:03.257
5 mj 2015-03-17 01:28:06.677
6 mj 2015-03-17 06:28:06.677
7 mj 2015-03-17 16:28:07.460
8 mj 2015-03-17 07:28:03.257
9 mj 2015-03-18 01:28:08.193
10 mj 2015-03-18 05:28:03.257
11 mj 2015-03-18 06:28:03.257
12 mj 2015-03-18 18:28:03.257
13 mj 2015-03-19 01:28:06.677
14 mj 2015-03-19 06:28:06.677
15 mj 2015-03-19 16:28:07.460
16 mj 2015-03-19 07:28:03.257
17 mj 2015-03-20 01:28:08.193
18 mj 2015-03-20 05:28:03.257
19 mj 2015-03-20 06:28:03.257
20 mj 2015-03-20 18:28:03.257
below is the query that I am using.
DECLARE @i INT = 1
DECLARE @from DATETIME
, @to DATETIME
WHILE (@i <= 5)
BEGIN
SET @from = CONVERT(DATETIME, CONVERT(VARCHAR(10), DATEADD(D, -@i, '2015-03-20'), 102) + ' 6:00:00')
SET @to = CONVERT(DATETIME, CONVERT(VARCHAR(10), DATEADD(D, -@i + 1, '2015-03-20'), 102) + ' 6:00:00')
SELECT *, @to AS 'FetchedOn'
FROM Biometric
WHERE LogTime BETWEEN @from AND @to
ORDER BY LogTime
SET @i = @i + 1
END
that generates the following result.
Id Name LogTime FetchedOn
14 mj 2015-03-19 06:28:06.677 2015-03-20 06:00:00.000
16 mj 2015-03-19 07:28:03.257 2015-03-20 06:00:00.000
15 mj 2015-03-19 16:28:07.460 2015-03-20 06:00:00.000
17 mj 2015-03-20 01:28:08.193 2015-03-20 06:00:00.000
18 mj 2015-03-20 05:28:03.257 2015-03-20 06:00:00.000
Id Name LogTime FetchedOn
11 mj 2015-03-18 06:28:03.257 2015-03-19 06:00:00.000
12 mj 2015-03-18 18:28:03.257 2015-03-19 06:00:00.000
13 mj 2015-03-19 01:28:06.677 2015-03-19 06:00:00.000
Id Name LogTime FetchedOn
6 mj 2015-03-17 06:28:06.677 2015-03-18 06:00:00.000
8 mj 2015-03-17 07:28:03.257 2015-03-18 06:00:00.000
7 mj 2015-03-17 16:28:07.460 2015-03-18 06:00:00.000
9 mj 2015-03-18 01:28:08.193 2015-03-18 06:00:00.000
10 mj 2015-03-18 05:28:03.257 2015-03-18 06:00:00.000
Id Name LogTime FetchedOn
3 mj 2015-03-16 06:28:03.257 2015-03-17 06:00:00.000
4 mj 2015-03-16 18:28:03.257 2015-03-17 06:00:00.000
5 mj 2015-03-17 01:28:06.677 2015-03-17 06:00:00.000
Id Name LogTime FetchedOn
1 mj 2015-03-16 01:28:03.257 2015-03-16 06:00:00.000
2 mj 2015-03-16 05:28:03.257 2015-03-16 06:00:00.000
now I want to get the same result without using loop. I am using sql 2014, is there any alternate solution?
icolumn to group the results by date?