SQL不能排序的问题。
select distinct aa.CustomType,aa.LoanBlankDate,
(select BrachName from CAPS_BrachIns where ID =CAST(substring(aa.ContractNm ,2,2) as Int)) wd,
aa.CustomName,aa.ContractNm,aa.PeriodInterst,
isnull(c.receivableAmount,0),isnull(aa.PeriodInterst,0)-isnull(c.receivableAmount,0) receivebalance
from
(select b.CustomType,a.LoanBlankDate,a.CustomName,a.ContractNm,a.PeriodInterst,a.PeriodNm,a.IsAdvanceBlack
from InterestList a
left join OPSInf b on
a.ContractNm=b.LoanNm
where convert(varchar(10),LoanBlankDate,120) between '2013-08-26' and '2013-09-19'
) aa
left join
InterestReceive c on c.ContractNm=aa.ContractNm and c.PeriodNm=aa.PeriodNm and c.RepayType='PeriodInterst'
where aa.IsAdvanceBlack!='true' order by aa.CustomType,aa.LoanBlankDate,wd
一直很奇怪,这个代码最后一句排序,不能按照WD字段进行,其他2个字段可以排序
[解决办法]
语法没问题。应该也不会报错。应该是楼主的逻辑有问题。说一下你的SQL的执行顺序吧:
#1.from aa left join InterestReceive
#2.WHERE aa.IsAdvanceBlack != 'true'
#3.select后面所有字段及其计算值
#4.DISTINCT
#5.ORDER BY
[解决办法]
分析到位