gaps-and-islands

Numbering islands in SQL Server 2012

限于喜欢 提交于 2019-12-21 20:05:27
问题 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

Numbering islands in SQL Server 2012

不想你离开。 提交于 2019-12-21 20:01:53
问题 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

Group consecutively values in MySQL and add an id to such groups

有些话、适合烂在心里 提交于 2019-12-21 05:13:09
问题 I have a simple table and I need to identified groups of four rows (the groups aren't consecutives), but each rows of each row has a +1 in the value. For example: ---------------------- | language | id | ---------------------- | C | 16 | | C++ | 17 | | Java | 18 | | Python | 19 | | HTML | 65 | | JavaScript | 66 | | PHP | 67 | | Perl | 68 | ---------------------- I want to add a column that indicates the group or set, how is possible to get this output using MySQL?: ---------------------------

reducing table to avoid space inefficiency

懵懂的女人 提交于 2019-12-20 04:40:12
问题 I have the following table : id study_start_time study_end_time ____________________________________________ 1234 168 256 2345 175 233 1234 256 300 1234 300 389 2345 400 425 4567 200 225 ı want to obtain the below table ; id study_start_time study_end_time ____________________________________________ 1234 168 389 process ; 1234 starts at 168. minutes , work until to 256. minutes 1234 " " 256. " " " " " 300. " 1234 " " 300. " " " " " 389. " but actual table should be reduced to as shown below

Retrieve missing dates from database via MySQL

故事扮演 提交于 2019-12-20 03:30:54
问题 So I have a table where I collect data for the jobs that I do. Each time I create a job I assign it a date. The problem with this is the days I don't have jobs aren't stored in the database therefore when I graph my data I never see the days that I had zero jobs. My current query looks like this: SELECT job_data_date, SUM(job_data_invoice_amount) as job_data_date_income FROM job_data WHERE job_data_date >= '2010-05-05' GROUP BY job_data_date ORDER BY job_data_date; The output is: | job_data

How to group timestamps into islands (based on arbitrary gap)?

可紊 提交于 2019-12-20 03:20:26
问题 Consider this list of dates as timestamptz : I grouped the dates by hand using colors: every group is separated from the next by a gap of at least 2 minutes. I'm trying to measure how much a given user studied, by looking at when they performed an action (the data is when they finished studying a sentence.) e.g.: on the yellow block, I'd consider the user studied in one sitting, from 14:24 till 14:27, or roughly 3 minutes in a row. I see how I could group these dates with a programming

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

廉价感情. 提交于 2019-12-20 03:14: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

Group rows by contiguous date ranges for groups of values

隐身守侯 提交于 2019-12-20 03:05:23
问题 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

Can window function LAG reference the column which value is being calculated?

风格不统一 提交于 2019-12-19 16:51:33
问题 I need to calculate value of some column X based on some other columns of the current record and the value of X for the previous record (using some partition and order). Basically I need to implement query in the form SELECT <some fields>, <some expression using LAG(X) OVER(PARTITION BY ... ORDER BY ...) AS X FROM <table> This is not possible because only existing columns can be used in window function so I'm looking way how to overcome this. Here is an example. I have a table with events.

Joining together consecutive date validity intervals

你离开我真会死。 提交于 2019-12-19 09:06:54
问题 I have a series of records containing some information (product type) with temporal validity. I would like to meld together adjacent validity intervals, provided that the grouping information (the product type) stays the same. I cannot use a simple GROUP BY with MIN and MAX , because some product types ( A , in the example) can "go away" and "come back". Using Oracle 11g. A similar question for MySQL is: How can I do a contiguous group by in MySQL? Input data : | PRODUCT | START_DATE | END