|
--There is indeed a problem
--Build a table
create table tab
(
id varchar(2),
num int
)
insert into tab select '01', 1
insert into tab select '02', 3
insert into tab select '03', 2
insert into tab select '04', 5
insert into tab select '05', 18
insert into tab select '06', 99
insert into tab select '07', 19
insert into tab select '08', 29
insert into tab select '09', 39
insert into tab select '10', 49
--Modified statement
select
c.cnum +'small/'+ c.cnum +'big' as name,
case when a.id <(a.cou + 1)*1.0/2 then cast(a.num as varchar) +'/'+cast(b.num as varchar)
when a.id = (a.cou + 1)*1.0/2 then cast(a.num as varchar)
else'' end as value
from
(
select distinct id = (select count(distinct num) from tab where num <= a.num),
cou = (select count(distinct num) from tab),
num
from tab a
)a inner join
(
select distinct id = (select count(distinct num) from tab where num <= a.num),
cou = (select count(distinct num) from tab),
num
from tab a
)b on a.id = b.cou + 1-b.id
inner join info c on a.id = c.num
--result
name value
------------- ------------------------------------- ------------------------
Min/Max 1/99
Second smallest / second largest 2/49
Third smallest/third largest 3/39
Fourth smallest/fourth largest 5/29
Fifth Smallest/Fifth Largest 18/19
Sixth smallest/sixth largest
Seventh smallest/seventh largest
Eighth small/eighth largest
Ninth Small/Ninth Large
Tenth Smallest/Tenth Largest
(The number of rows affected is 10 rows)
--===================================
--Re-test the previous data
--===================================
--Build table drop table tab
create table tab
(
id varchar(2),
num int
)
insert into tab select '01', 1
insert into tab select '02', 3
insert into tab select '03', 3
insert into tab select '04', 5
insert into tab select '05', 18
insert into tab select '06', 99
select
c.cnum +'small/'+ c.cnum +'big' as name,
case when a.id <(a.cou + 1)*1.0/2 then cast(a.num as varchar) +'/'+cast(b.num as varchar)
when a.id = (a.cou + 1)*1.0/2 then cast(a.num as varchar)
else'' end as value
from
(
select distinct id = (select count(distinct num) from tab where num <= a.num),
cou = (select count(distinct num) from tab),
num
from tab a
)a inner join
(
select distinct id = (select count(distinct num) from tab where num <= a.num),
cou = (select count(distinct num) from tab),
num
from tab a
)b on a.id = b.cou + 1-b.id
inner join info c on a.id = c.num
--result
name value
------------- ------------------------------------- ------------------------
Min/Max 1/99
Second smallest / second largest 3/18
Third smallest/third largest 5
Fourth smallest/fourth largest
Fifth smallest / fifth largest
(The number of rows affected is 5 rows) |
|