gaps-and-islands

Select all integers that are not already in table in postgres

↘锁芯ラ 提交于 2019-12-11 09:33:32
问题 I have some ids in a table, but there are gaps in between. I want to select these gaps. For example, the integer numbers in my table are: 1 2 5 9 15 And I want to select: 3 4 6 7 8 10 11 12 13 14 My version of PostgreSQL is 9.1.1, so I cannot use int4range. 回答1: Use generate_series() and LEFT JOIN to the table: SELECT g.nr FROM generate_series(1,15) g(nr) LEFT JOIN tbl USING (nr) WHERE tbl.nr IS NULL; Replace all occurrences of nr with your actual column name. Or use one of the other basic

How to calculate changes in Oracle sql

流过昼夜 提交于 2019-12-11 06:49:51
问题 I have the following table with the following columns: HID_1 HID_2 Attr1 Attr2 Attr3 Attr4 Attr5 123 111 wo e ak ERR 20180630 123 111 wo e ak ERR 20180730 123 111 wo e ak ERR 20180830 123 111 qe e ak ERR 20180930 123 111 qe e ak ERR 20181030 123 111 aa a ak ERR 20181130 Where HID_1 and HID_2 are hash-id ad other 4 columns are defined by the group by statement and the last one is time_id(date of the last day of the month). In general in this table I have much more records with a lot of

SQL query to return nil for dates not present in the table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 04:22:33
问题 I have a table 'my_table'. It has the following data : ID --- Date 1 --- 01/30/2012 2 --- 01/30/2012 3 --- 05/30/2012 I can write a SQL query to return the count of ID's between certain dates, grouped by month, like this : {"01/30/2012" => 2, "05/30/2012" => 1} How can I get a result which has all the missing months between the requested dates with value '0', like this : {"01/30/2012" => 2, "02/30/2012" => 0, "03/30/2012" => 0, "04/30/2012" => 0, "05/30/2012" => 1} Thanks in advance. 回答1: The

SQL Transpose rows to columns without aggregate

做~自己de王妃 提交于 2019-12-11 04:13:44
问题 I know there is lot of answers but none of my use. My table looked like where we store meter on and off status on given time. After every Off status there is an On status. We want to transpose table like this MeterNo | [power off] | [power on] ____________________________________________________________________ x0039938 | 2016-10-08 14:14:37.610 | 2016-10-08 14:17:15.047 ____________________________________________________________________ x0039938 | 2016-10-08 14:20:50.257 | NULL ____________

Enumerating table partitions in Postgres table

风格不统一 提交于 2019-12-11 03:33:59
问题 Suppose I have a table like this: id | part | value ----+-------+------- 1 | 0 | 8 2 | 0 | 3 3 | 0 | 4 4 | 1 | 6 5 | 0 | 13 6 | 0 | 4 7 | 1 | 2 8 | 0 | 11 9 | 0 | 15 10 | 0 | 3 11 | 0 | 2 I would like to enumerate groups that have part atribute 0. Ultimately I want to get this: id | part | value | number ----+-------+----------------- 1 | 0 | 8 | 1 2 | 0 | 3 | 2 3 | 0 | 4 | 3 4 | 1 | 6 | 0 5 | 0 | 13 | 1 6 | 0 | 4 | 2 7 | 1 | 2 | 0 8 | 0 | 11 | 1 9 | 0 | 15 | 2 10 | 0 | 3 | 3 11 | 0 | 2 | 4

SQL, finding discrete overlapping time intervals of multiple ranges against one “key” interval & calculate “most restrictive” common overlaps?

北城余情 提交于 2019-12-11 03:18:27
问题 I'm working in SQL Server Management Studio so I suppose this is a Microsoft SQL Server T-SQL question. The real world scenario is this: I have multiple employees working across multiple locations and the "time in" and "time out" records for each. I have already created a unique "Shift ID" for each set of time intervals and joined, based on employee, date, and location the shifts of other employees that match my keystone employee, or the one against which I am comparing everyone else.

How to find a gap in range in SQL

这一生的挚爱 提交于 2019-12-11 01:34:51
问题 This question explains how to find the first "unused" number in a table, but how can I find the same so that I can define extra constraints. How do I alter the query so that I get the first unused number after that's greater than 100 e.g. If I have 23, 56, 100, 101, 103 in my table i should get 102. 回答1: in mysql and postgresql SELECT id + 1 FROM test mo WHERE NOT EXISTS ( SELECT NULL FROM test mi WHERE mi.id = mo.id + 1 ) and mo.id> 100 ORDER BY id LIMIT 1 fiddle for mysql and fiddle for

How to identify the first gap in multiple start and end date ranges for each distinct member in T-SQL

泄露秘密 提交于 2019-12-10 13:53:32
问题 I have been working on the below but getting no results and the deadline is fast approaching. Also, there are over a million rows as the below. Appreciate your help on the below. Objective: Group results by MEMBER and build Continuous Coverage Ranges for each Member by combining individual Date Ranges which either overlap or run consecutive to each other with no breaks between the Start & End day of the range. I have data in the below format: MemberCode ----- ClaimID ----- StartDate -----

Determine if a range is completely covered by a set of ranges

与世无争的帅哥 提交于 2019-12-10 11:20:00
问题 How can I check if a range is completely covered by a set of ranges. In the following example: WITH ranges(id, a, b) AS ( SELECT 1, 0, 40 UNION SELECT 2, 40, 60 UNION SELECT 3, 80, 100 UNION SELECT 4, 10, 30 ), tests(id, a, b) AS ( SELECT 1, 10, 90 UNION SELECT 2, 10, 60 ) SELECT * FROM tests WHERE -- ? I want to select 10, 60 because all of it is covered by 0, 40 and 40, 60 (and 10, 30 ) I want to exclude 10, 90 because it is exposed between 60, 80 Assume that a is inclusive and b is

PostgreSQL - column value changed - select query optimization

老子叫甜甜 提交于 2019-12-10 03:56:22
问题 Say we have a table: CREATE TABLE p ( id serial NOT NULL, val boolean NOT NULL, PRIMARY KEY (id) ); Populated with some rows: insert into p (val) values (true),(false),(false),(true),(true),(true),(false); ID VAL 1 1 2 0 3 0 4 1 5 1 6 1 7 0 I want to determine when the value has been changed. So the result of my query should be: ID VAL 2 0 4 1 7 0 I have a solution with joins and subqueries: select min(id) id, val from ( select p1.id, p1.val, max(p2.id) last_prev from p p1 join p p2 on p2.id