Teradata SQL 加入字符串的代码
Teradata SQL code to join against a string
我有一个 table A 具有以下值
+----+----------+-----------------------+
| ID | Date | Name |
+----+----------+-----------------------+
| 1 | 1/4/2019 | Kara,Sara,John |
| 2 | 3/2/2018 | Sara |
| 3 | 4/3/2019 | Lynn,John,Chris,Agnes |
| 4 | 2/1/2020 | Phillip, Anton |
| 5 | 5/1/2020 | Quinn |
| 6 | 7/6/2020 | Idie,John |
+----+----------+-----------------------+
还有一个 table B 具有以下值
+-------+
| Name |
+-------+
| John |
| Sara |
| Chris |
+-------+
我希望输出如下:
+----+----------+-----------------------+--------+-----------------+
| ID | Date | Name | B.Name | Exists in List? |
+----+----------+-----------------------+--------+-----------------+
| 1 | 1/4/2019 | Kara,Sara,John | Sara | Yes |
| 1 | 1/4/2019 | Kara, Sara, John | John | Yes |
| 2 | 3/2/2018 | Sara | Sara | Yes |
| 3 | 4/3/2019 | Lynn,John,Chris,Agnes | John | Yes |
| 3 | 4/3/2019 | Lynn,John,Chris,Agens | Chris | Yes |
| 4 | 2/1/2020 | Phillip, Anton | | No |
| 5 | 5/1/2020 | Quinn | | No |
| 6 | 7/6/2020 | Idie,John | John | Yes |
+----+----------+-----------------------+--------+-----------------+
我尝试使用 CONTAINS,但看起来 teradata sql 不接受它。尝试使用 CSVLD 将文本转换为 column.However,因为字符串可以接受的逗号数量不固定,如果我事先不知道需要从文本中重新创建多少列,则无法使用 CSVLD 函数。
想知道是否有任何替代方法可以根据一串值连接列?感谢您的宝贵意见。
您真的应该修复您的数据模型——在一个字符串中存储多个值是一个非常糟糕的数据设计。 SQL 有一种很好的存储列表的方法——它被称为 table。
假设您受困于别人非常非常糟糕的数据模型,您可以使用 left join
:
select a.*, b.name,
(case when b.name is not null then 'Yes' else 'No' end) as in_list
from a left join
b
on ',' || a.name || ',' like '%,' || b.name || ',%';
我有一个 table A 具有以下值
+----+----------+-----------------------+
| ID | Date | Name |
+----+----------+-----------------------+
| 1 | 1/4/2019 | Kara,Sara,John |
| 2 | 3/2/2018 | Sara |
| 3 | 4/3/2019 | Lynn,John,Chris,Agnes |
| 4 | 2/1/2020 | Phillip, Anton |
| 5 | 5/1/2020 | Quinn |
| 6 | 7/6/2020 | Idie,John |
+----+----------+-----------------------+
还有一个 table B 具有以下值
+-------+
| Name |
+-------+
| John |
| Sara |
| Chris |
+-------+
我希望输出如下:
+----+----------+-----------------------+--------+-----------------+
| ID | Date | Name | B.Name | Exists in List? |
+----+----------+-----------------------+--------+-----------------+
| 1 | 1/4/2019 | Kara,Sara,John | Sara | Yes |
| 1 | 1/4/2019 | Kara, Sara, John | John | Yes |
| 2 | 3/2/2018 | Sara | Sara | Yes |
| 3 | 4/3/2019 | Lynn,John,Chris,Agnes | John | Yes |
| 3 | 4/3/2019 | Lynn,John,Chris,Agens | Chris | Yes |
| 4 | 2/1/2020 | Phillip, Anton | | No |
| 5 | 5/1/2020 | Quinn | | No |
| 6 | 7/6/2020 | Idie,John | John | Yes |
+----+----------+-----------------------+--------+-----------------+
我尝试使用 CONTAINS,但看起来 teradata sql 不接受它。尝试使用 CSVLD 将文本转换为 column.However,因为字符串可以接受的逗号数量不固定,如果我事先不知道需要从文本中重新创建多少列,则无法使用 CSVLD 函数。 想知道是否有任何替代方法可以根据一串值连接列?感谢您的宝贵意见。
您真的应该修复您的数据模型——在一个字符串中存储多个值是一个非常糟糕的数据设计。 SQL 有一种很好的存储列表的方法——它被称为 table。
假设您受困于别人非常非常糟糕的数据模型,您可以使用 left join
:
select a.*, b.name,
(case when b.name is not null then 'Yes' else 'No' end) as in_list
from a left join
b
on ',' || a.name || ',' like '%,' || b.name || ',%';