Question

I have a very basic knowledge of SQL but for the purpose of handling a large amount of data, I need to use it. Here's my issue:

I have two tables, as follows:

table1:
id    begin    end
100   1998     2013
101   1996     2009

table2:
id    date     price    qtt
100   1996     10       200
100   1999     12       200
101   1997     13       100
101   2013     14       100

What I need to do is to erase the lines which have date smaller than the begin field or date larger than end for each id. Meaning that for id 101, no records should have date before 1996 nor after 2009.

So my intended result is:

id    date     price    qtt
100   1999     12       200
101   1997     13       100

Efficiency is a concern, as I am doing this for a table with several million records. I have prepared a sqlfiddle with these tables so that it is easier for you.

http://sqlfiddle.com/#!2/66f54f/1

I apologize if this question is too simple but after searching for three hours, I was unable to solve it myself. Thank you in advance!

Was it helpful?

Solution

Here is standard SQL syntax for what you want to do:

delete from table2
    where not exists (select 1
                      from table1
                      where table1.id = table2.id and
                            table2.date between table1."begin" and table1."end"
                     );

Do note that if an id exists in table2 but not table1, then all rows for that id will be deleted.

This should be pretty efficient in most databases if you have an index on table1(id, date).

OTHER TIPS

Try this...

DELETE FROM A
FROM TABLE2 A
inner join table1 B
on A.id=B.id
and (A.[DATE] BETWEEN B.[BEGIN] AND B.[End])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top