组合这些 mySQL 查询
我需要合并这些查询,以便我返回月份列表,其中包含新的总计和返回的总计(以跟踪新用户和返回用户)。我目前有以下两个疑问。第一个计数为新设备(检测频率),第二个计数为返回。我想输出结果,因此它是一个包含每个月的行的表,然后是包含“新建”和“返回”数据的两列。
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
我环顾四周,但没有找到任何有关如何使用别名来完成此操作的信息。
I need to combine these queries, so i get back a list of months, with a new total, and returning total (to track new and returning users). I currently have these two queries below. The first counts devices which are new (detection freq), the second counts as returning. I want to output the results so it's a table with rows for each month, then two columns with New, and Returning data.
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
I have had a look around but not found any information on how this can be done with aliases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这又如何呢?
(或加入,取决于您的喜好)。
What about this ?
(or a join, depending on what you prefer).
这只为您提供一行两列,但应该让您了解如何使用别名
This only gives you one row with two columns, but should give you an idea of how you can use aliases