我有一个包含字段Document Type
的表格。我希望那些Customer
的{{1}} Sum(Amount)
Document Type
4
- Sum(Amount)
Document type
6
> 0
然后应显示其Customer
Document Type
(4,6)
的那些记录。
答案 0 :(得分:1)
让我们假设您的表名是TableA,而Customer列名是CustomerID。
我们可以通过下面的SQL逻辑来实现这个要求。
select x.* from TableA x
join
(select * from
(select dt4.customerID, sum_amount_dt4-sum_amount_dt6 as dt4minusdt6
from
(select CustomerID,sum(Amount) as sum_amount_dt4 from TableA a
where a.Docutype=4)dt4
join
(select CustomerID,sum(Amount) as sum_amount_dt6 from TableA a
where a.Docutype=6)dt6
on dt4.customerID=dt6.customerID
)dt46
where dt46.dt4minusdt6>0 ) y
on x.customerID=y.customerID