Hi I have a table of thousands of people that have each bought different cars and contains the purchase date like so:
ID Car Purchase_Date
-- --- -------------
1 Lancia 2000-01-24
1 Ford 2003-04-17
1 Honda 2007-11-10
1 Honda 2013-06-28
2 Ford 1998-03-03
2 Ferrari 2007-04-19
3 Ford 1995-10-31
3 Honda 2008-08-20
3 Delorian 2230-01-01
Where I want to bring back ALL data for an ID if the owner has owned a Ford AND a Honda, including any other cars they have, but they must have had both a Ford and a Honda. i.e. like the following:
ID Car Purchase_Date
-- --- -------------
1 Lancia 2000-01-24
1 Ford 2003-04-17
1 Honda 2007-11-10
1 Honda 2013-06-28
3 Ford 1995-10-31
3 Honda 2008-08-20
3 Delorian 2230-01-01
so basically person all data for ID 2 is removed because they owned a ford, but not a honda. All data is retrieved for person 1 including data for the Lancia because they at least had a Ford and a Honda - The same for person 3.
I just need a starting point as I am having difficulty getting my head around this. Obviously I could select only data relating to specific cars such as:
select * from mytable
where Car in ('Ford','Honda')
but then I lose all the other data for cars which I am interested in.
I suppose I could create a column for each type of car I am interested in using case statements to tell me if they have had a car or not using something like:
select distinct id, Ford, honda from
(select distinct id,
case when (select distinct id from mytable where Car = 'Ford') is null then 0 else 1 end as Ford,
case when (select distinct id from mytable where Car = 'Honda') is null then 0 else 1 end as Honda
from mytable a)
where I could query all id's where each column = 1 and save the list of id's and inner join onto the original table and bring data back for those id's only. However this seems slow to me, especially when inevitably having to scale up the database size and the amount of cars needed for the criteria.
Does anyone have any suggestions how I can go about this?