gaps-and-islands

Group close numbers

偶尔善良 提交于 2019-12-03 05:59:48
I have a table with 2 columns of integers. The first column represents start index and the second column represents end index. START END 1 8 9 13 14 20 20 25 30 42 42 49 60 67 Simple So far. What I would like to do is group all the records that follow together: START END 1 25 30 49 60 67 A record can follow by Starting on the same index as the previous end index or by a margin of 1: START END 1 10 10 20 And START END 1 10 11 20 will both result in START END 1 20 I'm using SQL Server 2008 R2. Any help would be Great This works for your example, let me know if it doesn't work for other data

Using Inner Join to determine the interval between specific records and related events in a table

时光总嘲笑我的痴心妄想 提交于 2019-12-02 18:33:02
问题 I have a simple table that is used to record events against specific visits: Describe Histories; +------------------+ | Field | +------------------+ | HistoryId | | VisitId | | Location | | Event | | EventTime | +------------------+ Individuals are associated with Visits (VisitId). For each Visit, an individual may have multiple History records. Events can be Admission, Transfer or Discharge. I am trying to write a Query to calculate the duration in each Location for each individual. Note,

Complex 'Gaps and Islands' issue

南楼画角 提交于 2019-12-02 13:03:24
问题 I have a table in a Postgres DB like this: person | eventdate | type -------------------------------------- <uuid-1> | 2016-05-14 | 300 <uuid-3> | 2016-05-14 | 300 <uuid-1> | 2016-05-15 | 301 <uuid-1> | 2016-05-16 | 301 <uuid-1> | 2016-05-18 | 304 <uuid-1> | 2016-05-22 | 300 <uuid-2> | 2016-05-22 | 304 <uuid-2> | 2016-05-27 | 301 <uuid-1> | 2016-05-30 | 300 <uuid-1> | 2016-06-01 | 300 <uuid-2> | 2016-06-15 | 501 <uuid-2> | 2016-06-16 | 301 <uuid-4> | 2016-06-16 | 300 <uuid-5> | 2016-06-20 |

Using Inner Join to determine the interval between specific records and related events in a table

你离开我真会死。 提交于 2019-12-02 12:09:43
I have a simple table that is used to record events against specific visits: Describe Histories; +------------------+ | Field | +------------------+ | HistoryId | | VisitId | | Location | | Event | | EventTime | +------------------+ Individuals are associated with Visits (VisitId). For each Visit, an individual may have multiple History records. Events can be Admission, Transfer or Discharge. I am trying to write a Query to calculate the duration in each Location for each individual. Note, that they may visit a Location multiple times for each Visit. An individual enters a Location with an

How to label groups in postgresql when group belonging depends on the preceding line?

眉间皱痕 提交于 2019-12-02 11:24:26
I want, in a request, to fill all Null values by the last known value. When it's in a table and not in a request, it's easy: If I define and fill my table as follows: CREATE TABLE test_fill_null ( date INTEGER, value INTEGER ); INSERT INTO test_fill_null VALUES (1,2), (2, NULL), (3, 45), (4,NULL), (5, null); SELECT * FROM test_fill_null ; date | value ------+------- 1 | 2 2 | 3 | 45 4 | 5 | Then I just have to fill like that: UPDATE test_fill_null t1 SET value = ( SELECT t2.value FROM test_fill_null t2 WHERE t2.date <= t1.date AND value IS NOT NULL ORDER BY t2.date DESC LIMIT 1 ); SELECT *

Autoincrement, but omit existing values in the column

妖精的绣舞 提交于 2019-12-02 10:48:05
问题 I have a table: create table DB.t1 (id SERIAL,name varchar(255)); and insert some data: insert into DB.t1 (name) values ('name1'); insert into DB.t1 (id,name) values (5,'name2'); insert into DB.t1 (name) values ('name3'); insert into DB.t1 (name) values ('name4'); insert into DB.t1 (name) values ('name5'); insert into DB.t1 (name) values ('name6'); insert into DB.t1 (name) values ('name7'); select * from DB.t1; Then I can see: 1 name1 5 name2 2 name3 3 name4 4 name5 5 name6 -- how to make

Get total time interval from multiple rows if sequence not broken

China☆狼群 提交于 2019-12-02 04:28:16
问题 I have Work and Person tables (these are just examples to understand problem). Structure Work table id INTEGER person_id INTEGER dt_from DATETIME dt_to DATETIME Person table person_id INTEGER name VARCHAR(50) Data Work table id | person_id | dt_from | dt_to ------------------------------------------------- 1 | 1 | 2011-01-01 | 2011-02-02 2 | 1 | 2011-02-02 | 2011-04-04 3 | 1 | 2011-06-06 | 2011-09-09 4 | 2 | 2011-01-01 | 2011-02-02 5 | 2 | 2011-02-02 | 2011-03-03 ....etc. Person table Just

How to display all the dates between two given dates in SQL

こ雲淡風輕ζ 提交于 2019-12-02 01:59:31
Using SQL server 2000. If the Start date is 06/23/2008 and End date is 06/30/2008 Then I need the Output of query as 06/23/2008 06/24/2008 06/25/2008 . . . 06/30/2008 I Created a Table names as Integer which has 1 Column, column values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 then I used the below mentioned query Tried Query SELECT DATEADD(d, H.i * 100 + T .i * 10 + U.i, '" & dtpfrom.Value & "') AS Dates FROM integers H CROSS JOIN integers T CROSS JOIN integers U order by dates The above query is displaying 999 Dates only. 999 Dates means (365 + 365 + 269) Dates Only. Suppose I want to select more

Group rows by contiguous date ranges for groups of values

你说的曾经没有我的故事 提交于 2019-12-02 01:13:43
Consider some table T , ordered by Col1, Col2, Date1, Date2 : Col1 Col2 Date1 Date2 rate ABC 123 11/4/2014 11/5/2014 -90 ABC 123 11/4/2014 11/6/2014 -55 ABC 123 11/4/2014 11/7/2014 -90 ABC 123 11/4/2014 11/10/2014 -90 I want to group the data so that changes are easily audited/reduce repetition, so I have Col1 Col2 Date1 start_Date2 end_Date2 rate ABC 123 11/4/2014 11/5/2014 11/5/2014 -90 ABC 123 11/4/2014 11/6/2014 11/6/2014 -55 ABC 123 11/4/2014 11/7/2014 11/10/2014 -90 I can easily do that if I can get another column with the rows numbered as 1 2 3 3 (only important that numbers are

How to number consecutive records per island?

本小妞迷上赌 提交于 2019-12-01 22:57:50
问题 I have a table which looks like: group date color A 1-1-2019 R A 1-2-2019 Y B 1-1-2019 R B 1-2-2019 Y B 1-3-2019 Y B 1-4-2019 R B 1-5-2019 R B 1-6-2019 R And it's ordered by group and date. I want an extra column showing sequential number of consecutive color 'R' for each group. Required output: group date color rank A 1-1-2019 R 1 A 1-2-2019 Y null B 1-1-2019 R 1 B 1-2-2019 Y null B 1-3-2019 Y null B 1-4-2019 R 1 B 1-5-2019 R 2 B 1-6-2019 R 3 I've tried to use window function with partition