mySQL:从前一个间隔获得3个月的间隔

时间:2017-04-04 22:59:05

标签: mysql sql

select pd.state AS StateName, pd.county AS `County Name`,
   SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `0-3 Months`, 
   SUM(`0-3 Months` - INTERVAL 3 MONTH) AS `3-6 Months`


from resnet.property_details pd join
 resnet.property_closings pc
 on pd.property_id = pc.Property_id
group by pd.state, pd.county;

如何获得前3个月间隔值的3个月间隔?那么这将是3-6个月?

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用BETWEEN..AND,例如:

select pd.state AS StateName, pd.county AS `County Name`,
   SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `0-3 Months`, 
   SUM(pc.close_dt BETWEEN CURDATE() - INTERVAL 3 MONTH AND CURDATE() - INTERVAL 6 MONTH) AS `3-6 Months`
from resnet.property_details pd join
 resnet.property_closings pc
 on pd.property_id = pc.Property_id
group by pd.state, pd.county;