我正在使用Google BigQuery,我正在尝试从'table2'中找到'userid',不包括那些存储在'table1'中的2次或更多次。 这是代码:
Sub SendMail()
Dim objOutlook As Object
Dim objMail As Object
Dim ws As Worksheet
Set objOutlook = CreateObject("Outlook.Application")
Set ws = ActiveSheet
For Each cell In ws.Range("A2:A1000")
Set objMail = objOutlook.CreateItem(0)
With objMail
.To = cell.Value
.Cc = cell.Offset(0, 1).Value
.Bcc = cell.Offset(0, 2).Value
.Subject = cell.Offset(0, 3).Value
.Body = cell.Offset(0, 4).Value
.Attachments.Add cell.Offset(0, 5).Value
'.Attachments.Add cell.Offset(0, 6).Value
'.Attachments.Add cell.Offset(0, 7).Value
.Send
End With
Set objMail = Nothing
Next cell
Set ws = Nothing
Set objOutlook = Nothing
End Sub
问题是,这是从'table1'返回存储2次或更多次的'userid',我尝试将#standardSQL
WITH t100 AS (
SELECT count_table.userid
From(
SELECT userid,COUNT(`project.dataset.table1`.userid) as notification_count
FROM `project.dataset.table1`
GROUP BY userid) as count_table
where notification_count >= 2
)
SELECT userid FROM `project.dataset.table2` WHERE userid NOT IN (SELECT userid FROM t100)
添加到WHERE userid IS NOT NULL
,但它没有任何区别。
为了让一切都更清晰,这个:
SELECT userid FROM t100
,不为空,由于某种原因返回的结果仍会显示在上面第一个代码的结果中。
答案 0 :(得分:2)
我尝试将WHERE userid IS NOT NULL添加到SELECT用户ID FROM T100,但它没有区别
这当然没有任何影响,因为当你执行COUNT(userid) as notification_count
时,它总是为userid NULL返回0,因此被HAVING notification_count >= 2
过滤掉了
如果您使用COUNT(1)代替 - 那么您可能会在t100的输出中获得null userid。所以userid is NULL
定义不是问题
正如其他人指出的那样 - 您的查询应该有效 - 所以如果您继续遇到问题 - 您需要在此问题上进行更多挖掘并向我们提供更多详细信息
同时,请尝试以下另一个版本的(看起来不错)查询
#standardSQL
WITH t100 AS (
SELECT userid
FROM `project.dataset.table1`
GROUP BY userid
HAVING COUNT(userid) >= 2
)
SELECT userid
FROM `project.dataset.table2` AS t2
LEFT join t100 ON t100.userid = t2.userid
WHERE t100.userid IS NULL
答案 1 :(得分:1)
不确定为什么这不起作用,但出于一般原则,我从不将(not) in
与select语句结合使用。相反,我会left outer join
子查询并过滤其中的空值:
#standardSQL
with t100 as (
select
count_table.userid
from(
select
userid
,count(`project.dataset.table1`.userid) as notification_count
from `project.dataset.table1`
group by
userid
) as count_table
where notification_count >= 2
)
select
t2.userid as userid
from `project.dataset.table2` t2
left outer join t100
on t100.userid = t2.userid
where t100.userid is null
答案 2 :(得分:0)
由于空处理。我们的issue tracker关于NOT IN
与NOT EXISTS
的帖子类似。 documentation for IN州:
在IN列表中使用NULL的IN只能返回TRUE或NULL,永远不会为FALSE
要实现所需的语义,您应该使用反半连接(NOT EXISTS
)。例如,
#standardSQL
WITH t100 AS (
SELECT
userid,
COUNT(userid) as notification_count
FROM `project.dataset.table1`
GROUP BY userid
HAVING notification_count >= 2
)
SELECT userid
FROM `project.dataset.table2` AS t2
WHERE NOT EXISTS (SELECT 1 FROM t100 WHERE userid = t2.userid);