怎么查询表中某一字段最大值所在行的数据

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/alafqq/article/details/83803655

 

SQL> select * from aaa;

        ID     SEQ_ID NAME
---------- ---------- --------------------
         1          1 A
         1          2 A
         1          3 A
         2          1 B
         2          2 B
         3          1 C
         3          2 C
         3          3 C
         3          4 C

9 rows selected

SQL> 
SQL>  SELECT *
  2    FROM AAA A
  3   WHERE A.SEQ_ID = (SELECT MAX(B.SEQ_ID) FROM AAA B WHERE B.ID = A.ID)
  4  /

        ID     SEQ_ID NAME
---------- ---------- --------------------
         1          3 A
         2          2 B
         3          4 C

www.cnoug.org 论坛 yesl

TOP N中的TOP1,(依需要选用rank,dense_rank,row_number)
SELECT ID, SEQ_ID, NAME
  FROM (SELECT ID,
               SEQ_ID,
               NAME,
               RANK() OVER(PARTITION BY ID ORDER BY SEQ_ID DESC NULLS LAST) DRN
          FROM TA)
WHERE DRN = 1

SQL> SELECT ID, SEQ_ID, NAME
  2    FROM (SELECT ID,
  3                 SEQ_ID,
  4                 NAME,
  5                 RANK() OVER(PARTITION BY ID ORDER BY SEQ_ID DESC NULLS LAST) DRN
  6            FROM AAA)
  7   WHERE DRN =1
  8  /

        ID     SEQ_ID NAME
---------- ---------- --------------------
         1          3 A
         2          2 B
         3          4 C

 

 

具体的就不说,下面谈谈这个 Nulls last ,

 

select v.id,

       v.projectyear,

       p.oacode,

       p.oapublishdate,

       dense_rank() OVER(PARTITION BY V.ID, V.projectyear ORDER BY TO_DATE(P.APPLYDATE, 'YYYY-MM-DD HH24:MI:SS') DESC NULLS LAST) DRN

  from (select F_GETPROJECTDESIGBINDID(ID) bindid, id, projectyear

          from V_GETSUPER) v,

       PM_OADATAPROTOCOL_PM p

 where v.bindid = p.PMBIZCODE

   AND P.PMBIZTYPE = 'DESIGN_DESIGNAUDITING'

   AND P.FILETYPE = 'OAFileType0001'

--------------------------------------------------------------------

以上这段代码始终把 null放在最前面。诶;

 

最后解决方案,在外面再套一层,在里面转date后,再外面order by;

----------------------------------------------------------------------

 

SELECT ID,projectyear,oacode,oapublishdate,rank() OVER(PARTITION BY ID, projectyear 

ORDER BY  APPLYDATE DESC NULLS LAST

) DRN,APPLYDATE,APPID FROM (

select v.id,

       v.projectyear,

       p.oacode,

       p.oapublishdate,

       TO_DATE(oapublishdate, 'YYYY-MM-DD HH24:MI:SS') APPLYDATE,

       P.APPID

       from (select F_GETPROJECTDESIGBINDID(ID) bindid, id, projectyear

          from V_GETSUPER) v,

       PM_OADATAPROTOCOL_PM p

 where v.bindid = p.PMBIZCODE

   AND P.PMBIZTYPE = 'DESIGN_DESIGNAUDITING'

   AND P.FILETYPE = 'OAFileType0001' ) 

------------------------------

为什么不考虑第一种方案呢,那真是浮云啊。。。。

因为他用了2次表。。。我的结果集都是经过千辛万苦算出来的,我还怎么会舍得让他使用2次啊。。。

---------------下午了,好多地方要用了,看来治标不治本啊;;;;

没办法了,只能出杀手锏了

CREATE OR REPLACE VIEW V_GETLAST_PM AS

SELECT OACODE, OAPUBLISHDATE, PMBIZCODE, PMBIZTYPE, FILETYPE

  FROM (SELECT OACODE,

               OAPUBLISHDATE,

               PMBIZCODE,

               PMBIZTYPE,

               FILETYPE,

               RANK() OVER(PARTITION BY PMBIZCODE ORDER BY APPLYDATE DESC NULLS LAST) DRN

          FROM (SELECT PMBIZCODE,

                       P.OACODE,

                       P.OAPUBLISHDATE,

                       TO_DATE(OAPUBLISHDATE, 'YYYY-MM-DD HH24:MI:SS') APPLYDATE,

                       P.PMBIZTYPE,

                       P.FILETYPE

                  FROM PM_OADATAPROTOCOL_PM P))

 WHERE DRN = 1

此视图闪亮登场,为什么还不用第一种方法呢;原来是连接啊。。。

如果这样用连接,nul 是不会等于null的!!! 因此我的空数据会离我而去,因此我还是选择
早上的方式!!!新建了个视图而已啦;

 

 

展开阅读全文

某一字段分组取最大(小)值所在行数据(为MVP准备的文章)

05-12

[code=SQL]--按某一字段分组取最大(小)值所在行的数据rn(爱新觉罗.毓华 2007-10-23于浙江杭州)rn/*rn数据如下:rnname val memorna 2 a2(a的第二个值)rna 1 a1--a的第一个值rna 3 a3:a的第三个值rnb 1 b1--b的第一个值rnb 3 b3:b的第三个值rnb 2 b2b2b2b2rnb 4 b4b4rnb 5 b5b5b5b5b5rn*/rn--创建表并插入数据:rncreate table tb(name varchar(10),val int,memo varchar(20))rninsert into tb values('a', 2, 'a2(a的第二个值)')rninsert into tb values('a', 1, 'a1--a的第一个值')rninsert into tb values('a', 3, 'a3:a的第三个值')rninsert into tb values('b', 1, 'b1--b的第一个值')rninsert into tb values('b', 3, 'b3:b的第三个值')rninsert into tb values('b', 2, 'b2b2b2b2')rninsert into tb values('b', 4, 'b4b4')rninsert into tb values('b', 5, 'b5b5b5b5b5')rngornrn--一、按name分组取val最大的值所在行的数据。rn--方法1:rnselect a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.namern--方法2:rnselect a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)rn--方法3:rnselect a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.namern--方法4:rnselect a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.namern--方法5rnselect a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 3 a3:a的第三个值rnb 5 b5b5b5b5b5rn*/rnrn--二、按name分组取val最小的值所在行的数据。rn--方法1:rnselect a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.namern--方法2:rnselect a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)rn--方法3:rnselect a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.namern--方法4:rnselect a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.namern--方法5rnselect a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 1 a1--a的第一个值rnb 1 b1--b的第一个值rn*/rnrn--三、按name分组取第一次出现的行所在的数据。rnselect a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 2 a2(a的第二个值)rnb 1 b1--b的第一个值rn*/rnrn--四、按name分组随机取一条数据。rnselect a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 1 a1--a的第一个值rnb 5 b5b5b5b5b5rn*/rnrn--五、按name分组取最小的两个(N个)valrnselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.valrnselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.valrnselect a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 1 a1--a的第一个值rna 2 a2(a的第二个值)rnb 1 b1--b的第一个值rnb 2 b2b2b2b2rn*/rnrn--六、按name分组取最大的两个(N个)valrnselect a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.valrnselect a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.valrnselect a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.namern/*rnname val memo rn---------- ----------- -------------------- rna 2 a2(a的第二个值)rna 3 a3:a的第三个值rnb 4 b4b4rnb 5 b5b5b5b5b5rn*/rn--七,如果整行数据有重复,所有的列都相同。rn/*rn数据如下:rnname val memorna 2 a2(a的第二个值)rna 1 a1--a的第一个值rna 1 a1--a的第一个值rna 3 a3:a的第三个值rna 3 a3:a的第三个值rnb 1 b1--b的第一个值rnb 3 b3:b的第三个值rnb 2 b2b2b2b2rnb 4 b4b4rnb 5 b5b5b5b5b5rn*/rn--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。rn--创建表并插入数据:rncreate table tb(name varchar(10),val int,memo varchar(20))rninsert into tb values('a', 2, 'a2(a的第二个值)')rninsert into tb values('a', 1, 'a1--a的第一个值')rninsert into tb values('a', 1, 'a1--a的第一个值')rninsert into tb values('a', 3, 'a3:a的第三个值')rninsert into tb values('a', 3, 'a3:a的第三个值')rninsert into tb values('b', 1, 'b1--b的第一个值')rninsert into tb values('b', 3, 'b3:b的第三个值')rninsert into tb values('b', 2, 'b2b2b2b2')rninsert into tb values('b', 4, 'b4b4')rninsert into tb values('b', 5, 'b5b5b5b5b5')rngornrnselect * , px = identity(int,1,1) into tmp from tbrnrnselect m.name,m.val,m.memo fromrn(rn select t.* from tmp t where val = (select min(val) from tmp where name = t.name)rn) m where px = (select min(px) fromrn(rn select t.* from tmp t where val = (select min(val) from tmp where name = t.name)rn) n where n.name = m.name)rnrndrop table tb,tmprnrn/*rnname val memorn---------- ----------- --------------------rna 1 a1--a的第一个值rnb 1 b1--b的第一个值rnrn(2 行受影响)rn*/rn--在sql server 2005中可以使用row_number函数,不需要使用临时表。rn--创建表并插入数据:rncreate table tb(name varchar(10),val int,memo varchar(20))rninsert into tb values('a', 2, 'a2(a的第二个值)')rninsert into tb values('a', 1, 'a1--a的第一个值')rninsert into tb values('a', 1, 'a1--a的第一个值')rninsert into tb values('a', 3, 'a3:a的第三个值')rninsert into tb values('a', 3, 'a3:a的第三个值')rninsert into tb values('b', 1, 'b1--b的第一个值')rninsert into tb values('b', 3, 'b3:b的第三个值')rninsert into tb values('b', 2, 'b2b2b2b2')rninsert into tb values('b', 4, 'b4b4')rninsert into tb values('b', 5, 'b5b5b5b5b5')rngornrnselect m.name,m.val,m.memo fromrn(rn select * , px = row_number() over(order by name , val) from tbrn) m where px = (select min(px) fromrn(rn select * , px = row_number() over(order by name , val) from tbrn) n where n.name = m.name)rnrndrop table tbrnrn/*rnname val memorn---------- ----------- --------------------rna 1 a1--a的第一个值rnb 1 b1--b的第一个值rnrn(2 行受影响)rn*/[/code] 论坛

没有更多推荐了,返回首页