|
发表于 2020-11-27 20:45:01
|
显示全部楼层
--确实有问题
--建表
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
--修改后的语句
select
c.cnum + '小/'+ c.cnum + '大'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
--结果
name value
------------- -------------------------------------------------------------
最小/最大 1/99
次小/次大 2/49
第三小/第三大 3/39
第四小/第四大 5/29
第五小/第五大 18/19
第六小/第六大
第七小/第七大
第八小/第八大
第九小/第九大
第十小/第十大
(所影响的行数为 10 行)
--====================================
--再测试之前的数据
--====================================
--建表 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 + '小/'+ c.cnum + '大'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
--结果
name value
------------- -------------------------------------------------------------
最小/最大 1/99
次小/次大 3/18
第三小/第三大 5
第四小/第四大
第五小/第五大
(所影响的行数为 5 行)
|
|