2

I'm trying to see what other SKUs have been purchased if SKU A was purchased.

What I have so far is something like:

Select *
    From Test 
    Where Transaction ID = Sku 'A'

but of course the where statement doesn't work. I don't have the ability to create new tables or anything but I think it should be possible to just do a query? Any help would be appreciated as I'm pulling my hair out here!

Thanks!

Table Name: Test (Original Image):

+ -------------- + --- + ----------------- +
| Transaction ID | SKU | Desired in Output |
+ -------------- + --- + ----------------- +
| 1              | A   | Y                 |
| 1              | B   | Y                 |
| 2              | A   | Y                 |
| 2              | D   | Y                 |
| 3              | F   | N                 |
| 3              | G   | N                 |
+ -------------- + --- + ----------------- +
2
  • Which DBMS are you using? Postgres? Oracle? DB2? Firebird? Commented Feb 27, 2017 at 21:16
  • Please read meta.stackoverflow.com/questions/285551/… and the accepted answer Commented Feb 27, 2017 at 21:17

3 Answers 3

1

You could use a subquery

Select *
    From Test 
    Where Transaction_ID in (
        select Transaction_ID
            from Test
            where SKU = 'A'
    )
Sign up to request clarification or add additional context in comments.

Comments

0

You can use exists() in your where like so:

select *
from t
where exists (
  select 1
  from t as i
  where i.TransactionId = t.TransactionId
    and Sku = 'A'
);

Comments

0

Using an inner join to select Transaction id's that contain A:

create table #test
(
Transaction_id int,
Sku varchar(1)
)

insert into #test (Transaction_id, Sku) values (1, 'A')
insert into #test (Transaction_id, Sku) values (1, 'B')
insert into #test (Transaction_id, Sku) values (2, 'A')
insert into #test (Transaction_id, Sku) values (2, 'D')
insert into #test (Transaction_id, Sku) values (3, 'F')
insert into #test (Transaction_id, Sku) values (3, 'G')

select t.Transaction_id, t.Sku
from #test t
join 
(
    select Transaction_id
    from #test
    where Sku = 'A'

) x on x.Transaction_id = t.Transaction_id

drop table #test

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.