gaps-and-islands

Group data by the change of grouping column value in order

末鹿安然 提交于 2019-11-27 20:12:38
With the following data create table #ph (product int, [date] date, price int) insert into #ph select 1, '20120101', 1 insert into #ph select 1, '20120102', 1 insert into #ph select 1, '20120103', 1 insert into #ph select 1, '20120104', 1 insert into #ph select 1, '20120105', 2 insert into #ph select 1, '20120106', 2 insert into #ph select 1, '20120107', 2 insert into #ph select 1, '20120108', 2 insert into #ph select 1, '20120109', 1 insert into #ph select 1, '20120110', 1 insert into #ph select 1, '20120111', 1 insert into #ph select 1, '20120112', 1 I would like to produce the following

SQL Query to show gaps between multiple date ranges

孤者浪人 提交于 2019-11-27 18:24:50
Im working on a SSRS / SQL project and trying to write a query to get the gaps between dates and I am completely lost with how to write this.Basically we have a number of devices which can be scheduled for use and I need a report to show when they are not in use. I have a table with Device ID, EventStart and EventEnd times, I need to run a query to get the times between these events for each device but I am not really sure how to do this. For example: Device 1 Event A runs from `01/01/2012 08:00 - 01/01/2012 10:00` Device 1 Event B runs from `01/01/2012 18:00 - 01/01/2012 20:00` Device 1 Event

SQL to determine minimum sequential days of access?

本小妞迷上赌 提交于 2019-11-27 16:36:22
The following User History table contains one record for every day a given user has accessed a website (in a 24 hour UTC period). It has many thousands of records, but only one record per day per user. If the user has not accessed the website for that day, no record will be generated. Id UserId CreationDate ------ ------ ------------ 750997 12 2009-07-07 18:42:20.723 750998 15 2009-07-07 18:42:20.927 751000 19 2009-07-07 18:42:22.283 What I'm looking for is a SQL query on this table with good performance , that tells me which userids have accessed the website for (n) continuous days without

How to group continuous ranges using MySQL

点点圈 提交于 2019-11-27 15:36:49
I have a table that contains categories, dates and rates. Each category can have different rates for different dates, one category can have only one rate at a given date. Id CatId Date Rate ------ ------ ------------ --------- 000001 12 2009-07-07 1 000002 12 2009-07-08 1 000003 12 2009-07-09 1 000004 12 2009-07-10 2 000005 12 2009-07-15 1 000006 12 2009-07-16 1 000007 13 2009-07-08 1 000008 13 2009-07-09 1 000009 14 2009-07-07 2 000010 14 2009-07-08 1 000010 14 2009-07-10 1 Unique index (catid, Date, Rate) I would like for each category to group all continuous dates ranges and keep only the

Detect consecutive dates ranges using SQL

左心房为你撑大大i 提交于 2019-11-27 12:43:32
I want to fill the calendar object which requires start and end date information. I have one column which contains a sequence of dates. Some of the dates are consecutive (have one day difference) and some are not. InfoDate 2013-12-04 consecutive date [StartDate] 2013-12-05 consecutive date 2013-12-06 consecutive date [EndDate] 2013-12-09 [startDate] 2013-12-10 [EndDate] 2014-01-01 [startDate] 2014-01-02 2014-01-03 [EndDate] 2014-01-06 [startDate] 2014-01-07 [EndDate] 2014-01-29 [startDate] 2014-01-30 2014-01-31 [EndDate] 2014-02-03 [startDate] 2014-02-04 [EndDate] I want to pick each

Change number column

时光总嘲笑我的痴心妄想 提交于 2019-11-27 08:18:45
问题 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

How to find the boundaries of groups of contiguous sequential numbers?

让人想犯罪 __ 提交于 2019-11-27 07:43:13
I have a table with the following definition CREATE TABLE mytable ( id INT IDENTITY(1, 1) PRIMARY KEY, number BIGINT, status INT ) and example data INSERT INTO mytable VALUES (100,0), (101,0), (102,0), (103,0), (104,1), (105,1), (106,0), (107,0), (1014,0), (1015,0), (1016,1), (1017,0) Looking only at the rows where status = 0 how can I collapse the Number values into ranges of contiguous sequential numbers and find the start and end of each range? i.e. For the example data the results would be FROM to Number 100 103 Number 106 107 Number 1014 1015 Number 1017 1017 As mentioned in the comments

MySQL: Find Missing Dates Between a Date Range

一笑奈何 提交于 2019-11-27 04:44:18
问题 I need some help with a mysql query. I've got db table that has data from Jan 1, 2011 thru April 30, 2011. There should be a record for each date. I need to find out whether any date is missing from the table. So for example, let's say that Feb 2, 2011 has no data. How do I find that date? I've got the dates stored in a column called reportdatetime. The dates are stored in the format: 2011-05-10 0:00:00, which is May 5, 2011 12:00:00 am. Any suggestions? 回答1: This is a second answer, I'll

Group data by the change of grouping column value in order

对着背影说爱祢 提交于 2019-11-27 04:26:12
问题 With the following data create table #ph (product int, [date] date, price int) insert into #ph select 1, '20120101', 1 insert into #ph select 1, '20120102', 1 insert into #ph select 1, '20120103', 1 insert into #ph select 1, '20120104', 1 insert into #ph select 1, '20120105', 2 insert into #ph select 1, '20120106', 2 insert into #ph select 1, '20120107', 2 insert into #ph select 1, '20120108', 2 insert into #ph select 1, '20120109', 1 insert into #ph select 1, '20120110', 1 insert into #ph

How to Determine Values for Missing Months based on Data of Previous Months in T-SQL

杀马特。学长 韩版系。学妹 提交于 2019-11-27 02:28:00
问题 I have a set of transactions occurring at specific points in time: CREATE TABLE Transactions ( TransactionDate Date NOT NULL, TransactionValue Integer NOT NULL ) The data might be: INSERT INTO Transactions (TransactionDate, TransactionValue) VALUES ('1/1/2009', 1) INSERT INTO Transactions (TransactionDate, TransactionValue) VALUES ('3/1/2009', 2) INSERT INTO Transactions (TransactionDate, TransactionValue) VALUES ('6/1/2009', 3) Assuming that the TransactionValue sets some kind of level, I