gaps-and-islands

How to count number of non-consecutive values in a column using SQL?

我是研究僧i 提交于 2019-12-06 03:25:58
Following-up on my question here . Say I have a table in an Oracle database like the one below (table_1) which tracks service involvement for a particular individual: name day srvc_ inv bill 1 1 bill 2 1 bill 3 0 bill 4 0 bill 5 1 bill 6 0 susy 1 1 susy 2 0 susy 3 1 susy 4 0 susy 5 1 My goal is to get a summary table which lists, for all unique individuals, whether there was service involvement and the number of distinct service episodes (in this case 2 for bill and 3 for susy), where a distinct service episode is identified by a break in activity over days. To get any service involvement, I

Oracle SQL. What statement should I use

删除回忆录丶 提交于 2019-12-05 21:37:53
DATA given: inventory_num_id inventory_group_id num code 9681066 100003894 211 E 9679839 100003894 212 E 9687165 100003894 213 E 9680883 100003894 214 I 9710863 100003894 515 E 9681246 100003894 516 E 9682695 100003894 517 E 9681239 100003894 518 E 9685409 100003894 519 E 9679843 100003894 520 C 9679844 100003894 521 C 9714882 100003894 522 E 9679845 100003894 523 I 9681211 100003894 524 E 9681216 100003894 525 E 9682696 100003894 526 E 9681227 100003894 527 E Result examples should be like: inventory_group_id code start end ------------------ ---- ----- ---- 100003894 E 211 213 100003894 I

Sql - Merging rows if date connects

岁酱吖の 提交于 2019-12-05 20:36:35
I have table with rows: clientid, startdate and enddate. Date cant overlap for same clientid. I would like to merge rows for every client if date connects. table looks like this: clientid startdate enddate 1 10.10.2017 12.10.2017 1 12.10.2017 13.10.2017 1 13.10.2017 17.10.2017 1 10.11.2017 17.11.2017 1 17.11.2017 23.11.2017 1 12.12.2017 14.12.2017 2 10.11.2017 15.11.2017 2 01.12.2017 02.12.2017 2 02.12.2017 05.12.2017 Final table should looks like this: clientid startdate enddate 1 10.10.2017 17.10.2017 1 10.11.2017 23.11.2017 1 12.12.2017 14.12.2017 2 10.11.2017 15.11.2017 2 01.12.2017 05.12

Islands and Gaps Issue

北慕城南 提交于 2019-12-05 17:57:48
Backstory: I have a database that has data points of drivers in trucks which also contain the. While in a truck, the driver can have a 'driverstatus'. What I'd like to do is group these statuses by driver, truck. As of now, I've tried using LAG/LEAD to help. The reason for this is so I can tell when a driverstatus change occurs, and then I can mark that row as having the last datetime of that status. That in itself is insufficient, because I need to group the statuses by their status and date. For this, I've got something such as DENSE_RANK, but I can't manage to get that right concerning the

Make Postgres choose the next minimal available id

て烟熏妆下的殇ゞ 提交于 2019-12-05 09:52:29
I would like to make Postgres choose the first next available id so that no error occurs in the following case: CREATE TABLE test( id serial PRIMARY KEY, name varchar ); Then: INSERT INTO test VALUES (2,'dd'); INSERT INTO test (name) VALUES ('aa'); INSERT INTO test (name) VALUES ('bb'); This will give a constraint error since id is primary. How can I tell Postgres to insert the record with the next free id? Erwin Brandstetter Generally it's best to never overrule the default in a serial column. If you sometimes need to provide id values manually, replace the standard DEFAULT clause nextval(

Select rows where price didn't change

一世执手 提交于 2019-12-04 23:41:26
问题 Suppose you have a table like (am using SQL Server 2008, no audit log - table is HUGE): SecID | Date | Price 1 1/1/11 10 1 1/2/11 10 1 1/3/11 5 1 1/4/11 10 1 1/5/11 10 Suppose this table is HUGE (millions of rows for different secIDs and Date) - I would like to return the records when the price changed (looking for something better than using a cursor and iterating): Am trying to figure out how to get: SecID | StartDate | EndDate | Price 1 1/1/11 1/2/11 10 1 1/3/11 1/3/11 5 1 1/4/11 1/5/11 10

Filtering out duplicate subsequent records in a SELECT

时间秒杀一切 提交于 2019-12-04 22:47:28
问题 (PostgreSQL 8.4) Table "trackingMessages" stores tracking events between mobile devices (tm_nl_mobileid) and fixed devices (tm_nl_fixedId). CREATE TABLE trackingMessages ( tm_id SERIAL PRIMARY KEY, -- PK tm_nl_mobileId INTEGER, -- FK to mobile tm_nl_fixedId INTEGER, -- FK to fixed tm_date INTEGER, -- Network time tm_messageType INTEGER, -- 0=disconnect, 1=connect CONSTRAINT tm_unique_row UNIQUE (tm_nl_mobileId, tm_nl_fixedId, tm_date, tm_messageType) ); Problem here is that it's possible that

SQL GROUP BY: intervals in continuity?

大憨熊 提交于 2019-12-04 22:39:26
问题 The idea is that say you have the following table. ------------- | oID | Area| ------------- | 1 | 5 | | 2 | 2 | | 3 | 3 | | 5 | 3 | | 6 | 4 | | 7 | 5 | ------------- If grouping by continuity is possible this pseudo query SELECT SUM(Area) FROM sample_table GROUP BY CONTINUITY(oID) would return ------------- | SUM(Area) | ------------- | 10 | | 12 | ------------- Where the continuity break arises at oID or rather the lack thereof an entry representing oID 4. Does such functionality exist

How can I identify groups of consecutive dates in SQL?

纵然是瞬间 提交于 2019-12-04 19:37:01
问题 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 -

How to select ranges in a range of record in oracle

寵の児 提交于 2019-12-04 19:06:50
If I have a table like this Number Status ------ ------ 1 A 2 A 3 A 4 U 5 U 6 A 7 U 8 U 9 A 10 A What query can I use to group the range into ranges where Status = A? Range Count Status ----- ----- ------ 1-3 3 A 6-6 1 A 9-10 2 A My query is select min(number) || '--' || max(number), count(*), Status from table where Status = 'A' group by Status Range Count Status ----- ----- ------ 1-10 6 A This is a nice way, fancy name " Tabibitosan method " given by Aketi Jyuuzou. SQL> WITH data AS 2 (SELECT num - DENSE_RANK() OVER(PARTITION BY status ORDER BY num) grp, 3 status, 4 num 5 FROM t 6 ) 7