[英]where clause in sql with multiple conditions based on priorities SQL BigQuery
I have two tables that I am merging together in BigQuery.我有两个要在 BigQuery 中合并的表。 I need to filter the data based on multiple conditions.我需要根据多个条件过滤数据。 I need to return all the fields where url = https://www.mywebpage%
, duration = 15000
and the third condition should grab either action = Midpoint
or action = Complete
.我需要返回其中 url = https://www.mywebpage%
,持续时间 = 15000
所有字段,第三个条件应该获取 action = Midpoint
或 action = Complete
。 Below is the code:下面是代码:
SELECT
d.duration,
c.action,
c.url
FROM
(
`table_action_url` c
INNER JOIN `table_duration` d ON (d.id = c.id)
)
WHERE c.url LIKE "https://www.mywebpage%"
AND d.duration = '15000'
AND c.action like 'Midpoint'
OR (c.action like 'Complete')
This returns different fields for duration and url, but right actions.这将返回不同的持续时间和 url 字段,但是正确的操作。 I also tried the following:我还尝试了以下方法:
SELECT
d.duration,
c.action,
c.url
FROM
(
`table_action_url` c
INNER JOIN `table_duration` d ON (d.id = c.id)
)
WHERE (c.url LIKE "https://www.mywebpage%" AND d.duration = '15000' AND c.action like 'Midpoint')
OR (c.url LIKE "https://www.mywebpage%" AND d.duration = '15000' AND c.action like 'Complete')
This query returns the right url and duration but for action, the values are all Complete.此查询返回正确的 url 和持续时间,但对于 action,所有值都是 Complete。 The desired output is:所需的输出是:
duration action url
15000 Midpoint https://www.mywebpage
15000 Complete https://www.mywebpage
15000 Midpoint https://www.mywebpage
AND
goes before OR
... AND
在OR
之前...
You have:你有:
WHERE (c.url LIKE "https://www.mywebpage%"
AND d.duration = '15000'
AND c.action like 'Midpoint')
OR (c.action like 'Complete')
But you want:但你想要:
WHERE c.url LIKE "https://www.mywebpage%"
AND d.duration = '15000'
AND c.action in ('Midpoint', 'Complete')
You need parentheses.你需要括号。 Not in the FROM
clause.不在FROM
子句中。 In the WHERE
clause:在WHERE
子句中:
FROM `table_action_url` c JOIN
`table_duration` d
ON d.id = c.id
WHERE c.url LIKE 'https://www.mywebpage%' AND
d.duration = '15000' AND
(c.action LIKE 'Midpoint' OR c.action LIKE 'Complete')
In your particular case, you can replace the LIKE
with IN
:在您的特定情况下,您可以用IN
替换LIKE
:
WHERE c.url LIKE 'https://www.mywebpage%' AND
d.duration = '15000' AND
c.action IN ('Midpoint', 'Complete')
But if you want wildcard matching, use regexp_contains()
instead:但是,如果您想要通配符匹配,请改用regexp_contains()
:
WHERE c.url LIKE 'https://www.mywebpage%' AND
d.duration = '15000' AND
REGEXP_CONTAINS(c.action, '^(Midpoint|Complete)$'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.