Note that there are some explanatory texts on larger screens.

plurals
  1. POList all SQL records occurring within x hours of another set of records
    primarykey
    data
    text
    <p>Am I even close on this?<br> I'm trying to list all the records that contain "Ape"... as well as all the records that occur x days BEFORE any "Ape" record(s). I guess I need a self-join table?</p> <pre><code>-- Doesn't work: SELECT tblA.MyDate, tblA.MyPet FROM TestTable As tblA WHERE tblA.MyPet='Ape' UNION SELECT tblA.MyDate, tblA.MyPet FROM TestTable AS tblA, TestTable AS tblB WHERE tblA.MyPet='Ape' AND tblA.MyDate&gt;tblB.MyDate-0.5 AND tblA.MyDate&lt;tblB.MyDate ORDER BY tblA.MyDate ASC -- Doesn't work: SELECT * FROM TestTable As tblA INNER JOIN TestTable As tblB ON tblA.MyPet = 'Ape' AND tblA.MyDate&gt;tblB.MyDate-1 AND tblA.MyKey&gt;tblB.MyKey -- Doesn't work: SELECT * FROM TestTable WHERE MyDate IN (SELECT MyDate-1 FROM TestTable WHERE MyPet='Ape') -- Doesn't work SELECT id, uid, date FROM orders current WHERE EXISTS ( SELECT * from orders future WHERE future.date &lt; DateAdd(DAYS, 1, current.date) AND future.date &gt; getdate() AND future.uid = current.uid ) -- Doesn't work SELECT * FROM TestTable AS tblA WHERE EXISTS ( SELECT * FROM TestTable AS tblB WHERE tblB &lt; DateAdd(DAYS, 1, tblA.MyDate) AND tblB.MYDate &gt; GetDate() AND tblA.MyKey = tblB.MyKey ) </code></pre> <p>=========================== Here are the tables:</p> <pre><code>CREATE TABLE TestTable ( MyKey Int IDENTITY(1,1) PRIMARY KEY, MyDate DateTime NOT NULL DEFAULT GetDate(), MyPet VarChar(22) NOT NULL DEFAULT '' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 06:12:10', 'Cat' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 10:11:10', 'Dog' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 14:13:10', 'Fish' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 16:14:10', 'Duck' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 17:15:10', 'Bird' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('01-Dec-2012 20:16:10', 'Kitten') INSERT INTO TestTable(MyDate, MyPet) VALUES('02-Dec-2012 01:17:10', 'Dog' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('02-Dec-2012 12:19:10', 'Fish' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('02-Dec-2012 13:20:10', 'Duck' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('02-Dec-2012 14:21:10', 'Bird' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('02-Dec-2012 16:18:10', 'Cat' ) -- These are within 1 day before any "Ape" record INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 05:26:10', 'Dog' ) -- INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 11:22:10', 'Kitten') -- INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 12:23:10', 'Duck' ) -- INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 13:24:10', 'Bird' ) -- INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 14:25:10', 'Ape' ) -- An "Ape" record INSERT INTO TestTable(MyDate, MyPet) VALUES('03-Dec-2012 16:27:10', 'Cat' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('04-Dec-2012 01:32:10', 'Dog' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('04-Dec-2012 04:28:10', 'Fish' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('04-Dec-2012 07:30:10', 'Bird' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('04-Dec-2012 10:31:10', 'Kitten') INSERT INTO TestTable(MyDate, MyPet) VALUES('04-Dec-2012 16:29:10', 'Duck' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('05-Dec-2012 11:35:10', 'Kitten') INSERT INTO TestTable(MyDate, MyPet) VALUES('05-Dec-2012 12:36:10', 'Duck' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('05-Dec-2012 13:33:10', 'Duck' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('05-Dec-2012 13:37:10', 'Bird' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('05-Dec-2012 14:34:10', 'Dog' ) INSERT INTO TestTable(MyDate, MyPet) VALUES('06-Dec-2012 04:41:10', 'Fish' ) -- These are within 1 day before any "Ape" record INSERT INTO TestTable(MyDate, MyPet) VALUES('06-Dec-2012 05:39:10', 'Dog' ) -- INSERT INTO TestTable(MyDate, MyPet) VALUES('06-Dec-2012 14:38:10', 'Kitten') -- INSERT INTO TestTable(MyDate, MyPet) VALUES('06-Dec-2012 16:40:10', 'Ape' ) -- An "Ape" record INSERT INTO TestTable(MyDate, MyPet) VALUES('06-Dec-2012 16:42:10', 'Duck' ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload