2017-04-04 74 views
1
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個月?mySQL:從前一個間隔獲取3個月間隔

enter image description here

回答

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;