如何解决这个问题或在没有子查询的情况下找到第二个 [英] how to solve this or finding second maximum without subquery

查看:51
本文介绍了如何解决这个问题或在没有子查询的情况下找到第二个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 创建  #ime(num  varchar  20 ),name  varchar  20 ),sal  int ); 
插入 进入 #ime ' 1'' < span class =code-string> kannan', 10000 );
插入 进入 #ime ' 2'' < span class =code-string> dinesh', 2000 );
插入 进入 #ime ' 3'' < span class =code-string> dinesh1', 3000 );





对于上面的表使用以下查询我得到的列名称kk未找到

选择名称,dense_Rank()结束(按sal排序)为kk来自#ime其中kk = 2

任何伙伴帮我识别该表中的第二个薪水而没有子查询

解决方案

据我所知, 不能没有子查询(派生表),因为不可能使用排名函数的结果作为条件。

实现这一目标的唯一方法是使用子查询(派生表)!



子查询和派生表的官方含义: SQL中子查询和派生表之间的区别 [ ^ ]


< blockquote>

 选择 * 来自选择 num,name,sal,dense_rank() over  order   by  sal  desc )排名来自 #ime)结果
其中 result.rank = 2


按以下方式创建子查询



 select * from(select name,dense_Rank()over(order by sal)as kk from #ime)as a b $ b where a。 KK = 2 


create table #ime(num varchar(20),name varchar(20),sal int);
insert into #ime values('1','kannan',10000);
insert into #ime values('2','dinesh',2000);
insert into #ime values('3','dinesh1',3000);



for the above table using that following query i got that column name kk was not found
select name, dense_Rank() over(order by sal)as kk from #ime where kk=2
any buddy help me to identify the second salary from that table without sub-query

解决方案

As far as i know, it can't be done without subquery (derived table), because it's impossible to use result of ranking functions as a condition.
The only way to achieve that is to use subquery (derived table)!

Official meaning of subquery and derived table: The difference between subqueries and derived tables in SQL[^]


select * from (select num,name,sal,dense_rank()over(order by sal desc) rank from #ime) result
where result.rank=2


Make a subquery as following way

select * from ( select name, dense_Rank() over(order by sal)as kk from #ime) as a
 where a.kk=2


这篇关于如何解决这个问题或在没有子查询的情况下找到第二个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆