关于order by 排序的问题

fstao fstao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
比如下面的语句:

create table #tmp001(rowid int identity(1,1),id int,listdate datetime,amount decimal(18,4),getamount decimal(18,4))
insert into #tmp001(id,listdate,amount)
select id,listdate,amount
from t_wh_billofdocument as p
left join #billofdocument_receivables as p1 on p.id=p1.wh_billofdocument_id
WHERE convert(datetime,convert(varchar(10),p.listdate))<@lastdate1
and p.bs_customer_id=@bs_customer_id
union all
select id,listdate,amount
from t_wh_gatheringpayment
where type=1
and convert(datetime,convert(varchar(10),listdate))<@lastdate1
and bs_customer_id=@bs_customer_id

select id,listdate,amount
into #tmp002
from #tmp001 order by listdate

select * from #tmp002

select id,listdate,amount
from #tmp001 order by listdate



第一个排序方法,如果是先按日期排序后插入临时表:
select id,listdate,amount
into #tmp002
from #tmp001 order by listdate

select * from #tmp002

第二个排序方法是直接排序:
select id,listdate,amount
from #tmp001 order by listdate

我发现,第一种的做法显示出来的数据的日期排序是不对的。而第二种排序方法是对的。
第一个排序的数据显示是:


第二个排序的数据显示是:


很显然,第二个排序的数据是对的,第一个排序数据不对(因为2015-10-16 16:31:53.000和2015-10-20 10:36:24.000这两数据跑到后面去了。),请问第一个先用排序插入临时表,为什么不会排序的?如何来改第一个排序的sql语句?
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
fstao
MS-SQL Server 34,876 成员 254,635 内容