Select (SELECT TOP(1) CariBakiye FROM
CariHesapHareketleri
where CId= cr.CId ORDER BY Trh DESC
) as kk, CariHesapHareketleri.CId from
Cari cr
看到你是选择Top(1)
cariBakiye 但是 CId 呢?看看这种方式尝试只执行这么多的查询(在删除连接和别名之后),你会得到重复的 CariBakiye 和 All Cid 值
所以你的查询在这里是错误的。你能详细说明你的要求吗?
select TOP(1) (SELECT TOP(1) CariBakiye FROM
CariHesapHareketleri
where CId= cr.CId ORDER BY Trh DESC
) as kk, CariHesapHareketleri.CId from
Cari cr
join CariHesapHareketleri on cr.CId=CariHesapHareketleri.CId
where kk > 0
select balanceaccount,(SELECT TOP(1) balanceaccount
FROM balanceaccountmovement
WHERE balanceaccount.CId = cr.CId
ORDER BY Date DESC) as kk,
balanceaccountmovement.CId
from Customer cr
join balanceaccountmovement
on cr.CId = balanceaccountmovement.CId
where kk > 0
试试这个,你会发现你没有得到重复的数据,但是sql
这些数据对你来说是怎样的?所以TOP
在你的主查询中也使用子句。
- 希望我的方向是正确的。如果不是,请告诉我