[英]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. 我有一个主表(MT),其中包含一个名为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. 我已经开发了一个子查询(SQ),它返回从交易表中提取的前五种畅销葡萄酒的价值。 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. 我想找出MT中哪些客户购买了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. 查看上面的列表,我希望查询返回的客户是1001-1003(含),而不是未购买前五名葡萄酒的客户1004。
I thought to following would achieve this but it comes up with 0 rows 我以为可以做到这一点,但它带有0行
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: 我认为
in
要简单得多:
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
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.