简体   繁体   English

不同时间段计数的 SQL 代码

[英]SQL Code for counts over different time periods

表

I need to figure out how to answer this: Find the number of property views per branch within 1 month, 2 months, and 3 months of client registration in one query.我需要弄清楚如何回答这个问题:在一个查询中找出客户注册后 1 个月、2 个月和 3 个月内每个分行的房产查看次数。

I'm struggling with how to put this in one query, would CASE be the best way?我正在努力解决如何将其放入一个查询中,CASE 是最好的方法吗?

Thanks for any input.感谢您提供任何意见。

You could probably just use UNION你可能只使用UNION

select '1 month' as type, count(*) from tablename where month < 1
union
select '2 month' as type, count(*) from tablename where month < 2
union
select '3 month' as type, count(*) from tablename where month < 3

This would yield your counts as 3 different rows.这将使您的计数为 3 个不同的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM