gaps-and-islands

Finding the largest group of consecutive numbers within a partition

人盡茶涼 提交于 2019-12-04 12:52:12
I have the following data ordered by player_id and match_date. I would like to find out the group of records that has the maximum number of consecutive runs (4 runs from 2014-04-03 till 2014-04-12 for 3 consecutive times) player_id match_date runs 1 2014-04-01 5 1 2014-04-02 55 1 2014-04-03 4 1 2014-04-10 4 1 2014-04-12 4 1 2014-04-14 3 1 2014-04-19 4 1 2014-04-20 44 2 2014-04-01 23 2 2014-04-02 23 2 2014-04-03 23 2 2014-04-10 23 2 2014-04-12 4 2 2014-04-14 3 2 2014-04-19 23 2 2014-04-20 1 I have come up with the following SQL: select *,row_number() over (partition by ranked.player_id,ranked

Need an array of date blocks from MySql Database

最后都变了- 提交于 2019-12-04 12:40:30
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! Look at the diagram below which represents some overlapping time periods X----| |--------| |------X |-------X X------------X |----|

Find missing time intervals in a table

廉价感情. 提交于 2019-12-04 12:12:07
问题 I have the following table which contains values read every 15 minutes from several different devices: ID DeviceID Date Value ---------------------------------------------- 1 3 24.08.2011 00:00:00 0.51 2 3 24.08.2011 00:15:00 2.9 3 3 24.08.2011 00:30:00 0 4 3 24.08.2011 00:45:00 7.1 5 3 24.08.2011 01:00:00 1.05 6 3 24.08.2011 03:15:00 3.8 I'd like to find all the gaps in the table for each device, where there are no entries, for a given month. For the table above, the result should be

Numbering islands in SQL Server 2012

耗尽温柔 提交于 2019-12-04 11:22:16
I need to number islands in SQL Server 2012. Island is defined as a set of rows where there is no day gaps between DateFrom and DateTo within the same ItemId ). The following dataset: CREATE TABLE #Ranges (ItemId INT, DateFrom DATETIME, DateTo DATETIME) INSERT INTO #Ranges VALUES (1,'2015-01-31','2015-02-17') INSERT INTO #Ranges VALUES (1,'2015-02-18','2015-03-31') INSERT INTO #Ranges VALUES (1,'2015-04-14','2015-05-21') INSERT INTO #Ranges VALUES (2,'2015-07-12','2015-07-19') INSERT INTO #Ranges VALUES (2,'2015-07-20','2015-07-24') INSERT INTO #Ranges VALUES (2,'2015-07-26','2015-08-02')

Return rows of the latest 'streak' of data

你离开我真会死。 提交于 2019-12-04 09:58:35
Given a simple table with the following data: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 4 | W | 2012-01-04 3 | W | 2012-01-03 2 | L | 2012-01-02 1 | W | 2012-01-01 How would I write a query to return the lastest losing or winning streak of rows using PostgreSQL? In this case, I'm looking for a result of: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 I'm guessing the answer is to use lag() and partition syntax, but I can't seem to nail it down. Assuming (as you don't tell)

Jump SQL gap over specific condition & proper lead() usage

岁酱吖の 提交于 2019-12-04 02:03:59
问题 (PostgreSQL 8.4) Continuing with my previous example, I wish to further my understanding of gaps-and-islands processing with Window-functions. Consider the following table and data: CREATE TABLE T1 ( id SERIAL PRIMARY KEY, val INT, -- some device status INT -- 0=OFF, 1=ON ); INSERT INTO T1 (val, status) VALUES (10, 0); INSERT INTO T1 (val, status) VALUES (11, 0); INSERT INTO T1 (val, status) VALUES (11, 1); INSERT INTO T1 (val, status) VALUES (10, 1); INSERT INTO T1 (val, status) VALUES (11,

SQL to determine multiple date ranges (SQL Server 2000)

好久不见. 提交于 2019-12-04 01:58:25
问题 I have a table which contains an ID and a Date for an event. Each row is for one date. I am trying to determine consecutive date ranges and consolidate output to show the ID,StartDate,EndDate ID Date 200236 2011-01-02 00:00:00.000 200236 2011-01-03 00:00:00.000 200236 2011-01-05 00:00:00.000 200236 2011-01-06 00:00:00.000 200236 2011-01-07 00:00:00.000 200236 2011-01-08 00:00:00.000 200236 2011-01-09 00:00:00.000 200236 2011-01-10 00:00:00.000 200236 2011-01-11 00:00:00.000 200236 2011-01-12

sql query to get deleted records

和自甴很熟 提交于 2019-12-04 01:53:05
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. I think easiest would be to have a dummy/temp table with just ids. 1-1000 then left join to that table. But be sure to remove the "deleted" records from your dummy/temp table once you

How can I identify groups of consecutive dates in SQL?

一世执手 提交于 2019-12-03 12:07:35
Im trying to write a function which identifies groups of dates, and measures the size of the group. I've been doing this procedurally in Python until now but I'd like to move it into SQL. for example, the list Bill 01/01/2011 Bill 02/01/2011 Bill 03/01/2011 Bill 05/01/2011 Bill 07/01/2011 should be output into a new table as: Bill 01/01/2011 3 Bill 02/01/2011 3 Bill 03/01/2011 3 Bill 05/01/2011 1 Bill 07/01/2011 1 Ideally this should also be able to account for weekends and public holidays - the dates in my table will aways be Mon-Fri (I think I can solve this by making a new table of working

Trouble using ROW_NUMBER() OVER (PARTITION BY …)

十年热恋 提交于 2019-12-03 09:04:35
问题 I'm using SQL Server 2008 R2. I have table called EmployeeHistory with the following structure and sample data: EmployeeID Date DepartmentID SupervisorID 10001 20130101 001 10009 10001 20130909 001 10019 10001 20131201 002 10018 10001 20140501 002 10017 10001 20141001 001 10015 10001 20141201 001 10014 Notice that the Employee 10001 has been changing 2 departments and several supervisors over time. What I am trying to do is to list the start and end dates of this employee's employment in each