我有两张桌子,一张有客户,一张有付款
需要具有特定标准的客户的最新付款。为该 specific_criteria 创建了一个查询)
select top 1 *
from payments
where id in (select id from specific_criteria);
显然,这只返回一行。
我实际上不能在这个数据库中编写 VB 代码来做到这一点。
寻找一些 SQL 代码,以获得每个客户的最新付款。
SELECT c.*,
p.*
FROM Customers c
INNER JOIN
Payments p ON p.Customer_ID=c.ID
WHERE p.ID IN (SELECT p2.ID FROM
(SELECT p1.ID, MAX(p1.payment_date)
FROM Payments p1
GROUP BY p1.ID) AS p2)
像这样的东西会起作用吗?
select c.*, (
select top 1 p.*
from payments p
where p.customer_id = c.id
order by p.payment_date desc )
from customers c
where customer_critera