gaps-and-islands

Find gaps of a sequence in SQL without creating additional tables

老子叫甜甜 提交于 2021-02-18 11:29:14
问题 I have a table invoices with a field invoice_number . This is what happens when i execute select invoice_number from invoice : invoice_number -------------- 1 2 3 5 6 10 11 I want a SQL that gives me the following result: gap_start | gap_end 4 | 4 7 | 9 How can i write a SQL to perform such query? I am using PostgreSQL. 回答1: With modern SQL, this can easily be done using window functions: select invoice_number + 1 as gap_start, next_nr - 1 as gap_end from ( select invoice_number, lead(invoice

Select on value change

北城余情 提交于 2021-02-10 18:20:30
问题 I have a table that looks like this in a MySQL database: CREATE TABLE IF NOT EXISTS Example(Batch_Num int, Time DATETIME); INSERT INTO Example VALUES (1,'2020-12-04 05:06:12'), (1,'2020-12-04 05:06:13'), (1,'2020-12-04 05:06:14'), (2,'2020-12-04 05:06:20'), (2,'2020-12-04 05:07:12'), (2,'2020-12-04 05:07:20'), (1,'2020-12-04 05:07:25'), (1,'2020-12-04 05:07:35'), (3,'2020-12-04 05:07:35'); I would like to select all lines where the Batch_Num is different from the previous value including the

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

徘徊边缘 提交于 2021-02-08 10:32:23
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

≡放荡痞女 提交于 2021-02-08 10:31:54
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

Oracle SQL lead lag across different group

这一生的挚爱 提交于 2021-02-08 08:30:31
问题 Suppose I have the below table. They key is just concat P1, P2, P3. I want to compare between key for each day. for example, from day 1 to day2, abc is removed and abe, aby is added. P1 P2 P3 DAY KEY a b c 1 abc a b e 2 abe a b y 2 aby a b x 3 abx a b c 3 abc Expected result set: KEY OPERATION DAY abc ADD 1 abe ADD 2 aby ADD 2 abc REMOVE 2 abx ADD 3 abc ADD 3 abe REMOVE 3 aby REMOVE 3 And what if the day is not sequential. For example: P1 P2 P3 DAY KEY a b c 1 abc a b e 2 abe a b y 2 aby a b

postgresql given “1,2,3,6,7,8,11,12,15,18,19,20”, a query to return the maximum of each group of consecutive numbers

假如想象 提交于 2021-02-05 12:20:29
问题 given 1,2,3,6,7,8,11,12,15,18,19,20 , write a query to return the maximum of each group of the consecutive numbers are grouped by the query below, but I don't know how to obtain the maximum for each group of consecutive numbers with my current query with trans as ( select c1, case when lag(c1) over (order by c1) = c1 - 1 then 0 else 1 end as new from table1 ), groups as ( select c1, sum(new) over (order by c1) as grpnum from trans ), ranges as ( select grpnum, min(c1) as low, max(c1) as high

postgresql given “1,2,3,6,7,8,11,12,15,18,19,20”, a query to return the maximum of each group of consecutive numbers

China☆狼群 提交于 2021-02-05 12:20:07
问题 given 1,2,3,6,7,8,11,12,15,18,19,20 , write a query to return the maximum of each group of the consecutive numbers are grouped by the query below, but I don't know how to obtain the maximum for each group of consecutive numbers with my current query with trans as ( select c1, case when lag(c1) over (order by c1) = c1 - 1 then 0 else 1 end as new from table1 ), groups as ( select c1, sum(new) over (order by c1) as grpnum from trans ), ranges as ( select grpnum, min(c1) as low, max(c1) as high

Min and Max values grouping by consecutive ranges

霸气de小男生 提交于 2021-01-29 05:13:43
问题 I have a table that informs me a error type and line number that error occurred. (The process is irrelevant at this moment). I need to group by error type and show line start and line end for each error type, resulting of a range of each error type. I need to consider gaps of lines My table and queries was: create table errors ( err_type varchar(10), line integer); insert into errors values ('type_A', 1),('type_A', 2),('type_A', 3), ('type_A', 6),('type_A', 7), ('type_B', 9),('type_B', 10), (

Counting null values between dates

拈花ヽ惹草 提交于 2021-01-29 03:37:35
问题 I'm trying to calculate the number of null values between dates. My table looks like this: transaction_date transaction_sale 10/1/2018 NULL 11/1/2018 33 12/1/2018 NULL 1/1/2019 NULL 2/1/2019 NULL 3/1/2019 2 4/1/2019 NULL 5/1/2019 NULL 6/1/2019 10 I'm looking to get the following output: transaction_date transaction_sale count 10/1/2018 NULL NULL 11/1/2018 33 1 12/1/2018 NULL NULL 1/1/2019 NULL NULL 2/1/2019 NULL NULL 3/1/2019 2 3 4/1/2019 NULL NULL 5/1/2019 NULL NULL 6/1/2019 10 2 回答1: count(

Group rows by ID and find max/min(date_from, date_to) with date gaps

こ雲淡風輕ζ 提交于 2021-01-28 11:55:39
问题 I need to group data by id and find max/min (date_from, date_to). But if there is date gap it should be new row. I have following data: SYS_ID ITEM_ID DATE_FROM DATE_TO 1 1 01.01.2019 20.01.2019 1 1 15.01.2019 10.02.2019 1 1 15.02.2019 20.02.2019 1 1 18.02.2019 10.03.2019 1 1 10.03.2019 22.03.2019 1 2 01.01.2019 10.01.2019 1 2 15.01.2019 25.01.2019 Result should be: SYS_ID ITEM_ID DATE_FROM DATE_TO 1 1 01.01.2019 10.02.2019 1 1 15.02.2019 22.03.2019 1 2 01.01.2019 10.01.2019 1 2 15.01.2019 25