不确定如何做到这一点,但我试图从MYSQL数据库中获取所有包含多年拖欠的包裹。现在,我可以使用以下查询计算多年来我有多少次违约:
SELECT
parcel, COUNT(*), due
FROM
`deliquent_property`
WHERE year IN ('2013', '2012', '2011', '2010', '2009') AND
CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
GROUP BY parcel
HAVING COUNT(*) > 2;
所以我得到这样的东西
'XXX-XXX-XXX1' '3' '167.00'
'XXX-XXX-XXX2' '4' '190'
其中金额是数据库中该宗地的最后一条记录。问题是我真正想要的是每年显示过期金额的包裹清单,类似这样(跳过那些没有至少3次拖欠的包裹):
'XXX-XXX-XXX1' '2013' '1267.78'
'XXX-XXX-XXX1' '2012' '1000.38'
'XXX-XXX-XXX1' '2011' '167.00'
'XXX-XXX-XXX2' '2013' '1000.00'
'XXX-XXX-XXX2' '2012' '500.00'
'XXX-XXX-XXX2' '2011' '100.00'
'XXX-XXX-XXX2' '2010' '190.00'
我已经到了一半,但我迷失了最后一部分。
答案 0 :(得分:1)
您可以将表格加入列表。这是一种方式:
select dp.*
from delinquent_property dp join
(SELECT parcel, COUNT(*)
FROM delinquent_property
WHERE year IN ('2013', '2012', '2011', '2010', '2009') AND
CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
GROUP BY parcel
HAVING COUNT(*) > 2
) p3
on dp.parcel = p3.parcel
where CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
order by parcel;