求一SQL語句,謝謝先拉

hycheng163 2009-03-18 03:41:29
create table #t (kk varchar(10),hh varchar(10),ll int)
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo',null,9
結果:
aa | bb | 2
ff | gg | 5
oo | null | 9
tt | null | 10
比較簡練,快速的語句
...全文
73 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
select kk+' | '+isnull(hh,'null')+' | '+cast(ll as varchar(10))
from ( select kk, hh, ll, row_number() over(partition by kk order by hh desc) rn from t) a
where a.rn = 1
htl258_Tony 2009-03-18
  • 打赏
  • 举报
回复
误解了
htl258_Tony 2009-03-18
  • 打赏
  • 举报
回复
create table #t (kk varchar(10),hh varchar(10),ll int) 
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo',null,9
select * from #t a where ll in(select min(ll) from #t where kk=a.kk)
drop table #T

kk hh ll
---------- ---------- -----------
aa bb 2
ff NULL 4
oo NULL 9
tt NULL 10

(所影响的行数为 4 行)
htl258_Tony 2009-03-18
  • 打赏
  • 举报
回复
select * from #t a where ll in(select max(ll) from #t where kk=a.kk)
水族杰纶 2009-03-18
  • 打赏
  • 举报
回复
create table #t (kk varchar(10),hh varchar(10),ll int) 
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo',null,9
select * from #t t where not exists(select 1 from #t where kk=t.kk and ll<t.ll)
drop table #T
/*kk hh ll
---------- ---------- -----------
aa bb 2
ff NULL 4
tt NULL 10
oo NULL 9

(影響 4 個資料列)*/
dawugui 2009-03-18
  • 打赏
  • 举报
回复
create table #t (kk varchar(10),hh varchar(10),ll int) 
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo',null,9
go

select t.* from #t t where hh = (select max(hh) from #t where kk = t.kk)
union all
select t.* from #t t where hh is null and kk not in (select t.kk from #t t where hh = (select max(hh) from #t where kk = t.kk))
order by kk

drop table #t

/*
kk hh ll
---------- ---------- -----------
aa bb 2
ff gg 5
oo NULL 9
tt NULL 10

(所影响的行数为 4 行)
*/
百年树人 2009-03-18
  • 打赏
  • 举报
回复
select 
kk+' | '+case when hh is null then 'null' else hh end+' | '+ltrim(ll)
from #t

/**

--------------------------------------
aa | bb | 2
aa | null | 3
ff | gg | 5
ff | null | 4
tt | null | 10
oo | null | 9

(所影响的行数为 6 行)
**/
hycheng163 2009-03-18
  • 打赏
  • 举报
回复
--改了一下:
create table #t (kk varchar(10),hh varchar(10),ll int)
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo','jj',9

/*
結果
aa | bb | 2
ff | gg | 5
oo | jj | 9
tt | NULL| 10
*/
百年树人 2009-03-18
  • 打赏
  • 举报
回复
select 
kk+'|'+case when hh is null then 'null' else hh end+'|'+ltrim(ll)
from #t
moonshineidolon 2009-03-18
  • 打赏
  • 举报
回复
没看到规则呢
playwarcraft 2009-03-18
  • 打赏
  • 举报
回复

create table #t (kk varchar(10),hh varchar(10),ll int)
insert into #t select 'aa','bb',2
insert into #t select 'aa',null,3
insert into #t select 'ff','gg',5
insert into #t select 'ff',null,4
insert into #t select 'tt',null,10
insert into #t select 'oo',null,9

go
select a.*
from #t a
inner join
(select kk,max(hh) as hh
from #t
group by kk) b
on a.kk=b.kk and isnull(a.hh,'#$')=isnull(b.hh,'#$')

/*
aa bb 2
ff gg 5
oo NULL 9
tt NULL 10

*/
GO
drop table #t

22,233

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧