我需要将这些查询组合在一起,因此我会返回一个月份列表,其中包含新的总数和返回的总数(用于跟踪新用户和返回用户)。我目前在下面有这两个问题。第一个计数是新的设备(检测频率),第二个计数为返回。我想输出结果,因此它是一个包含每个月行的表,然后是两列包含New和Returning数据。
SELECT COUNT( DISTINCT (mac) ) AS new,
EXTRACT( MONTH FROM date_time ) AS month
FROM detected_devices
WHERE client_id = 11
AND venue_id = 1
AND detection_frequency = 1
GROUP BY month
UNION ALL
SELECT COUNT( DISTINCT (mac) ) AS returning,
EXTRACT( MONTH FROM date_time ) AS month
FROM detected_devices
WHERE client_id = 11
AND venue_id = 1
AND detection_frequency > 1
GROUP BY month
我已经浏览了一下,但没有找到任何有关如何使用别名来完成此操作的信息。
答案 0 :(得分:0)
这个怎么样?
SELECT t1.month, t1.new, t2.returning FROM (query 1) as t1, (query 2) as t2 where t1.month = t2.month;
(或加入,取决于您的喜好)。
答案 1 :(得分:0)
这只会给你一行有两列,但应该让你知道如何使用别名
SELECT
A.new,
B.returning
FROM
(SELECT COUNT( DISTINCT (mac) ) AS new,
EXTRACT( MONTH FROM date_time ) AS month
FROM detected_devices
WHERE client_id = 11
AND venue_id = 1
AND detection_frequency = 1
GROUP BY month) A,
(SELECT COUNT( DISTINCT (mac) ) AS returning,
EXTRACT( MONTH FROM date_time ) AS month
FROM detected_devices
WHERE client_id = 11
AND venue_id = 1
AND detection_frequency > 1
GROUP BY month) B