gaps-and-islands

SQL Query for In/Out time attendance with null row when date not in table1

梦想的初衷 提交于 2019-12-08 00:12:02
问题 I have a two table with the below sample output. tb1 is in time and tb2 is out time I want to build a query to achieve the below results with null values if row in tb2 not in tb2 or row in tb1 not in tb1: i need get null if OINDX in table2 and not in table1 or IINDX in table1 and not in table2 and if date not in dates between two date get null row with date only like this photo this code to create two tables to try code and help me CREATE TABLE [dbo].[TIMEIN]( [IINDX] [int] NULL, [USERID]

Sql - Merging rows if date connects

跟風遠走 提交于 2019-12-07 14:12:02
问题 I have table with rows: clientid, startdate and enddate. Date cant overlap for same clientid. I would like to merge rows for every client if date connects. table looks like this: clientid startdate enddate 1 10.10.2017 12.10.2017 1 12.10.2017 13.10.2017 1 13.10.2017 17.10.2017 1 10.11.2017 17.11.2017 1 17.11.2017 23.11.2017 1 12.12.2017 14.12.2017 2 10.11.2017 15.11.2017 2 01.12.2017 02.12.2017 2 02.12.2017 05.12.2017 Final table should looks like this: clientid startdate enddate 1 10.10.2017

Grouping Events in Postgres

戏子无情 提交于 2019-12-06 15:19:18
I've got an events table that is generated by user activity on a site: timestamp | name 7:00 AM | ... 7:01 AM | ... 7:02 AM | ... 7:30 AM | ... 7:31 AM | ... 7:32 AM | ... 8:01 AM | ... 8:03 AM | ... 8:05 AM | ... 8:08 AM | ... 8:09 AM | ... I'd like to aggregate over the events to provide a view of when a user is active. I'm defining active to mean the period in which an event is within +/- 2 minutes. For the above that'd mean: from | till 7:00 AM | 7:02 AM 7:30 AM | 7:32 AM 8:01 AM | 8:05 AM 8:08 AM | 8:09 AM What's the best way to write a query that'll aggregate in that method? Is it

Filter those data from table where there is a gap in sequence

微笑、不失礼 提交于 2019-12-06 15:03:08
I want to set the doprocess flag to 0 if the below data is empty between any of the targets mentioned Example: create table TestSAMP ( id int identity(1,1), modelid navrchar(max), target1 nvarchar(max), target2 nvarchar(max), target3 nvarchar(max), target4 nvarchar(max), doprcoess int default(1) ) --VALID SET DOPROCESS FLAG TO 1 INSERT INTO TestSAMP(modelid,target1,target2,target3,target4) VALUES('1','T1','T2','T3','T4') --NOTVALID SET DOPROCESS FLAG TO 0 DUE TO THE DATA IS MISSING IN SEQUENCE INSERT INTO TestSAMP(modelid,target1,target2,target3,target4) VALUES('2','TR','','T3','T4') --VALID

Mysql sum of records by month for the last 12 months

最后都变了- 提交于 2019-12-06 13:18:37
Hello I'm using this sql query to get the last 12 month records based on month for chart representation: SELECT DATE_FORMAT(drives.timestamp, "%b") AS Month, DATE_FORMAT(drives.timestamp, "%d-%m-%Y %H:%i:%s") AS Exact_date, drives.departure, drives.destination, drives.route, CONCAT(drivers.name, " ", drivers.surname) as driver, drivers.id as driver_id FROM drives, drivers WHERE drives.driver = drivers.id AND drives.timestamp > DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY drives.timestamp Asc however if there are no records for a month it is not included in the result set as expected, and I'm

How do I find records that have the same value in adjacent records in Sql Server? (I believe the correct term for this is a region??)

99封情书 提交于 2019-12-06 11:45:26
Finding the start and end time for adjacent records that have the same value? I have a table that contains heart rate readings (in beats per minute) and datetime field. (Actually the fields are heartrate_id , heartrate , and datetime .) The data are generated by a device that records the heart rate and time every 6 seconds. Sometimes the heart rate monitor will give false readings and the recorded beats per minute will "stick" for an period of time. By sticks, I mean the beats per minute value will be identical in adjacent times. Basically I need to find all the records where the heart rate is

Determine if a range is completely covered by a set of ranges

北城以北 提交于 2019-12-06 11:24:25
How can I check if a range is completely covered by a set of ranges. In the following example: WITH ranges(id, a, b) AS ( SELECT 1, 0, 40 UNION SELECT 2, 40, 60 UNION SELECT 3, 80, 100 UNION SELECT 4, 10, 30 ), tests(id, a, b) AS ( SELECT 1, 10, 90 UNION SELECT 2, 10, 60 ) SELECT * FROM tests WHERE -- ? I want to select 10, 60 because all of it is covered by 0, 40 and 40, 60 (and 10, 30 ) I want to exclude 10, 90 because it is exposed between 60, 80 Assume that a is inclusive and b is exclusive i.e. the value 40 belongs to [40, 60) and not [0, 40) . The ranges can contain gaps and all kind of

Need an array of date blocks from MySql Database

喜欢而已 提交于 2019-12-06 07:06:23
问题 Ok, I have a database table of rows with a StartDate and an EndDate. What I need to do is return blocks of consumed time from that. So, for example, if I have 3 rows as follows: RowID StartDate EndDate 1 2011-01-01 2011-02-01 2 2011-01-30 2011-02-20 3 2011-03-01 2011-04-01 then the blocks of used time would be as follows: 2011-01-01 to 2011-02-20 and 2011-03-01 to 2011-04-01 Is there an easy method of extracting that from a MySql database? Any suggestions welcome! 回答1: Look at the diagram

Finding a 'run' of rows from an ordered result set

≡放荡痞女 提交于 2019-12-06 06:08:13
I'm trying to figure out a way of identifying a "run" of results (successive rows, in order) that meet some condition. Currently, I'm ordering a result set, and scanning by eye for particular patterns. Here's an example: SELECT the_date, name FROM orders WHERE the_date BETWEEN to_date('2013-09-18',..) AND to_date('2013-09-22', ..) ORDER BY the_date -------------------------------------- the_date | name -------------------------------------- 2013-09-18 00:00:01 | John -------------------------------------- 2013-09-19 00:00:01 | James -------------------------------------- 2013-09-20 00:00:01 |

mysql date list with count even if no data on specific date [duplicate]

ぃ、小莉子 提交于 2019-12-06 03:39:43
Possible Duplicate: MySQL how to fill missing dates in range? I'm trying to make a graph from mysqldata, Postid | date | text 1 | 2012-01-01 | bla 2 | 2012-01-01 | bla 3 | 2012-01-02 | bla 4 | 2012-01-02 | bla 5 | 2012-01-04 | bla 6 | 2012-01-04 | bla 7 | 2012-01-05 | bla Now, I'd like to get the number of posts on every day, INCLUDING dates with zero. For example, i'd like to be able to get the first week like this: date | count(Postid) 2012-01-01 | 2 2012-01-02 | 2 2012-01-03 | 0 2012-01-04 | 2 2012-01-05 | 1 2012-01-06 | 0 2012-01-07 | 0 I'm looking for a generic solution, where i don't