gaps-and-islands

Transform Dates into Date Range in MYSQL---how to handle gaps in the dates

我怕爱的太早我们不能终老 提交于 2019-12-23 05:19:55
问题 I am looking for a help in transforming below data into the required output. We have data at Item,LOC DAY level data which needs to be transformed to Item,Loc Date Range to reduce the number of records in the table and for other requirements. Item LOC RP_DATE RP_IND 1003785256 543 2016-11-05 Y 1003785256 543 2016-11-06 Y 1003785256 543 2016-11-07 Y 1003785256 543 2016-11-09 Y 1003785256 543 2016-11-10 Y 1003790365 150 2016-11-05 Y 1003797790 224 2016-11-05 Y 1003797790 224 2016-11-06 Y

SQL Count Grouping by a sequence of numbers

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 04:35:09
问题 i have a SQL table like this: id pNum ----- -------- 100 12 100 13 100 15 100 16 100 17 200 18 200 19 300 20 300 21 300 25 and i want to group by the id and the pNum sequences, and count the number of rows. having a result like this. id res ----- -------- 100 2 100 3 200 2 300 2 300 1 any idea on how to do it? 回答1: If your DBMS supports window functions (e.g. SQL Server 2005+) SELECT id, count(*) AS res FROM (SELECT *, [pNum] - ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY [pNum]) AS Grp FROM

SQL Count Grouping by a sequence of numbers

Deadly 提交于 2019-12-23 04:35:08
问题 i have a SQL table like this: id pNum ----- -------- 100 12 100 13 100 15 100 16 100 17 200 18 200 19 300 20 300 21 300 25 and i want to group by the id and the pNum sequences, and count the number of rows. having a result like this. id res ----- -------- 100 2 100 3 200 2 300 2 300 1 any idea on how to do it? 回答1: If your DBMS supports window functions (e.g. SQL Server 2005+) SELECT id, count(*) AS res FROM (SELECT *, [pNum] - ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY [pNum]) AS Grp FROM

Calculate time between On and Off Status rows SQL Server

╄→гoц情女王★ 提交于 2019-12-22 17:12:18
问题 I´m using SQL Server 2008, how can I calculate the time between On and Off status? I have the following table (ordered by timestamp ): ID | EQUIP_ID | TIMESTAMP | STATUS (1 on/0 off) 1 | 1 | 21/05/2012 13:00:00 | 1 3 | 1 | 21/05/2012 13:04:00 | 1 4 | 1 | 21/05/2012 13:05:00 | 0 6 | 1 | 21/05/2012 13:09:00 | 1 7 | 1 | 21/05/2012 13:10:00 | 1 9 | 1 | 21/05/2012 13:12:00 | 1 10 | 1 | 21/05/2012 13:13:00 | 0 10 | 1 | 21/05/2012 13:14:00 | 1 10 | 1 | 21/05/2012 13:15:00 | 0 And I expect a result

Finding a 'run' of rows from an ordered result set

社会主义新天地 提交于 2019-12-22 13:53:32
问题 I'm trying to figure out a way of identifying a "run" of results (successive rows, in order) that meet some condition. Currently, I'm ordering a result set, and scanning by eye for particular patterns. Here's an example: SELECT the_date, name FROM orders WHERE the_date BETWEEN to_date('2013-09-18',..) AND to_date('2013-09-22', ..) ORDER BY the_date -------------------------------------- the_date | name -------------------------------------- 2013-09-18 00:00:01 | John -------------------------

mysql date list with count even if no data on specific date [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-22 10:59:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: MySQL how to fill missing dates in range? I'm trying to make a graph from mysqldata, Postid | date | text 1 | 2012-01-01 | bla 2 | 2012-01-01 | bla 3 | 2012-01-02 | bla 4 | 2012-01-02 | bla 5 | 2012-01-04 | bla 6 | 2012-01-04 | bla 7 | 2012-01-05 | bla Now, I'd like to get the number of posts on every day, INCLUDING dates with zero. For example, i'd like to be able to get the first week like this: date | count

How to count number of non-consecutive values in a column using SQL?

半腔热情 提交于 2019-12-22 10:54:08
问题 Following-up on my question here. Say I have a table in an Oracle database like the one below (table_1) which tracks service involvement for a particular individual: name day srvc_ inv bill 1 1 bill 2 1 bill 3 0 bill 4 0 bill 5 1 bill 6 0 susy 1 1 susy 2 0 susy 3 1 susy 4 0 susy 5 1 My goal is to get a summary table which lists, for all unique individuals, whether there was service involvement and the number of distinct service episodes (in this case 2 for bill and 3 for susy), where a

Oracle SQL. What statement should I use

雨燕双飞 提交于 2019-12-22 10:35:07
问题 DATA given: inventory_num_id inventory_group_id num code 9681066 100003894 211 E 9679839 100003894 212 E 9687165 100003894 213 E 9680883 100003894 214 I 9710863 100003894 515 E 9681246 100003894 516 E 9682695 100003894 517 E 9681239 100003894 518 E 9685409 100003894 519 E 9679843 100003894 520 C 9679844 100003894 521 C 9714882 100003894 522 E 9679845 100003894 523 I 9681211 100003894 524 E 9681216 100003894 525 E 9682696 100003894 526 E 9681227 100003894 527 E Result examples should be like:

Islands and Gaps Issue

安稳与你 提交于 2019-12-22 08:44:43
问题 Backstory: I have a database that has data points of drivers in trucks which also contain the. While in a truck, the driver can have a 'driverstatus'. What I'd like to do is group these statuses by driver, truck. As of now, I've tried using LAG/LEAD to help. The reason for this is so I can tell when a driverstatus change occurs, and then I can mark that row as having the last datetime of that status. That in itself is insufficient, because I need to group the statuses by their status and date

Need help finding the correct T-SQL Query

可紊 提交于 2019-12-22 04:15:10
问题 I am not quite sure how to go about doing this. Basically I have have a table like this UserId DateRequested Approved ApprovedBy Notes ------------ ----------------------- -------- ----------- ----- 1 2011-05-26 0 NULL NULL 1 2011-05-27 0 NULL NULL 1 2011-05-28 0 NULL NULL 1 2011-06-05 0 NULL NULL 1 2011-06-06 0 NULL NULL 1 2011-06-25 0 NULL NULL Which basically contains the days an employee requests a holiday. Now, when a day or days is granted, this data needs to be copied over to a table