简体   繁体   中英

How can I include a subquery into an inner join?

How can I include a subquery into an inner join?

I have the following SQL:

SELECT 
  date_trunc(
    'hour',
    FROM_UNIXTIME(timefrom)
  ) AS HourFrom,
  date_trunc(
    'hour',
    (FROM_UNIXTIME(timeto) + interval '45' minute)
  ) AS HourTo
FROM 
  reservation
ORDER BY
  date_trunc(
    'day',
    FROM_UNIXTIME(timefrom)
  )

That gives me data like this:

TimeFrom                    TimeTo
2015-08-04 11:00:00.000     2015-08-04 14:00:00.000
2015-08-04 18:00:00.000     2015-08-04 20:00:00.000
2015-08-04 21:00:00.000     2015-08-04 23:00:00.000

I want to have a numbers table and do an inner join like the following:

SELECT DATE_ADD(HOUR, i - 1, TimeFrom) AS TimeFrom, 
       DATE_ADD(HOUR, i, TimeFrom) AS TimeTo
FROM (SELECT 1 AS i UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
      UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
      UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
      UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16
      UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20
      UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24
) AS numbers
INNER JOIN mytable ON numbers.i <= DATE_DIFF(HOUR, TimeFrom, TimeTo)
ORDER BY TimeFrom

So I can get something like this:

TimeFrom                    TimeTo
2015-08-04 11:00:00.000     2015-08-04 12:00:00.000
2015-08-04 12:00:00.000     2015-08-04 13:00:00.000
2015-08-04 13:00:00.000     2015-08-04 14:00:00.000
2015-08-04 18:00:00.000     2015-08-04 19:00:00.000
2015-08-04 19:00:00.000     2015-08-04 20:00:00.000
2015-08-04 21:00:00.000     2015-08-04 22:00:00.000
2015-08-04 22:00:00.000     2015-08-04 23:00:00.000

How can I use the result of the first query to do the inner join(replace mytable with that query result)? Is this possible? I have not been able to find a way to plug in my query to perform an inner join on it.

I am not very experienced with SQL syntax and have not been able to find an answer for this despite looking all day.

What you want to do is create a "derived table". That is, a table that derived from something else. Most times it's a view. But it can be done just the same with a typical SELECT statement. The syntax looks like this:

SELECT dt.name 
from (SELECT name from tableWithName) dt

Where "dt" is the name of the table. When you reference it in the primary select, you must use the derived table name as a prefix. "dt.name"

The second query you have here actually HAS this set up correctly already. But the word AS is incorrect syntax. it should just say

...SELECT 24
) numbers

And all of your parts in the begining SELECT should use that table name as a prefix:

SELECT DATE_ADD(HOUR, i - 1, numbers.TimeFrom) AS TimeFrom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM