gaps-and-islands

How to retrieve only the records where stat changes?

别说谁变了你拦得住时间么 提交于 2019-12-13 22:06:41
问题 I want to get same output: using the following sample data create table x ( id int, date datetime, stat int ) insert into x values (1, '2017-01-01', 100), (1, '2017-01-03', 100), (1, '2017-01-05', 100), (1, '2017-01-07', 150), (1, '2017-01-09', 150), (1, '2017-02-01', 150), (1, '2017-02-02', 100), (1, '2017-02-12', 100), (1, '2017-02-15', 100), (1, '2017-02-17', 150), (1, '2017-03-09', 150), (1, '2017-03-11', 150), (2, '2017-01-01', 100), (2, '2017-01-03', 100), (2, '2017-01-05', 100), (2,

Find ranges from a series of numbers in SQL/Oracle

廉价感情. 提交于 2019-12-13 18:20:05
问题 I have a table containing a series of numbers 1,2,3,4,5,11,12,13,14,15,101,102,103,104,105,510,511,512,513,515,516,517. I want an SQL Query, Procedure or Function so that I can get the ranges in the following formats. From-To: 1-5 11-15 101-105 510-517 OR 1-5, 11-15, 101-105, 510-517 回答1: You could do it using ROW_NUMBER analytic function. See Find range of consecutive values in a sequence of numbers or dates. For example, Range SQL> with data(num) as( 2 select 1 from dual union 3 select 2

Duration and idle time for a server from continuous dates

徘徊边缘 提交于 2019-12-13 09:42:46
问题 Server Customer. Start. End A. X. 12/10/2018 12:56 13/10/2018. 13:05 B. K. 12/10/2018 14:05. 12/10/2018. 14:25 A. N 12/10/2018.13:08. 13/10/2018. 17:09 A. Y 15/10/2018.16:07. 17/10/2018. 14:09 A. F. 18/10/2018 13:05. 18/10/2018 20:09 I want how much duration server A was used: 12/10/2018 12:56 to 18/10/2018 20:09 and how much time it was idle i.e 13/10/2018 17:09 to 15/10/2018 16:07 duration + 17/10/2018 14:09 to 18/10/2018. If any customer is login then that time is not counted as idle. How

LEAD/LAG SQL Server 2012/Gaps and Islands

一世执手 提交于 2019-12-13 06:06:55
问题 I'm having a few issues with LEAD/LAG. For each row within a set of IDs I'm wanting to get the previous/next source where isAQI = 1. Desired output is as follows in prevAQI and nextAQI columns. I've tried the same approach as Lag() with conditon in sql server, but with no luck. Any help would be much appreciated! Sample data as follows: DECLARE @a TABLE ( id int, timest datetime, source char(2), isAQI int, prevAQI char(2), nextAQI char(2)) INSERT @a VALUES (6694 ,'2015-06-11 08:55:06.000' ,'I

SQL: create sequential list of numbers from various starting points

余生长醉 提交于 2019-12-12 16:39:19
问题 I'm stuck on this SQL problem. I have a column that is a list of starting points ( prevdoc ), and anther column that lists how many sequential numbers I need after the starting point ( exdiff ). For example, here are the first several rows: prevdoc | exdiff ---------------- 1 | 3 21 | 2 126 | 2 So I need an output to look something like: 2 3 4 22 23 127 128 I'm lost as to where even to start. Can anyone advise me on the SQL code for this solution? Thanks! 回答1: ;with a as ( select prevdoc + 1

Filling in missing dates DB2 SQL

大城市里の小女人 提交于 2019-12-12 06:27:09
问题 My initial query looks like this: select process_date, count(*) batchCount from T1.log_comments order by process_date asc; I need to be able to do some quick analysis for weekends that are missing, but wanted to know if there was a quick way to fill in the missing dates not present in process_date . I've seen the solution here but am curious if there's any magic hidden in db2 that could do this with only a minor modification to my original query. 回答1: Note: Not tested, framed it based on my

Number of Groups/Islands of 1's in a Matrix: Definition clarification

核能气质少年 提交于 2019-12-12 04:39:05
问题 I am studying various solutions for Number of Groups (or "islands") of 1's in a Matrix, and while the following clear & concise Java solution looks in the right direction, it also looks incomplete to me: /* * Given a matrix of 0's and 1's, * find the number of groups of 1's in the matrix. * * A group of 1's is defined as all ADJACENT 1's * vertically or horizontally but not diagonally. */ public class Islands { /** * main entry point */ public static void main(String[] args) { int[][] A = new

Teradata SQL how to transfer “by date” to by “date range”?

风流意气都作罢 提交于 2019-12-11 19:14:38
问题 I have 600 Million rows as table 1 below. In Teradata SQL how to transfer "by date" to by "date range"? +-----------+-------+------------+----------+ | ProductID | Store | Trans_Date | Cost_Amt | +-----------+-------+------------+----------+ | 20202 | 2320 | 2018-01-02 | $9.23 | | 20202 | 2320 | 2018-01-03 | $9.23 | | 20202 | 2320 | 2018-01-04 | $9.23 | | 20202 | 2320 | 2018-01-05 | $9.38 | | 20202 | 2320 | 2018-01-06 | $9.38 | | 20202 | 2320 | 2018-01-07 | $9.38 | | 20202 | 2320 | 2018-01-08

How to tag a group of repeating items if the ids are consecutive for n rows?

浪子不回头ぞ 提交于 2019-12-11 18:55:26
问题 Building in the test for consecutive ids is proving difficult without breaking it down into parts or using a cursor which I'd like to avoid. pseudo query - SELECT all FROM table with the same description on multiple adjacent rows for >= 4 rows and set tag = 'y' and order by id (id,description, tag), (1, 'xxx', 'n'), (2, 'xxx', 'n'), (3, 'xxx', 'n'), (7, 'xxx', 'n'), (5, 'xxx', 'n'), (8, 'xxx', 'n'), (4, 'xxx', 'n'), (6, 'zzz', 'n') desired result (1, 'xxx', 'y') (2, 'xxx', 'y') (3, 'xxx', 'y'

How to group following rows by not unique value

大兔子大兔子 提交于 2019-12-11 11:46:49
问题 I have data like this: table1 _____________ id way time 1 1 00:01 2 1 00:02 3 2 00:03 4 2 00:04 5 2 00:05 6 3 00:06 7 3 00:07 8 1 00:08 9 1 00:09 I would like to know in which time interval I was on which way: desired output _________________ id way from to 1 1 00:01 00:02 3 2 00:03 00:05 6 3 00:06 00:07 8 1 00:08 00:09 I tried to use a window function: SELECT DISTINCT first_value(id) OVER w AS id, first_value(way) OVER w as way, first_value(time) OVER w as from, last_value(time) OVER w as to