【发布时间】:2013-05-17 13:07:06
【问题描述】:
在查询中
cr 是客户, ch?是客户付款, cari_kod 是客户代码, cari_unvan1 是客户名称 cha_tarihi 是付款日期, cha_meblag 是支付金额
查询的目的,获取指定的客户列表以及他们最后的付款日期和金额...
实际上我的经理需要更多详细信息,但查询非常慢,这就是我只使用 3 个子查询的原因。
问题是如何组合它们?
我研究过 Cte 和“with 子句”和“where”中的子查询,但没有运气。
任何人都可以提出建议。
操作系统是win2003,sql server版本是mssql 2005。
问候
select cr.cari_kod,cr.cari_unvan1, cr.cari_temsilci_kodu,
(select top 1
chh1.cha_tarihi
from dbo.CARI_HESAP_HAREKETLERI chh1 where chh1.cha_kod=cr.cari_kod order by chh1.cha_RECno) as sontar,
(select top 1
chh2.cha_meblag
from dbo.CARI_HESAP_HAREKETLERI chh2 where chh2.cha_kod=cr.cari_kod order by chh2.cha_RECno) as sontutar
from dbo.CARI_HESAPLAR cr
where (select top 1
chh3.cha_tarihi
from dbo.CARI_HESAP_HAREKETLERI chh3 where chh3.cha_kod=cr.cari_kod order by chh3.cha_RECno) >'20130314'
and
cr.cari_bolge_kodu='322'
or
cr.cari_bolge_kodu='324'
order by cr.cari_kod
【问题讨论】:
-
亲爱的帕拉多,我怎么能添加代码/代码标签,因为我不能这样做。感谢编辑。
标签: sql sql-server-2005 subquery master-detail sql-subselect