1

Item - Id, Name,
PurchaseLog - Id, ItemId, CustomerId, PurchaseDate User - Id, UserName

For given two customer's usernams, find names of common items they purchased within last year.

Is this naive (or even correct)? :

select distinct item.id, item.name
from item i, PurchaseLog log_username1, PurchaseLog log_username2,  user user1, user user2
where lower(user1.username) = lower('UserName1') AND
      lower(user2.username) = lower('UserName2') AND
      log_username1.itemid = log_username2.itemid AND
      log_username2.itemid = i.itemid AND
      log_username1 >-- satisfy date contraint AND
      log_username2 >-- satisfy date contraint
1
  • Looks fine so far. How are you planning to encode the date constraints? Mind you, a better way might be to use inner joins. Apr 28, 2012 at 1:56

2 Answers 2

8

You describe the basic requirement for an intersection query.

select item.id, item.name
from item, PurchaseLog p, user u
where lower(u.username) = lower('Username1')
AND p.user_id = u.user_id
and item.id = p.itemid
and p.purchasedate between SYSDATE and SYSDATE-365
INTERSECT
select item.id, item.name
from item, PurchaseLog p, user u
where lower(u.username) = lower('Username2')
AND p.user_id = u.user_id
and item.id = p.itemid
and p.purchasedate between SYSDATE and SYSDATE-365

This will return a list of item.id and item.name that appear for both users.

2
  • will it be faster than what I was doing or yours is just more elegant?
    – TPR
    Apr 28, 2012 at 2:29
  • @progtick What you were attempting made no sense when what you wanted was the intersection between two result sets. I could not follow the logic of your attempted approach. Databases work with sets very well.
    – tawman
    Apr 28, 2012 at 2:33
0

This solution solution uses semi-joins witch should be optimal in your case because it doesn't need a distinct on the result. The /+inline/ hint tells the optimizer to not use a temporary table on the user_items subquery. To improve performances you should use a column(a virtual column should be optimal for it) with user.userid constrained to be lowercase and use an index on it, so later you don't need to call lower on queries on it.

I also supposed you missed to specify the join condition between PurchaseLog and user tables in your query.

with user_items as (
        select /*+inline*/ lower(username),itemid
        from PurchaseLog
            join user using (userid) /*you were missing this join in your query*/
        where  log_username >-- satisfy date contraint
    )
select item.id, item.name
from item i
where itemid in (
        select itemid
        from user_items
        where username = lower('UserName1')
    ) and itemid in (
        select itemid
        from user_items
        where username = lower('UserName2')
    )
/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.