Cross joins can be difficult to work with because they relate data in ways that are usually unintuitive. Here's how I would do it instead, with the simple, default, INNER JOIN
:
WITH day1_info AS
(SELECT sub1, sub2
FROM mytable)
SELECT
day2_info.sub1 - day1_info.sub1 AS sub1_difference,
day2_info.sub2 - day1_info.sub2 AS sub2_difference,
FROM
mytable AS day2_info JOIN day1_info
ON day1_info.date = '2014-11-07'
AND day2_info.date = '2014-11-08'
If you'd like to do this for multiple sets of dates, you can do that too. Just change the JOIN
statement slightly. (Note that in this case, you may also want to SELECT
one of the dates as well, so that you know which time period each result applies to.)
WITH day1_info AS
(SELECT sub1, sub2
FROM mytable)
SELECT
day2_info.date,
day2_info.sub1 - day1_info.sub1 AS sub1_difference,
day2_info.sub2 - day1_info.sub2 AS sub2_difference,
FROM
mytable AS day2_info JOIN day1_info
ON (day1_info.date::timestamp + '1 day') = day2_info.date::timestamp