Skip to main content
added 104 characters in body
Source Link
user2864740
  • 62.5k
  • 15
  • 159
  • 234

This is a good candidate for a self-join.

SELECT o.* FROM mytable t -- FromFor every row from This table, CROSS JOIN mytable o -- selectand for every row in the Other (see note #2) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

Notes:

  1. Because > is used in the value comparison, the t.id <> o.id clause can be omitted. In any case, the query planner should figure this stuff out just fine.
  2. The CROSS JOIN starts off a Cartesian product but the WHERE filters bring it back down quickly. In particular the t.id = {id} clause (assuming that id has a unique constraint) brings down the multiplicity to at most one output row for each input row in o.

This is a good candidate for a self-join.

SELECT o.* FROM mytable t -- From This table, CROSS JOIN mytable o -- select every row in the Other (see note #2) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

Notes:

  1. Because > is used in the value comparison, the t.id <> o.id clause can be omitted.
  2. The CROSS JOIN starts off a Cartesian product but the WHERE filters bring it back down quickly. In particular the t.id = {id} clause (assuming that id has a unique constraint) brings down the multiplicity to at most one output row for each input row in o.

This is a good candidate for a self-join.

SELECT o.* FROM mytable t -- For every row from This table, CROSS JOIN mytable o -- and for every row in the Other (see note #2) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

Notes:

  1. Because > is used in the value comparison, the t.id <> o.id clause can be omitted. In any case, the query planner should figure this stuff out just fine.
  2. The CROSS JOIN starts off a Cartesian product but the WHERE filters bring it back down quickly. In particular the t.id = {id} clause (assuming that id has a unique constraint) brings down the multiplicity to at most one output row for each input row in o.
added 104 characters in body
Source Link
user2864740
  • 62.5k
  • 15
  • 159
  • 234

This is a good candidate for a self-join:self-join.

SELECT o.* FROM mytable t -- From This table, CROSS JOIN mytable o -- select every row in the Other (butsee reallynote this#2) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

(Because > is used in the value comparison, we could omit the t.id <> o.id clause.) Notes:

  1. Because > is used in the value comparison, the t.id <> o.id clause can be omitted.
  2. The CROSS JOIN starts off a Cartesian product but the WHERE filters bring it back down quickly. In particular the t.id = {id} clause (assuming that id has a unique constraint) brings down the multiplicity to at most one output row for each input row in o.

This is a good candidate for a self-join:

SELECT o.* FROM mytable t -- From This table, CROSS JOIN mytable o -- select every row in the Other (but really this) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

(Because > is used in the value comparison, we could omit the t.id <> o.id clause.)

This is a good candidate for a self-join.

SELECT o.* FROM mytable t -- From This table, CROSS JOIN mytable o -- select every row in the Other (see note #2) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

Notes:

  1. Because > is used in the value comparison, the t.id <> o.id clause can be omitted.
  2. The CROSS JOIN starts off a Cartesian product but the WHERE filters bring it back down quickly. In particular the t.id = {id} clause (assuming that id has a unique constraint) brings down the multiplicity to at most one output row for each input row in o.
Source Link
user2864740
  • 62.5k
  • 15
  • 159
  • 234

This is a good candidate for a self-join:

SELECT o.* FROM mytable t -- From This table, CROSS JOIN mytable o -- select every row in the Other (but really this) table WHERE t.id <> o.id -- such that it is a different row AND o.date > t.date -- with a date later AND t.id = {id} -- than a specific row. 

(Because > is used in the value comparison, we could omit the t.id <> o.id clause.)