DB2: select all rows of data when multiple categories types of a column for each primary ID is found

StackOverflow https://stackoverflow.com/questions/22099301

  •  18-10-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

In your first query you've used IN clause. That clause has translated as conditionA OR conditionB, so if you have only one of two brands, the query is satisfacted too.

You can use a double exists subquery, one for Honda, one for Ford related in AND

I suppose ID field is not a primary key for your table (has duplicated)

Try this:

select t1.*
from mytable t1
where exists(
    select 'hasford'
    from mytable t2
    where t2.id = t1.id
   and t2.car = 'Ford'
)
and exists(
    select 'hashonda'
    from mytable t3
    where t3.id = t1.id
   and t3.car = 'Honda'
)

OTHER TIPS

Select * from your_table where id in
(
    Select t1.id from 
           Your_table t1 
    Inner join your_table t2
    On t1.id = t2.id
    Where t1.car = 'ford'
    And t2.car = 'Honda'
 )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top