gaps-and-islands

Query Records and Group by a block of time

北城余情 提交于 2019-11-29 02:43:47
I have an application that may be run several times a day. Each run results in data that is written to a table to report on events that occurred. The main report table looks something like this: Id SourceId SourceType DateCreated 5048 433 FILE 5/17/2011 9:14:12 AM 5049 346 FILE 5/17/2011 9:14:22 AM 5050 444 FILE 5/17/2011 9:14:51 AM 5051 279 FILE 5/17/2011 9:15:02 AM 5052 433 FILE 5/17/2011 12:34:12 AM 5053 346 FILE 5/17/2011 12:34:22 AM 5054 444 FILE 5/17/2011 12:34:51 AM 5055 279 FILE 5/17/2011 12:35:02 AM I can tell that there were two runs, but I would like a way to be able to query for a

How to find missing rows (dates) in a mysql table?

China☆狼群 提交于 2019-11-28 12:49:51
I have tried several topics like this one: How to find missing data rows using SQL? here, but I couldn't make it work in my situation. I have a table named posts in MySQL which I save user diaries in it every day. Sometimes users forget to write a post for a day and I want to make it possible for them to submit it later. So the db structures like this: date userid 2011-10-01 1 2011-10-02 1 (missing) 2011-10-04 1 2011-10-05 1 (missing) 2011-10-07 1 So I want to show a dropdown list of missing dates in this table of missing rows to user, so he can select the date he wants to submit the post for.

How to check a sequence efficiently for used and unused values in PostgreSQL

大憨熊 提交于 2019-11-28 11:36:41
问题 In PostgreSQL (9.3) I have a table defined as: CREATE TABLE charts ( recid serial NOT NULL, groupid text NOT NULL, chart_number integer NOT NULL, "timestamp" timestamp without time zone NOT NULL DEFAULT now(), modified timestamp without time zone NOT NULL DEFAULT now(), donotsee boolean, CONSTRAINT pk_charts PRIMARY KEY (recid), CONSTRAINT chart_groupid UNIQUE (groupid), CONSTRAINT charts_ichart_key UNIQUE (chart_number) ); CREATE TRIGGER update_modified BEFORE UPDATE ON charts FOR EACH ROW

Getting Unavailable dates for renting a product that has stocks

感情迁移 提交于 2019-11-28 10:38:54
问题 Database queries, normally so simple, yet sometimes so difficult. (brain trainer) So I have products, stocks and rentStockOrders. These products can be rented for a set of days. The stocks also have a date when they are available. If a new product (stock) can be rented depends on the already rented stocks of that product. A stock item cannot be rented before it's available date. A rentStockOrder (linked between order and stocks) contains the bookings, thus rentStartDate and rentEndDate . A

GROUP BY for continuous rows in SQL

假如想象 提交于 2019-11-28 09:30:01
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 State Date Count 12 1 2009-07-16 10:00 1 45 2 2009-07-16 13:00 2 77 1 2009-07-16 15:00 3 Is this

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

℡╲_俬逩灬. 提交于 2019-11-28 08:50:40
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 need to know what the level was between the transactions. I need this in the context of a set of T-SQL

MySQL to fill in missing dates when using GROUP BY DATE(table.timestamp) without joining on temporary table

白昼怎懂夜的黑 提交于 2019-11-28 06:57:52
问题 after reading through a couple similar Qs/As I haven't quite found the solution I'm looking for. The table data I have is GROUP BY DATE(timestamp) and returning a count, example result: [timestamp] => 2010-05-12 20:18:36 [count] => 10 [timestamp] => 2010-05-14 10:10:10 [count] => 8 Without using a temporary table, or calendar table is there a way to fill in those missing dates? so that with the same table data would return: [timestamp] => 2010-05-12 20:18:36 [count] => 10 /** * I would like

oracle sql query to list all the dates of previous month

浪尽此生 提交于 2019-11-28 04:56:01
问题 Guys i have a requirement to list all the dates of the previous month like below 20101201 20101202 20101203 20101204 20101205 .. .. .. .. .. .. .. .. 20101231 kindly let me know if any better way to do than this query. select TO_CHAR(TRUNC(SYSDATE,'MM')-1,'YYYYMMDD')-(level-1) as EACH_DATE from dual A connect by level < (TO_NUMBER(TO_CHAR(TRUNC(SYSDATE,'MM')-1,'DD'))+1) Also please let me know the problem with this query it says "missing right parenthesis" SELECT /*+ PARALLEL (A,8) */ /*+

SQL - Find missing int values in mostly ordered sequential series

好久不见. 提交于 2019-11-27 23:23:09
I manage a message based system in which a sequence of unique integer ids will be entirely represented at the end of the day, though they will not necessarily arrive in order. I am looking for help in finding missing ids in this series using SQL. If my column values are something like the below, how can I find which ids I am missing in this sequence, in this case 6 ? The sequence will begin and end at an arbitrary point each day, so min and max would differ upon each run. Coming from a Perl background I through some regex in there. ids 1 2 3 5 4 7 9 8 10 Help would be much appreciated. Edit:

SQL to find time elapsed from multiple overlapping intervals

ぐ巨炮叔叔 提交于 2019-11-27 21:36:37
Not using MSSQL or DB2 or Oracle. No CTE. No OVERLAP predicate. No INTERVAL data type. The situation: on a vehicle to be repaired work can not start until all parts ordered for the job have been received. Parts may be ordered multiple times prior to the start of repair. We need to extract the time for which the vehicle was on "parts hold" So for a vehicle identified as id = 1 parts were ordered (d1) and received (d2) on 4 different occasions ID d1 d2 1 8/1 8/8 1 8/2 8/6 1 8/12 8/14 1 8/3 8/10 8/1 8/8 d1 d2 |-------------------------------| 8/2 8/6 8/12 8/14 d1 d2 d1 d2 |---------------| |-----