Skip to content
Advertisement

Using SQL result as a filter for another query

Here’s my code and there’s a thousand transaction_no result. Which is I have to use as a filter for another code with the same table.

select Item_Code, Transaction_No, Sales, Quantity
from `transaction_table`
where item_code = 'HTHP-P'

Advertisement

Answer

You could use in, if you want to filter on the transactions:

select . . .
from `transaction_table` tt
where tt.transacton_no in (select tt2.Transaction_No
                           from `transaction_table` tt2
                           where tt2.item_code = 'HTHP-P'
                          );

If you want all rows for transactions that have the specified item, you can also use qualify:

select tt.*
from `transaction_table` tt
where 1=1
qualify countif(tt2.item_code = 'HTHP-P') over (partition by Transaction_No) > 0;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement