Вот одно решение (пробовал на SQLite3, но оно аналогично для других СУБД, если вы заменили на соответствующую функцию даты):
create table orders([date],[order],price);
insert into orders values
('2013-01-01 06:05:32', 'tea', 3),
('2013-01-02 07:04:24','coffee',2),
('2013-04-03 13:31:23', 'tea', 3);
create table receipts([date],[order],quantity);
insert into receipts values
('2013-01-01 06:05:42', 'tea', 3),
('2013-01-02 07:04:54','coffee',2),
('2013-04-03 13:31:56', 'tea', 3);
-- My desired output is
--
-- | date | order | quantity | price |
-- |1/1/13 06:05:42| tea | 3 | 3 |
-- |1/2/13 07:04:54| coffee| 2 | 2 |
-- |4/3/13 13:31:56| tea | 3 | 3 |
select r.[date],[order],quantity,price
from orders o join receipts r using([order])
where r.date > o.date
and r.date < datetime(o.date,'+40 seconds')
или даже:
select r.[date],[order],quantity,price
from orders o join receipts r using([order])
where r.date between o.date and datetime(o.date,'+40 seconds')
Я использовал 40 секунд (вы говорите, примерно 30 секунд, но ваш пример выводит разницу в 33 секунды). Отрегулируйте по мере необходимости. Я также предполагаю (основываясь на вашем примере), что заказы всегда идут раньше поступлений.