gaps-and-islands

Joining together consecutive date validity intervals

烂漫一生 提交于 2019-12-01 06:13:57
I have a series of records containing some information (product type) with temporal validity. I would like to meld together adjacent validity intervals, provided that the grouping information (the product type) stays the same. I cannot use a simple GROUP BY with MIN and MAX , because some product types ( A , in the example) can "go away" and "come back". Using Oracle 11g. A similar question for MySQL is: How can I do a contiguous group by in MySQL? Input data : | PRODUCT | START_DATE | END_DATE | |---------|----------------------------------|----------------------------------| | A | July, 01

How to add a running count to rows in a 'streak' of consecutive days

匆匆过客 提交于 2019-11-30 22:23:45
Thanks to Mike for the suggestion to add the create/insert statements. create table test ( pid integer not null, date date not null, primary key (pid, date) ); insert into test values (1,'2014-10-1') , (1,'2014-10-2') , (1,'2014-10-3') , (1,'2014-10-5') , (1,'2014-10-7') , (2,'2014-10-1') , (2,'2014-10-2') , (2,'2014-10-3') , (2,'2014-10-5') , (2,'2014-10-7'); I want to add a new column that is 'days in current streak' so the result would look like: pid | date | in_streak -------|-----------|---------- 1 | 2014-10-1 | 1 1 | 2014-10-2 | 2 1 | 2014-10-3 | 3 1 | 2014-10-5 | 1 1 | 2014-10-7 | 1 2

SQL Query to group items by time, but only if near each other?

故事扮演 提交于 2019-11-30 18:36:28
问题 I am trying to craft a SQL statement to pull sample values from a DB. The table contains values that pertain to tool changes in a CNC machine. The current statement I have pulls values properly, but only if there is one occurrence of the tool in for a given program. If the tool appears multiple times, the time values correspond from the first load, to the last load. There is only one TIME column, and by finding the first and last occurrence of it, I can determine a tools in/out time. Basic

How to add a running count to rows in a 'streak' of consecutive days

倖福魔咒の 提交于 2019-11-30 17:16:00
问题 Thanks to Mike for the suggestion to add the create/insert statements. create table test ( pid integer not null, date date not null, primary key (pid, date) ); insert into test values (1,'2014-10-1') , (1,'2014-10-2') , (1,'2014-10-3') , (1,'2014-10-5') , (1,'2014-10-7') , (2,'2014-10-1') , (2,'2014-10-2') , (2,'2014-10-3') , (2,'2014-10-5') , (2,'2014-10-7'); I want to add a new column that is 'days in current streak' so the result would look like: pid | date | in_streak -------|-----------|

MySQL: group by consecutive days and count groups

泄露秘密 提交于 2019-11-30 08:31:19
问题 I have a database table which holds each user's checkins in cities. I need to know how many days a user has been in a city, and then, how many visits a user has made to a city (a visit consists of consecutive days spent in a city). So, consider I have the following table (simplified, containing only the DATETIME s - same user and city): datetime ------------------- 2011-06-30 12:11:46 2011-07-01 13:16:34 2011-07-01 15:22:45 2011-07-01 22:35:00 2011-07-02 13:45:12 2011-08-01 00:11:45 2011-08

Oracle: select missing dates

﹥>﹥吖頭↗ 提交于 2019-11-29 22:04:37
问题 I have a table with (among other things) dates in a field. I need to get a list of all dates that are more recent than the oldest date, older than the most recent date, and are completely missing from the table. So, if the table contained: 2012-01-02 2012-01-02 2012-01-03 2012-01-05 2012-01-05 2012-01-07 2012-01-08 I want a query that returns: 2012-01-04 2012-01-06 回答1: Something like this (assuming your table is named your_table and the date column is named the_date ): with date_range as (

How to check a sequence efficiently for used and unused values in PostgreSQL

独自空忆成欢 提交于 2019-11-29 16:48:27
In PostgreSQL (9.3) I have a table defined as: CREATE TABLE charts ( recid serial NOT NULL, groupid text NOT NULL, chart_number integer NOT NULL, "timestamp" timestamp without time zone NOT NULL DEFAULT now(), modified timestamp without time zone NOT NULL DEFAULT now(), donotsee boolean, CONSTRAINT pk_charts PRIMARY KEY (recid), CONSTRAINT chart_groupid UNIQUE (groupid), CONSTRAINT charts_ichart_key UNIQUE (chart_number) ); CREATE TRIGGER update_modified BEFORE UPDATE ON charts FOR EACH ROW EXECUTE PROCEDURE update_modified(); I would like to replace the chart_number with a sequence like:

oracle sql query to list all the dates of previous month

☆樱花仙子☆ 提交于 2019-11-29 11:09:11
Guys i have a requirement to list all the dates of the previous month like below 20101201 20101202 20101203 20101204 20101205 .. .. .. .. .. .. .. .. 20101231 kindly let me know if any better way to do than this query. select TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1) as EACH_DATE from dual A connect by level < (TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD'))+1) Also please let me know the problem with this query it says "missing right parenthesis" SELECT /*+ PARALLEL (A,8) */ /*+ DRIVING_STATE */ TO_CHAR(TRUNC(TRUNC(SYSDATE,'MM')-1,'MM'),'MONYYYY') "MONTH", TYPE AS "TRAFF", COLUMN, A

SQL query that returns all dates not used in a table

一曲冷凌霜 提交于 2019-11-29 07:46:34
So lets say I have some records that look like: 2011-01-01 Cat 2011-01-02 Dog 2011-01-04 Horse 2011-01-06 Lion How can I construct a query that will return 2011-01-03 and 2011-01-05, ie the unused dates. I postdate blogs into the future and I want a query that will show me the days I don't have anything posted yet. It would look from the current date to 2 weeks into the future. Update: I am not too excited about building a permanent table of dates. After thinking about it though it seems like the solution might be to make a small stored procedure that creates a temp table. Something like:

MySQL: group by consecutive days and count groups

北慕城南 提交于 2019-11-29 06:47:03
I have a database table which holds each user's checkins in cities. I need to know how many days a user has been in a city, and then, how many visits a user has made to a city (a visit consists of consecutive days spent in a city). So, consider I have the following table (simplified, containing only the DATETIME s - same user and city): datetime ------------------- 2011-06-30 12:11:46 2011-07-01 13:16:34 2011-07-01 15:22:45 2011-07-01 22:35:00 2011-07-02 13:45:12 2011-08-01 00:11:45 2011-08-05 17:14:34 2011-08-05 18:11:46 2011-08-06 20:22:12 The number of days this user has been to this city