gaps-and-islands

find start and stop date for contiguous dates in multiple rows

一曲冷凌霜 提交于 2019-12-18 18:38:46
问题 I'm having a bit of trouble displaying correct data from my table. Im not really sure what to search for either. Im not sure min(column) or max(column) will help me here. Lets see if i can explain my problem. My table contains this data: > Code (nvarchar) | DateFrom (datetime) | DateTo (datetime) > ========================================================= > 3006 | 2014-06-18 07:00:00 | 2014-06-18 08:00:00 > 3006 | 2014-06-18 09:00:00 | 2014-06-18 22:00:00 > 1006 | 2014-06-18 07:00:00 | 2014

SQL query that returns all dates not used in a table

偶尔善良 提交于 2019-12-18 05:06:27
问题 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

Get list with start and end values from table of datetimes

好久不见. 提交于 2019-12-18 04:45:14
问题 Currently i have a table built up like this way DeviceID Timestamp Value ---------------------------------------- Device1 1.1.2011 10:00:00 3 Device1 1.1.2011 10:00:01 4 Device1 1.1.2011 10:00:02 4 Device1 1.1.2011 10:00:04 3 Device1 1.1.2011 10:00:05 4 Device1 1.1.2011 14:23:14 8 Device1 1.1.2011 14:23:15 7 Device1 1.1.2011 14:23:17 4 Device1 1.1.2011 14:23:18 2 As you can see there are coming in some values from a device with a given timestamp (the column type is datetime). The problem is

SQL Query to show gaps between multiple date ranges

牧云@^-^@ 提交于 2019-12-17 15:32:05
问题 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

Insert Dates in the return from a query where there is none

岁酱吖の 提交于 2019-12-17 09:55:14
问题 We are building a query to count the number of events per hour, per day. Most days there are hours that do not have any activity and therefore where the query is run the count of activities per hour show up but there are gaps and the query excludes these. We still want to show the hours that do not have activity and display a zero so that zero value can then be charted. The query we using looks like this … select datepart(Year, dev_time) as Year, datepart(Month, dev_time) as Month, datepart

Check for x consecutive days - given timestamps in database

假如想象 提交于 2019-12-17 04:57:15
问题 Could anybody give me an idea or hint how you could check for X consecutive days in a database table (MySQL) where logins (user id, timestamp) are stored? Stackoverflow does it (e.g. badges like Enthusiast - if you log in for 30 consecutive days or so...). What functions would you have to use or what is the idea of how to do it? Something like SELECT 1 FROM login_dates WHERE ... ? 回答1: You can accomplish this using a shifted self-outer-join in conjunction with a variable. See this solution:

SQL query to find Missing sequence numbers

杀马特。学长 韩版系。学妹 提交于 2019-12-17 04:25:31
问题 I have a column named sequence . The data in this column looks like 1, 2, 3, 4, 5, 7, 9, 10, 15. I need to find the missing sequence numbers from the table. What SQL query will find the missing sequence numbers from my table? I am expecting results like Missing numbers --------------- 6 8 11 12 13 14 I am using only one table. I tried the query below, but am not getting the results I want. select de.sequence + 1 as sequence from dataentry as de left outer join dataentry as de1 on de.sequence

How do I group on continuous ranges

╄→гoц情女王★ 提交于 2019-12-17 03:42:22
问题 I know some basic sql, but this one is beyond me. I have looked high and low but no dice. I need a view of the following data, I can do this in the application layer code. But unfortunately for this particular one, the code must be put in the data layer. I am using T-SQL. Table Date Crew DayType 01-02-11 John Doe SEA 02-02-11 John Doe SEA 03-02-11 John Doe SEA 04-02-11 John Doe HOME 05-02-11 John Doe HOME 06-02-11 John Doe SEA I need a view like this DateFrom DateTo Name DayType 01-02-11 03

GROUP BY and aggregate sequential numeric values

帅比萌擦擦* 提交于 2019-12-17 03:16:43
问题 Using PostgreSQL 9.0. Let's say I have a table containing the fields: company , profession and year . I want to return a result which contains unique companies and professions, but aggregates (into an array is fine) years based on numeric sequence: Example Table: +-----------------------------+ | company | profession | year | +---------+------------+------+ | Google | Programmer | 2000 | | Google | Sales | 2000 | | Google | Sales | 2001 | | Google | Sales | 2002 | | Google | Sales | 2004 | |

SQL Server : Consecutive Rows Issue

*爱你&永不变心* 提交于 2019-12-14 00:56:41
问题 I'm having difficulty with a consecutive rows query, and need some assistance. I have a table that is part of a telephony switch. It tracks every employee who logs into a phone (and the phones extension). I have another table that tells me where that phone is (across multiple sites). The logic we're going for is if someone logs into a particular phone/site for 2 consecutive days, we'll trigger an event to update their employee record to make this site their primary location. The issue I'm