简体   繁体   English

如何在主表的字符串字段-Teradata中搜索子查询字符串的实例?

[英]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.

相关问题 Teradata中的子字符串功能 - Sub String function in Teradata 如何编写一个字符串搜索查询,该查询使用我在该字段上已建立的非聚集索引? - How do I write a string search query that uses the non-clustered indexing I have in place on the field? Teradata 字符串/文本搜索 - Teradata string/text search 如何为该表构建子查询? - How do I structure a sub query for this table? 如何编写sql查询来搜索列中的子字符串并且子字符串是一个变量 - how to write a sql query to search a sub string in column and the sub string is a variable 如何使用另一个表中的匹配值替换/更新列中每个字符串的所有实例? - How do I replace/update all instances of every string in a column with matching values from another table? 用于在表中搜索字符串的SQL查询? - SQL query to search for string in a table? 如何进行SQL查询以选择包含特定子字符串的所有项目? - How do I make a SQL query to select all items that contain a certain sub-string? 如何处理以字符串形式存储在Teradata的int字段中的日期? - How to deal with a date stored as a string in an int field in Teradata? 如何在Teradata 14.0强制转换时将字符串转换为整数并留空? - How do I cast a string to integer and have blank in case of error in the cast with Teradata 14.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM