简体   繁体   中英

How do I search for instances of a sub query string in a string field in the main table - teradata?

I have a Main Table (MT) that contains a field called Wine_Variety. This is populated with data that looks like the following...

CUSTOMER_NUMBER|WINE_VARIETY  

1001|SHIRAZ
1002|CHARDONNAY,MERLOT
1003|MERLOT,CHARDONNAY,MALBEC
1004|MALBEC,RIESLING

I have developed a Sub Query (SQ) that returns values for the top five selling wines, extracted from a transaction table. This basically produces a list that looks like the following

VARIETY|QUANTITY

SHIRAZ|10000
CABERNET SAUVIGNON|9500
CABERNET MALBEC|8000
CHARDONNAY|7000
CHAMPAGNE|6000

I want to find out which customers in MT have bought wines that appear in SQ.

Looking at the above lists I would expect the query to return customers 1001 - 1003 inclusive, but not customer 1004 who hasn't purchased wines in the top five.

I thought to following would achieve this but it comes up with 0 rows

sel * from MT 
where MT.Wine_Variety like any 
 (
   sel search_string from
    (
      sel top 5 variety, '''%'||variety||'%''' as search_string ,sum(quantity) as total_bought 
      from some_transaction_table
      where variety is not null and variety <> ''
      group by 1,2
      order by 3 desc
    ) a
 );

Yet it works if I try the following...

sel * from MT where MT.Wine_Variety like any ('%SHIRAZ%','%CHARDONNAY%')

Any thoughts?

I think in is much simpler:

select mt.*
from MT 
where MT.Wine_Variety in (select top 5 stt.variety
                          from some_transaction_table
                          where stt.variety is not null and stt.variety <> ''
                          group by stt.variety
                          order by sum(stt.quantity) desc
                         );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM