gaps-and-islands

How to generate list of all dates between sysdate-30 and sysdate+30?

大憨熊 提交于 2019-12-01 19:20:19
Purpose & What I've Got So Far I am attempting to create a view which checks for missing labor transactions. The view will be fed to a Crystal report. In this case, the view should take all dates between sysdate+30 and sysdate -30, and then should left outer join all labor records by active employees for each of those dates. It then gives a count of the number of labor transactions for each employee for each date. This gets passed to the Crystal Report, which will filter based on a specific date range (within the +/- 30 range by the view). From there, the count of all days will summed up per

wanted to get all dates in mysql result

南楼画角 提交于 2019-12-01 19:06:34
I have mysql table called user(id, name, join_on) join on is a date field what I want is to show in each day how many uses has been created I can use group by but it will only give me the dates when users get added like if date 4/12/10 5 users added 4/13/10 2 users added 4/15/10 7 users added here date 4/14/10 is missing and I want listing of all dates in one month. I have one solution for it by creating another table only for adding date and that table will left join my users table on join_on and will give total result but I don't want to do that as for creating that I need to create and add

wanted to get all dates in mysql result

我的未来我决定 提交于 2019-12-01 18:04:23
问题 I have mysql table called user(id, name, join_on) join on is a date field what I want is to show in each day how many uses has been created I can use group by but it will only give me the dates when users get added like if date 4/12/10 5 users added 4/13/10 2 users added 4/15/10 7 users added here date 4/14/10 is missing and I want listing of all dates in one month. I have one solution for it by creating another table only for adding date and that table will left join my users table on join

What is a good way to find gaps in a set of datespans?

自作多情 提交于 2019-12-01 18:02:13
问题 What is a way to find gaps in a set of date spans? For example, I have these date spans: 1/ 1/11 - 1/10/11 1/13/11 - 1/15/11 1/20/11 - 1/30/11 Then I have a start and end date of 1/7/11 and 1/14/11. I want to be able to tell that between 1/10/11 and 1/13/11 there is a gap so the start and end date is not possible. Or I want to return only the datespans up to the first gap encountered. If this can be done in SQL server that would be good. I was thinking to go through each date to find out if

Sequential Group By in sql server

邮差的信 提交于 2019-12-01 14:49:13
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 | | 1 | 9 | | 3 | 1 | +--------+------------+ How can I do that in SQL Server? NB: The values in the

Querying for a 'run' of consecutive columns in Postgres

一个人想着一个人 提交于 2019-12-01 13:01:28
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 given an event could return the length of the 'run' of events starting with that event. A run is

Jump SQL gap over specific condition & proper lead() usage

故事扮演 提交于 2019-12-01 12:18:57
(PostgreSQL 8.4) Continuing with my previous example , I wish to further my understanding of gaps-and-islands processing with Window-functions. Consider the following table and data: CREATE TABLE T1 ( id SERIAL PRIMARY KEY, val INT, -- some device status INT -- 0=OFF, 1=ON ); INSERT INTO T1 (val, status) VALUES (10, 0); INSERT INTO T1 (val, status) VALUES (11, 0); INSERT INTO T1 (val, status) VALUES (11, 1); INSERT INTO T1 (val, status) VALUES (10, 1); INSERT INTO T1 (val, status) VALUES (11, 0); INSERT INTO T1 (val, status) VALUES (10, 0); As previously explained, the devices turn ON and OFF

How to group ranged values using SQL Server

天大地大妈咪最大 提交于 2019-12-01 11:59:59
问题 I have a table of values like this 978412, 400 978813, 20 978834, 50 981001, 20 As you can see the second number when added to the first is 1 number before the next in the sequence. The last number is not in the range (doesnt follow a direct sequence, as in the next value). What I need is a CTE (yes, ideally) that will output this 978412, 472 981001, 20 The first row contains the start number of the range then the sum of the nodes within. The next row is the next range which in this example

Running total over date range - fill in the missing dates

丶灬走出姿态 提交于 2019-12-01 10:51:27
I have the following table. DATE | AMT 10/10 | 300 12/10 | 300 01/11 | 200 03/11 | 100 How do I get the monthly total? A result like - DATE | TOT 1010 | 300 1110 | 300 1210 | 600 0111 | 800 0211 | 800 0311 | 900 A sql statement like SELECT SUM(AMT) FROM TABLE1 WHERE DATE BETWEEN '1010' AND '0111' would result in the 800 for 0111 but... NOTE There is not a date restriction. which is my dilemma. How do I populate this column without doing a loop for all dates and have the missing months displayed as well? To cater for missing months, create a template table to join against. Think of it as

Running total over date range - fill in the missing dates

十年热恋 提交于 2019-12-01 07:34:00
问题 I have the following table. DATE | AMT 10/10 | 300 12/10 | 300 01/11 | 200 03/11 | 100 How do I get the monthly total? A result like - DATE | TOT 1010 | 300 1110 | 300 1210 | 600 0111 | 800 0211 | 800 0311 | 900 A sql statement like SELECT SUM(AMT) FROM TABLE1 WHERE DATE BETWEEN '1010' AND '0111' would result in the 800 for 0111 but... NOTE There is not a date restriction. which is my dilemma. How do I populate this column without doing a loop for all dates and have the missing months