22,233
社区成员
发帖
与我相关
我的任务
分享
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 行)
select * from #t a where ll in(select max(ll) from #t where kk=a.kk)
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 個資料列)*/
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 行)
*/
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 行)
**/
select
kk+'|'+case when hh is null then 'null' else hh end+'|'+ltrim(ll)
from #t
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