Advertisement
darth-vader

sql

Jun 5th, 2020
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. select * from (select table_1.id, table_2.id, (table_1.quantity + table_2.quantity) as total
  2. from
  3.                (select * from tables where not exists (
  4.     select *
  5.     from "reservations"
  6.     inner join "reservation_tables" on "reservations"."id" = "reservation_tables"."reservation_id"
  7.     where
  8.         "tables"."id" = "reservation_tables"."table_id"
  9.         and tsrange(start_at, end_at, '[]') && tsrange('2020-06-30 22:31:00', '2020-07-01 00:01:00', '[]')
  10.         and "canceled_at" is null and "reservations"."deleted_at" is null
  11. ))
  12.                as table_1,
  13.                (select * from tables where not exists (
  14.     select *
  15.     from "reservations"
  16.     inner join "reservation_tables" on "reservations"."id" = "reservation_tables"."reservation_id"
  17.     where
  18.         "tables"."id" = "reservation_tables"."table_id"
  19.         and tsrange(start_at, end_at, '[]') && tsrange('2020-06-30 22:31:00', '2020-07-01 00:01:00', '[]')
  20.         and "canceled_at" is null and "reservations"."deleted_at" is null
  21. ))
  22.                as table_2
  23. where table_1.id != table_2.id and table_1.allow_link = true and  table_2.allow_link = true
  24. ) as b
  25. where b.total >= 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement