sql - 无法加入两个子查询
问题描述
我有一张桌子,我想:1)检查满足要求的访问者(VisitorID)2)获取这些访问者访问我网站的所有时间。
我一直在尝试将我的第一个查询与我的所有条件一起加入,另一个查询在其中获得其余相关变量。
SELECT ID, h.Info, h.Action
FROM `table` as A,
UNNEST (hits) as h
WHERE (h.Info LIKE '%PPP%'
OR h.Info LIKE '%BBB%'
OR h.Info LIKE '%SSS%'
OR h.Info LIKE '%FFF%') AND
h.Category LIKE '%Lock%' AND
(h.Action LIKE '%Regist%' OR h.Action LIKE '%NotR%') AND
h.Label LIKE '%Success%'
JOIN
SELECT ID, h.Info, h.Action, time, visits, EXTRACT(TIME FROM TIMESTAMP_SECONDS(StartTime)) AS time_visit, PARSE_DATE("%Y%m%d", date) AS date_visit
FROM `table`,
UNNEST (hits) as h
WHERE (h.Info LIKE '%PPP%'
OR h.Info LIKE '%BBB%'
OR h.Info LIKE '%SSS%'
OR h.Info LIKE '%FFF%') AND
date > "20190529") AS B ON A.ID = B.ID
我已经多次更改 JOIN ,包括/排除括号,但我已经没有想法了。
解决方案
我会继续cte版本
with cte as
(
SELECT ID, h.Info, h.Action
FROM `table` as A,
UNNEST (hits) as h
WHERE (h.Info LIKE '%PPP%'
OR h.Info LIKE '%BBB%'
OR h.Info LIKE '%SSS%'
OR h.Info LIKE '%FFF%') AND
h.Category LIKE '%Lock%' AND
(h.Action LIKE '%Regist%' OR h.Action LIKE '%NotR%') AND
h.Label LIKE '%Success%'
),
cte1 as
(
SELECT ID, h.Info, h.Action, time, visits, EXTRACT(TIME FROM TIMESTAMP_SECONDS(StartTime)) AS time_visit, PARSE_DATE("%Y%m%d", date) AS date_visit
FROM `table`,
UNNEST (hits) as h
WHERE (h.Info LIKE '%PPP%'
OR h.Info LIKE '%BBB%'
OR h.Info LIKE '%SSS%'
OR h.Info LIKE '%FFF%') AND
date > "20190529"
) select cte.*,cte1.* from cte join cte1 on cte.id=cte1.id
推荐阅读
- css - React - 如何将 GlobalStyles 用于带有情感的 Google 字体
- entity-framework-core - 列是鉴别器吗?
- r - Tmap:增加情节和图例之间的空间
- javascript - 页面更新,当我尝试使用 ajax+php 发送邮件数据时
- javascript - 提交时文件上传为空
- javascript - 无法弄清楚如何对从 API 返回的 JSON 对象使用查询参数
- flutter - How to solve flutter "Infinity or NaN toInt" on some images
- javascript - 单击同一 div 中的另一个元素后如何选择特定元素
- c# - 在 C# unity 中使用多态性或接口
- reactjs - 我们是否应该为作为 api 响应接收的单个对象制作不同的 reducer 以避免复杂性。或者有一个减速器