gaps-and-islands

SQL Query to fill missing gaps across time and get last non-null value

做~自己de王妃 提交于 2020-01-03 03:02:48
问题 I have the following table in my database: Month|Year | Value 1 |2013 | 100 4 |2013 | 101 8 |2013 | 102 2 |2014 | 103 4 |2014 | 104 How can I fill in "missing" rows from the data, so that if I query from 2013-03 through 2014-03, I would get: Month|Year | Value 3 |2013 | 100 4 |2013 | 101 5 |2013 | 101 6 |2013 | 101 7 |2013 | 101 8 |2013 | 102 9 |2013 | 102 10 |2013 | 102 11 |2013 | 102 12 |2013 | 102 1 |2014 | 102 2 |2014 | 103 3 |2014 | 103 As you can see I want to repeat the previous Value

Return rows of the latest 'streak' of data

你说的曾经没有我的故事 提交于 2020-01-01 12:21:06
问题 Given a simple table with the following data: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 4 | W | 2012-01-04 3 | W | 2012-01-03 2 | L | 2012-01-02 1 | W | 2012-01-01 How would I write a query to return the lastest losing or winning streak of rows using PostgreSQL? In this case, I'm looking for a result of: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 I'm guessing the answer

Return rows of the latest 'streak' of data

独自空忆成欢 提交于 2020-01-01 12:21:03
问题 Given a simple table with the following data: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 4 | W | 2012-01-04 3 | W | 2012-01-03 2 | L | 2012-01-02 1 | W | 2012-01-01 How would I write a query to return the lastest losing or winning streak of rows using PostgreSQL? In this case, I'm looking for a result of: id | result | played ----+--------+------------ 7 | L | 2012-01-07 6 | L | 2012-01-06 5 | L | 2012-01-05 I'm guessing the answer

Crosstabbing rows based on sequence islands in a list of times

╄→尐↘猪︶ㄣ 提交于 2020-01-01 06:52:45
问题 Just writing the question title hurt my head! Please bear with me! Please see the following DDL & Dummy Data: CREATE TABLE [dbo].[tbl_Example]( [Date] [date] NULL, [Time] [time](0) NULL, [Branch] [varchar](3) NULL, [Count] [int] NULL ) ON [PRIMARY] GO INSERT [dbo].[tbl_Example] ([Date], [Time], [Branch], [Count]) VALUES (CAST(0x12350B00 AS Date), CAST(0x00907E0000000000 AS Time), N'001', 0) INSERT [dbo].[tbl_Example] ([Date], [Time], [Branch], [Count]) VALUES (CAST(0x12350B00 AS Date), CAST

Querying for a 'run' of consecutive columns in Postgres

孤街浪徒 提交于 2019-12-30 11:33:11
问题 I have a table: create table table1 (event_id integer, event_time timestamp without time zone); insert into table1 (event_id, event_time) values (1, '2011-01-01 00:00:00'), (2, '2011-01-01 00:00:15'), (3, '2011-01-01 00:00:29'), (4, '2011-01-01 00:00:58'), (5, '2011-01-02 06:03:00'), (6, '2011-01-02 06:03:09'), (7, '2011-01-05 11:01:31'), (8, '2011-01-05 11:02:15'), (9, '2011-01-06 09:34:19'), (10, '2011-01-06 09:34:41'), (11, '2011-01-06 09:35:06'); I would like to construct a statement that

Querying for a 'run' of consecutive columns in Postgres

泪湿孤枕 提交于 2019-12-30 11:33:06
问题 I have a table: create table table1 (event_id integer, event_time timestamp without time zone); insert into table1 (event_id, event_time) values (1, '2011-01-01 00:00:00'), (2, '2011-01-01 00:00:15'), (3, '2011-01-01 00:00:29'), (4, '2011-01-01 00:00:58'), (5, '2011-01-02 06:03:00'), (6, '2011-01-02 06:03:09'), (7, '2011-01-05 11:01:31'), (8, '2011-01-05 11:02:15'), (9, '2011-01-06 09:34:19'), (10, '2011-01-06 09:34:41'), (11, '2011-01-06 09:35:06'); I would like to construct a statement that

Detect SQL island over multiple parameters and conditions

余生颓废 提交于 2019-12-29 06:29:37
问题 (PostgreSQL 8.4) I got a great introduction to SQL gaps-and-islands here on Stack Overflow but I still have a question. Many island detection CTEs are based on a running order of a timestamp and some flag which breaks the sequence when it changes. But what if the "break" condition is a little more complex? CREATE TABLE T1 ( id SERIAL PRIMARY KEY, val INT, -- some device status INT -- 0=OFF, 1=ON ); INSERT INTO T1 (val, status) VALUES (10, 1); INSERT INTO T1 (val, status) VALUES (10, 0);

Query Records and Group by a block of time

馋奶兔 提交于 2019-12-29 05:34:14
问题 I have an application that may be run several times a day. Each run results in data that is written to a table to report on events that occurred. The main report table looks something like this: Id SourceId SourceType DateCreated 5048 433 FILE 5/17/2011 9:14:12 AM 5049 346 FILE 5/17/2011 9:14:22 AM 5050 444 FILE 5/17/2011 9:14:51 AM 5051 279 FILE 5/17/2011 9:15:02 AM 5052 433 FILE 5/17/2011 12:34:12 AM 5053 346 FILE 5/17/2011 12:34:22 AM 5054 444 FILE 5/17/2011 12:34:51 AM 5055 279 FILE 5/17

How to find missing rows (dates) in a mysql table?

瘦欲@ 提交于 2019-12-29 01:16:07
问题 I have tried several topics like this one: How to find missing data rows using SQL? here, but I couldn't make it work in my situation. I have a table named posts in MySQL which I save user diaries in it every day. Sometimes users forget to write a post for a day and I want to make it possible for them to submit it later. So the db structures like this: date userid 2011-10-01 1 2011-10-02 1 (missing) 2011-10-04 1 2011-10-05 1 (missing) 2011-10-07 1 So I want to show a dropdown list of missing

Change number column

◇◆丶佛笑我妖孽 提交于 2019-12-25 09:47:42
问题 I have NAME and PAY, but I need CHANGEGROUP in this example: NAME PAY DATE CHANGEGROUP Sally 12 10/01/2011 1 Sally 12 10/01/2011 1 Sally 12 11/02/2011 1 Sally 12 11/02/2011 1 Sally 12 12/01/2012 1 Sally 13 04/23/2013 2 Sally 12 04/24/2013 3 Sally 10 05/01/2013 4 Sally 10 10/01/2014 4 I tried RANK() and DENSE_RANK() , but they group according to the value - because pay goes down, it messes up my grouping. I saw this but it's not compatible with this older version of SQL 2005 回答1: This is a