在 SQL/Vertica 中,我如何 return 喜欢单独的、不相关的表中的列?
In SQL/Vertica how do I return like columns in separate, unrelated tables?
我有两个不相关的表。在我认为我可以加入的两个表中没有匹配的列的意义上不相关。
Table_1 包含我们网站收到的所有搜索信息。
Table_2 包含产品列表。
我想根据在 Table_2 中找到的产品过滤 Table_1 搜索量。
下面是一些示例数据:
Table_1
+-----------------+---------------+
| Keyword | Search_Volume |
+-----------------+---------------+
| Benedryl | 1 |
| Rimadyl | 25 |
| RimadylTablets | 3 |
| Dog Treats | 32 |
| Benedryl Liquid | 50 |
| Liquid Benedryl | 100 |
| Cat Food | 100 |
+-----------------+---------------+
Table_2
+--------------+
| Product_Name |
+--------------+
| Benedryl |
| Rimadyl |
+--------------+
Desired_Output
+----------+---------------+
| Keyword | Search Volume |
+----------+---------------+
| Benedryl | 151 |
| Rimadyl | 28 |
+----------+---------------+
您可以在连接两个表时使用 like
条件,然后对体积求和:
SELECT table2.product_name, SUM(search_volume)
FROM table2
JOIN table1 ON table1.keyword LIKE '%' || table2.product_name || '%'
GROUP BY table2.product_name
我有两个不相关的表。在我认为我可以加入的两个表中没有匹配的列的意义上不相关。
Table_1 包含我们网站收到的所有搜索信息。 Table_2 包含产品列表。
我想根据在 Table_2 中找到的产品过滤 Table_1 搜索量。
下面是一些示例数据:
Table_1
+-----------------+---------------+
| Keyword | Search_Volume |
+-----------------+---------------+
| Benedryl | 1 |
| Rimadyl | 25 |
| RimadylTablets | 3 |
| Dog Treats | 32 |
| Benedryl Liquid | 50 |
| Liquid Benedryl | 100 |
| Cat Food | 100 |
+-----------------+---------------+
Table_2
+--------------+
| Product_Name |
+--------------+
| Benedryl |
| Rimadyl |
+--------------+
Desired_Output
+----------+---------------+
| Keyword | Search Volume |
+----------+---------------+
| Benedryl | 151 |
| Rimadyl | 28 |
+----------+---------------+
您可以在连接两个表时使用 like
条件,然后对体积求和:
SELECT table2.product_name, SUM(search_volume)
FROM table2
JOIN table1 ON table1.keyword LIKE '%' || table2.product_name || '%'
GROUP BY table2.product_name