S
S
Shultc2014-02-03 02:01:29
SQL
Shultc, 2014-02-03 02:01:29

How to remove subqueries in BETWEEN in SQL?

Yes, it means that my SELECT is quite loaded. I would like to make it easier, at least by removing subqueries. What I'm scratching my head about the most is the subqueries in BETWEEN. Now the part with BETWEEN looks like this:

...
SELECT * FROM Visitors v
WHERE v.last_visit_time BETWEEN
    (SELECT start_time FROM Event WHERE event_id = @first_event) AND
    (SELECT start_time FROM Event WHERE event_id = @second_event) AND
...

That is, in this case, I'm interested in all the visitors who last visited us between two specific events. Is there a way to get rid of these subqueries? Without them, it works an order of magnitude faster, but I still want to do everything at once.
And, as a bonus, how can I remove the subquery from here:
...
AND g.place_id IN (SELECT place_id FROM Event WHERE event_id = @first_event)
...

All submitted requests are clones of the real ones. No corporation has suffered a data breach when asking this question.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2014-02-03
@Shultc

And what prevents you from getting the necessary values ​​in advance start_timein the same place where the variables @first_eventand are set @second_event? With place_idit is not entirely clear how many events can be with one event_id? If only one - then also receive in advance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question