gaps-and-islands

Resetting Row number according to record data change

五迷三道 提交于 2019-11-27 01:39:56
问题 i have got the set of data as follow name date x 2014-01-01 x 2014-01-02 y 2014-01-03 x 2014-01-04 and i'm trying to get this result name date row_num x 2014-01-01 1 x 2014-01-02 2 y 2014-01-03 1 x 2014-01-04 1 i have tried to run this query select name, date, row_number () over (partition by name order by date) as row_num from myTBL but unfortunately i get this result name date row_num x 2014-01-01 1 x 2014-01-02 2 y 2014-01-03 1 x 2014-01-04 3 please help. 回答1: You need to identify the

SQL - Find missing int values in mostly ordered sequential series

两盒软妹~` 提交于 2019-11-26 21:26:33
问题 I manage a message based system in which a sequence of unique integer ids will be entirely represented at the end of the day, though they will not necessarily arrive in order. I am looking for help in finding missing ids in this series using SQL. If my column values are something like the below, how can I find which ids I am missing in this sequence, in this case 6 ? The sequence will begin and end at an arbitrary point each day, so min and max would differ upon each run. Coming from a Perl

SQL to find time elapsed from multiple overlapping intervals

吃可爱长大的小学妹 提交于 2019-11-26 20:42:38
问题 Not using MSSQL or DB2 or Oracle. No CTE. No OVERLAP predicate. No INTERVAL data type. The situation: on a vehicle to be repaired work can not start until all parts ordered for the job have been received. Parts may be ordered multiple times prior to the start of repair. We need to extract the time for which the vehicle was on "parts hold" So for a vehicle identified as id = 1 parts were ordered (d1) and received (d2) on 4 different occasions ID d1 d2 1 8/1 8/8 1 8/2 8/6 1 8/12 8/14 1 8/3 8/10

SQL query to find Missing sequence numbers

天大地大妈咪最大 提交于 2019-11-26 19:55:25
I have a column named sequence . The data in this column looks like 1, 2, 3, 4, 5, 7, 9, 10, 15. I need to find the missing sequence numbers from the table. What SQL query will find the missing sequence numbers from my table? I am expecting results like Missing numbers --------------- 6 8 11 12 13 14 I am using only one table. I tried the query below, but am not getting the results I want. select de.sequence + 1 as sequence from dataentry as de left outer join dataentry as de1 on de.sequence + 1 = de1.sequence where de1.sequence is null order by sequence asc; How about something like: select

How to check any missing number from a series of numbers?

♀尐吖头ヾ 提交于 2019-11-26 19:19:50
问题 I am doing a project creating an admission system for a college; the technologies are Java and Oracle. In one of the tables, pre-generated serial numbers are stored. Later, against those serial numbers, the applicant's form data will be entered. My requirement is that when the entry process is completed I will have to generate a Lot wise report. If during feeding pre-generated serial numbers any sequence numbers went missing. For example, say in a table, the sequence numbers are 7001, 7002,

SQL to determine minimum sequential days of access?

送分小仙女□ 提交于 2019-11-26 18:45:46
问题 The following User History table contains one record for every day a given user has accessed a website (in a 24 hour UTC period). It has many thousands of records, but only one record per day per user. If the user has not accessed the website for that day, no record will be generated. Id UserId CreationDate ------ ------ ------------ 750997 12 2009-07-07 18:42:20.723 750998 15 2009-07-07 18:42:20.927 751000 19 2009-07-07 18:42:22.283 What I'm looking for is a SQL query on this table with good

GROUP BY and aggregate sequential numeric values

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 17:48:24
Using PostgreSQL 9.0. Let's say I have a table containing the fields: company , profession and year . I want to return a result which contains unique companies and professions, but aggregates (into an array is fine) years based on numeric sequence: Example Table: +-----------------------------+ | company | profession | year | +---------+------------+------+ | Google | Programmer | 2000 | | Google | Sales | 2000 | | Google | Sales | 2001 | | Google | Sales | 2002 | | Google | Sales | 2004 | | Mozilla | Sales | 2002 | +-----------------------------+ I'm interested in a query which would output

sql group by only rows which are in sequence

半城伤御伤魂 提交于 2019-11-26 17:43:51
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. This is known as the "islands" problem. Using Itzik Ben Gan's approach: ;WITH YourTable AS ( SELECT 1 AS N, 'A' AS C UNION ALL SELECT 2 AS N, 'A' AS C

Detect consecutive dates ranges using SQL

故事扮演 提交于 2019-11-26 17:40:24
问题 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 to group continuous ranges using MySQL

我只是一个虾纸丫 提交于 2019-11-26 17:14:04
问题 I have a table that contains categories, dates and rates. Each category can have different rates for different dates, one category can have only one rate at a given date. Id CatId Date Rate ------ ------ ------------ --------- 000001 12 2009-07-07 1 000002 12 2009-07-08 1 000003 12 2009-07-09 1 000004 12 2009-07-10 2 000005 12 2009-07-15 1 000006 12 2009-07-16 1 000007 13 2009-07-08 1 000008 13 2009-07-09 1 000009 14 2009-07-07 2 000010 14 2009-07-08 1 000010 14 2009-07-10 1 Unique index