最新建账年(over partition by)的研究

本文介绍了一种复杂的SQL查询方法,用于从数据库中选择特定记录。通过使用ROW_NUMBER()函数结合PARTITION BY和ORDER BY子句,可以有效地筛选出满足条件的数据行。文章详细解释了不同ORDER BY选项的效果,并指出了可能导致查询失败的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,给出正确的sql和截图

select *
  from (select glorgbookcode,
               glorgbookname,
               builtyear,
               row_number() over(partition by glorgbookname order by builtyear desc,glorgbookcode desc) mm
          from (select t.builtyear, b.glorgbookcode, b.glorgbookname
                  from gl_initbuild t, bd_glorgbook b
                 where t.pk_glorgbook = b.pk_glorgbook
                   and t.dr = '0'
                   and b.glorgbookname not like '%核算%'))
 where mm = 1
 order by glorgbookcode, builtyear desc

一个order by builtyear desc也是可行的。

 2,仅仅一个 order by  glorgbookcode desc,不可行。

3,少一个desc也是不行(ps:order by 后面不写东西,默认是升序asc)

4,对builtyear的升序排序asc不可行。

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/sumsen/archive/2012/05/29/2524816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值