gaps-and-islands

sql query to get deleted records

不打扰是莪最后的温柔 提交于 2019-12-09 14:38:48
问题 You have a table table1 that contains id column, that is int(11), not null, auto_increment and starts from 1. Suppose, you have 10,000 records. It is clear the id of the last record is 10,000. Once you removed 3 records, you have 9,997 records in the table, but the last record id value is still 10,000 (if the last record was not deleted). How to display what records have been removed using 1 sql query? Thank you. 回答1: I think easiest would be to have a dummy/temp table with just ids. 1-1000

SQL Server 2014 Merging Overlapping Date Ranges

好久不见. 提交于 2019-12-09 03:26:28
问题 I have a table with 200.000 rows in a SQL Server 2014 database looking like this: CREATE TABLE DateRanges ( Contract VARCHAR(8), Sector VARCHAR(8), StartDate DATE, EndDate DATE ); INSERT INTO DateRanges (Contract, Sector, StartDate, Enddate) SELECT '111', '999', '01-01-2014', '03-31-2014' union SELECT '111', '999', '04-01-2014', '06-30-2014' union SELECT '111', '999', '07-01-2014', '09-30-2014' union SELECT '111', '999', '10-01-2014', '12-31-2014' union SELECT '111', '888', '08-01-2014', '08

Detect and delete gaps in time series

我的梦境 提交于 2019-12-08 13:29:51
问题 I have daily time series for different companies in my dataset and work with PostgreSQL. My goal is to exclude companies with too incomplete time series. Therefor I want to exclude all companies which have 3 or more consecutive missing values. Furthermore I want to exclude all companies which have more than 50% missing values between their first and final date in the dataset. We can work with the following example data: date company value 2012-01-01 A 5 2012-01-01 B 2 2012-01-02 A NULL 2012

Oracle SQL how to group by, but have multiple rows if group is repeated at a later date

▼魔方 西西 提交于 2019-12-08 11:31:40
问题 I have the following query select paaf.assignment_id, paaf.position_id, paaf.effective_start_date effective_start_date, paaf.effective_end_date effective_end_date from per_all_assignments_f paaf where paaf.position_id is not null and paaf.assignment_type in ('E', 'C') and paaf.primary_flag = 'Y' and paaf.assignment_number like '209384%' order by 3 Which returns "assignment_id" "position_id" "effective_start_date" "effective_end_date" 6518 5323 01/01/2013 28/02/2014 6518 8133 01/03/2014 30/06

Determine contiguous dates in SQL gaps and islands

北慕城南 提交于 2019-12-08 07:04:57
问题 I have a situation where a single patient can receive multiple services. These services can have overlapping dates, and can gaps and islands. I am trying to write a query that will show the contiguous length of time that the patient was receiving some kind of service. Table is as follows: CREATE TABLE #tt (Patient VARCHAR(10), StartDate DATETIME, EndDate DATETIME) INSERT INTO #tt VALUES ('Smith', '2014-04-13', '2014-06-04'), ('Smith', '2014-05-07', '2014-05-08'), ('Smith', '2014-06-21', '2014

SQL Time Packing of Islands

狂风中的少年 提交于 2019-12-08 07:04:51
问题 I have an sql table that has something similar to this: EmpNo StartTime EndTime ------------------------------------------ 1 7:00 7:30 1 7:15 7:45 1 13:40 15:00 2 8:00 14:00 2 8:30 9:00 3 10:30 14:30 I've seen a lot of examples where you can find the gaps between everything, and a lot of examples where you can pack overlaps for everything. But I want to be able to separate these out by user. Sadly, I need a pure SQL solution. Ultimately, I would like to return: EmpNo StartTime EndTime -------

islands and gaps tsql

…衆ロ難τιáo~ 提交于 2019-12-08 06:42:31
问题 I have been struggling with a problem that should be pretty simple actually but after a full week of reading, googling, experimenting and so on, my colleague and we cannot find the proper solution. :( The problem: We have a table with two values: an employeenumber (P_ID, int) <--- identification of employee a date (starttime, datetime) <--- time employee checked in We need to know what periods each employee has been working. When two dates are less then @gap days apart, they belong to the

SQL Server Data Conversion

扶醉桌前 提交于 2019-12-08 06:40:44
问题 I have data in the format: Date, FirstName, LastName, Unit Jan1 , Bob , Guy , Home Jan2 , Bob , Guy , Home Jan3 , Bob , Guy , Home Jan5 , Bob , Guy , Home Jan6 , Bob , Guy , Home Jan7 , Bob , Guy , Home Jan8 , Bob , Guy , Offsite Jan3 , Jane , Doe , Home Jan4 , Jane , Doe , Home Jan5 , Jane , Doe , Home Jan9 , Bob , Guy , Home Jan10, Bob , Guy , Home Jan11, Bob , Guy , Home Jan12, Jane , Doe , Home Jan13, Jane , Doe , Home Jan14, Jane , Doe , Home and I want it in the format DateStart,

Grouping Events in Postgres

元气小坏坏 提交于 2019-12-08 06:05:10
问题 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

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

試著忘記壹切 提交于 2019-12-08 05:28:16
问题 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