用于查找购买为空的唯一访问的行计数的大查询

时间:2016-06-28 09:20:40

标签: sql google-bigquery

我有一张桌子

visit_num   visid_high  visid_low   visit_page_num  purchase
  1            123        456              1          null
  1            123        456              2           12
  2            123        556              1          null
  2            123        556              2          null
  2            123        556              3          null
  2            123        556              4          null

并希望得到唯一visit_num + visid_high + visid_low的行数,其中purchase为null。这里,visit_page_num是访问过的页面用户的id,访问是通过visid_high,visid_low和visit_num的串联定义的。我已经编写了查询以获取所购买的行数为空,下面是大查询语句

SELECT
  COUNT(UNIQUE(CONCAT(STRING(visid_high), STRING(visid_low), STRING(visit_num)))) AS Visits,
FROM
  table_name
WHERE
  purchased is null

并发现行数为2,这是错误的,它应该是1.此查询也为下一行提供行数。

visit_num   visid_high  visid_low   visit_page_num  purchase
  1            123        456              1          null

然而,此访问带有visit_page_num id 2的购买价值

visit_num   visid_high  visid_low   visit_page_num  purchase
  1            123        456              2          null

任何人都可以帮助我编写此查询以获取唯一访问的行数,其中购买为空。 提前致谢。 :)

1 个答案:

答案 0 :(得分:0)

SELECT 
  COUNT(Visit) AS Visits
FROM (
  SELECT
    CONCAT(STRING(visid_high), STRING(visid_low), STRING(visit_num)) AS Visit,
    COUNT(purchase) AS purchases
  FROM
    table_name
  GROUP BY 1
  HAVING purchases = 0
)