gaps-and-islands

Group close numbers

风格不统一 提交于 2020-01-12 04:41:09
问题 I have a table with 2 columns of integers. The first column represents start index and the second column represents end index. START END 1 8 9 13 14 20 20 25 30 42 42 49 60 67 Simple So far. What I would like to do is group all the records that follow together: START END 1 25 30 49 60 67 A record can follow by Starting on the same index as the previous end index or by a margin of 1: START END 1 10 10 20 And START END 1 10 11 20 will both result in START END 1 20 I'm using SQL Server 2008 R2.

Group close numbers

你说的曾经没有我的故事 提交于 2020-01-12 04:40:25
问题 I have a table with 2 columns of integers. The first column represents start index and the second column represents end index. START END 1 8 9 13 14 20 20 25 30 42 42 49 60 67 Simple So far. What I would like to do is group all the records that follow together: START END 1 25 30 49 60 67 A record can follow by Starting on the same index as the previous end index or by a margin of 1: START END 1 10 10 20 And START END 1 10 11 20 will both result in START END 1 20 I'm using SQL Server 2008 R2.

Sequential Group By in sql server

巧了我就是萌 提交于 2020-01-11 07:47:29
问题 For this Table: +----+--------+-------+ | ID | Status | Value | +----+--------+-------+ | 1 | 1 | 4 | | 2 | 1 | 7 | | 3 | 1 | 9 | | 4 | 2 | 1 | | 5 | 2 | 7 | | 6 | 1 | 8 | | 7 | 1 | 9 | | 8 | 2 | 1 | | 9 | 0 | 4 | | 10 | 0 | 3 | | 11 | 0 | 8 | | 12 | 1 | 9 | | 13 | 3 | 1 | +----+--------+-------+ I need to sum sequential groups with the same Status to produce this result. +--------+------------+ | Status | Sum(Value) | +--------+------------+ | 1 | 20 | | 2 | 8 | | 1 | 17 | | 2 | 1 | | 0 | 15

GROUP BY for continuous rows in SQL

这一生的挚爱 提交于 2020-01-09 10:10:27
问题 Given the following table: ID State Date 12 1 2009-07-16 10:00 45 2 2009-07-16 13:00 67 2 2009-07-16 14:40 77 1 2009-07-16 15:00 89 1 2009-07-16 15:30 99 1 2009-07-16 16:00 Question: How can i GROUP by the field "State", while still maintaining the borders between the state changes? SELECT MIN(ID) AS ID, State, MIN(Date) AS Date, COUNT(ID) AS Count FROM table GROUP BY State results in the following: ID State Date Count 12 1 2009-07-16 10:00 4 45 2 2009-07-16 13:00 2 but this is expected: ID

GROUP BY for continuous rows in SQL

独自空忆成欢 提交于 2020-01-09 10:10:10
问题 Given the following table: ID State Date 12 1 2009-07-16 10:00 45 2 2009-07-16 13:00 67 2 2009-07-16 14:40 77 1 2009-07-16 15:00 89 1 2009-07-16 15:30 99 1 2009-07-16 16:00 Question: How can i GROUP by the field "State", while still maintaining the borders between the state changes? SELECT MIN(ID) AS ID, State, MIN(Date) AS Date, COUNT(ID) AS Count FROM table GROUP BY State results in the following: ID State Date Count 12 1 2009-07-16 10:00 4 45 2 2009-07-16 13:00 2 but this is expected: ID

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

左心房为你撑大大i 提交于 2020-01-09 03:20:17
问题 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

SQL Server find overlaping date ranges

*爱你&永不变心* 提交于 2020-01-05 14:03:16
问题 I have a table with date ranges (seasons) for each year. The normal is that the end of the one season is the beggining of the next season. In the example below I have in bold the two irregular season setups. In the first the end of season 1 is a day after the beggining of season 2. In the second, the beggining of season 4 is one day after the end of seaon 3 +--------+----+--------------+--------------+ |SEASONID|YEAR|DATE FROM |DATE TO | +--------+----+--------------+--------------+ |1 |14 |

MySQL WEEK() : Get all weeks in date range (with/without records)

微笑、不失礼 提交于 2020-01-05 11:40:14
问题 Here's an sql query I am using to get count of records in a table separated by week (only date is stored in table). It works as expected. SELECT count(id), CONCAT('Week ',WEEK(complaintRaisedDate)) week FROM events WHERE categoryId=1 GROUP BY week ORDER BY week This yields result like count(id) week ---------- | ---------- 1 Week 36 2 Week 40 1 Week 41 How I want the result to be is below: count(id) week ---------- | ---------- 1 Week 36 0 Week 37 0 Week 38 0 Week 39 2 Week 40 1 Week 41 That

MySQL how to fill missing hours/dates in range?

倖福魔咒の 提交于 2020-01-05 09:34:55
问题 I'm trying to extract data according to Day of Week & also Hour of day in the same statement (so I can see how many visits I got per hour and over a week. Here's the statement. SELECT count(id) as count, HOUR(created) as hour_of_day, WEEKDAY(created) as day_of_week, DATE_FORMAT(created,'%W') name_of_day FROM visitors GROUP BY day_of_week,hour_of_day ORDER BY day_of_week,hour_of_day ASC Some sample data count hour_of_day day_of_week name_of_day 2 0 0 Monday 1 1 0 Monday 1 4 0 Monday 4 5 0

Dense_Rank ordering

痞子三分冷 提交于 2020-01-05 04:21:13
问题 I've read a few variations of this question, but the solutions don't seem to be working. I wish to dynamically create a "Subgroup" for each "OrderNo" & "GroupID". Subgroups should be ordered by "OrderLine" eg: (Expected outcome) OrderNo OrderLine GroupID Subgroup ------------------------------------ 10463 1 798 1 10463 2 799 2 10463 3 797 3 10463 5 65 4 10463 6 65 4 10463 7 65 4 10481 4 917 1 10481 5 918 2 10481 6 131 3 10481 7 131 3 10481 8 131 3 10481 9 130 4 I've used Dense_Rank() to