Question:
I have two simple tables:Table #1
apples_consumption
report_date | apples_consumed |
---|---|
2022-01-01 | 5 |
2022-02-01 | 7 |
2022-03-01 | 2 |
Table #2
hotel_visitors
visitor_id | check_in_date | check_out_date |
---|---|---|
1 | 2021-12-01 | 2022-02-01 |
2 | 2022-01-01 | NULL |
3 | 2022-02-01 | NULL |
4 | 2022-03-01 | NULL |
My purpose is to get a table which shows the ratio between number of visitors in the hotel to number of apples consumed by that time.
For the example above the desired query output should look like this:
report_date | visitors_count | apples_consumed |
---|---|---|
2022-01-01 | 2 –>(visitors #1, #2) | 5 |
2022-02-01 | 3 –>(visitors #1, #2, #3) | 7 |
2022-03-01 | 3 –>(visitors #2, #3, #4) | 2 |
If I were to write a solution to this task using code I would go over each
report_date
from the apples_consumption
table and count how many visitors have a lower/equal check_in_date
than that report_date
and also have a check_out_date
= NULL or check_out_date
greater/equal than that report_date
I came up with this query:
apples_consuptions
table has)I am looking for a more efficient way to achieve this result and your help will be highly appreciated!
Answer:
It is very rarely a good idea to put a subselect in your select list.Join your tables and then use an aggregate count:
If you have better answer, please add a comment about this, thank you!