gaps-and-islands

How do I group on continuous ranges

独自空忆成欢 提交于 2019-11-26 16:38:07
I know some basic sql, but this one is beyond me. I have looked high and low but no dice. I need a view of the following data, I can do this in the application layer code. But unfortunately for this particular one, the code must be put in the data layer. I am using T-SQL. Table Date Crew DayType 01-02-11 John Doe SEA 02-02-11 John Doe SEA 03-02-11 John Doe SEA 04-02-11 John Doe HOME 05-02-11 John Doe HOME 06-02-11 John Doe SEA I need a view like this DateFrom DateTo Name DayType 01-02-11 03-02-11 John Doe SEA 04-02-11 05-02-11 John Doe HOME 06-02-11 06-02-11 John Doe SEA Unfortunately the base

Find the smallest unused number in SQL Server

别等时光非礼了梦想. 提交于 2019-11-26 15:24:36
How do you find the smallest unused number in a SQL Server column? I am about to import a large number of manually recorded records from Excel into a SQL Server table. They all have a numeric ID (called document number), but they weren't assigned sequentially for reasons that no longer apply, meaning from now on when my web site records a new record, it needs to assign it the smallest possible document number (greater than zero) that has not already been taken. Is there a way to do this through plain SQL or is this a problem for TSQL/code? Thanks! EDIT Special thanks to WW for raising the

Detect consecutive dates ranges using SQL

∥☆過路亽.° 提交于 2019-11-26 13:44:53
问题 I want to fill the calendar object which requires start and end date information. I have one column which contains a sequence of dates. Some of the dates are consecutive (have one day difference) and some are not. InfoDate 2013-12-04 consecutive date [StartDate] 2013-12-05 consecutive date 2013-12-06 consecutive date [EndDate] 2013-12-09 [startDate] 2013-12-10 [EndDate] 2014-01-01 [startDate] 2014-01-02 2014-01-03 [EndDate] 2014-01-06 [startDate] 2014-01-07 [EndDate] 2014-01-29 [startDate]

How do I find a “gap” in running counter with SQL?

末鹿安然 提交于 2019-11-26 12:03:54
I'd like to find the first "gap" in a counter column in an SQL table. For example, if there are values 1,2,4 and 5 I'd like to find out 3. I can of course get the values in order and go through it manually, but I'd like to know if there would be a way to do it in SQL. In addition, it should be quite standard SQL, working with different DBMSes. In MySQL and PostgreSQL : SELECT id + 1 FROM mytable mo WHERE NOT EXISTS ( SELECT NULL FROM mytable mi WHERE mi.id = mo.id + 1 ) ORDER BY id LIMIT 1 In SQL Server : SELECT TOP 1 id + 1 FROM mytable mo WHERE NOT EXISTS ( SELECT NULL FROM mytable mi WHERE

How to find gaps in sequential numbering in mysql?

有些话、适合烂在心里 提交于 2019-11-26 05:57:15
We have a database with a table whose values were imported from another system. There is an auto-increment column, and there are no duplicate values, but there are missing values. For example, running this query: select count(id) from arrc_vouchers where id between 1 and 100 should return 100, but it returns 87 instead. Is there any query I can run that will return the values of the missing numbers? For example, the records may exist for id 1-70 and 83-100, but there are no records with id's of 71-82. I want to return 71, 72, 73, etc. Is this possible? Update ConfexianMJS provided much better

sql group by only rows which are in sequence

雨燕双飞 提交于 2019-11-26 05:33:54
问题 Say I have the following table: MyTable --------- | 1 | A | | 2 | A | | 3 | A | | 4 | B | | 5 | B | | 6 | B | | 7 | A | | 8 | A | --------- I need the sql query to output the following: --------- | 3 | A | | 3 | B | | 2 | A | --------- Basically I\'m doing a group by but only for rows which are together in the sequence. Any ideas? Note that the database is on sql server 2008. There is a post on this topic however it uses oracle\'s lag() function. 回答1: This is known as the "islands" problem.

Find the smallest unused number in SQL Server

核能气质少年 提交于 2019-11-26 04:24:54
问题 How do you find the smallest unused number in a SQL Server column? I am about to import a large number of manually recorded records from Excel into a SQL Server table. They all have a numeric ID (called document number), but they weren\'t assigned sequentially for reasons that no longer apply, meaning from now on when my web site records a new record, it needs to assign it the smallest possible document number (greater than zero) that has not already been taken. Is there a way to do this

How do I find a “gap” in running counter with SQL?

℡╲_俬逩灬. 提交于 2019-11-26 02:42:10
问题 I\'d like to find the first \"gap\" in a counter column in an SQL table. For example, if there are values 1,2,4 and 5 I\'d like to find out 3. I can of course get the values in order and go through it manually, but I\'d like to know if there would be a way to do it in SQL. In addition, it should be quite standard SQL, working with different DBMSes. 回答1: In MySQL and PostgreSQL : SELECT id + 1 FROM mytable mo WHERE NOT EXISTS ( SELECT NULL FROM mytable mi WHERE mi.id = mo.id + 1 ) ORDER BY id

How to find gaps in sequential numbering in mysql?

北城以北 提交于 2019-11-26 01:49:06
问题 We have a database with a table whose values were imported from another system. There is an auto-increment column, and there are no duplicate values, but there are missing values. For example, running this query: select count(id) from arrc_vouchers where id between 1 and 100 should return 100, but it returns 87 instead. Is there any query I can run that will return the values of the missing numbers? For example, the records may exist for id 1-70 and 83-100, but there are no records with id\'s

MySQL: Select All Dates In a Range Even If No Records Present

♀尐吖头ヾ 提交于 2019-11-26 01:36:30
问题 I have a database of users. I would like to create a graph based on userbase growth. The query I have now is: SELECT DATE(datecreated), count(*) AS number FROM users WHERE DATE(datecreated) > \'2009-06-21\' AND DATE(datecreated) <= DATE(NOW()) GROUP BY DATE(datecreated) ORDER BY datecreated ASC This returns almost what I want. If we get 0 users one day, that day is not returned as a 0 value, it is just skipped and the next day that has at least one user is returned. How can I get something