34,590
社区成员
发帖
与我相关
我的任务
分享
-->测试数据1: @T
declare @T table (smallid int,smallbigid int,small1 int,small2 int,small3 int,small4 int,small5 int,small6 int,small7 int)
insert into @T
select 1,1,10,11,15,16,17,12,12 union all
select 2,1,5,8,11,14,12,null,11 union all
select 3,1,2,14,13,18,20,14,17 union all
select 4,2,5,12,10,11,14,17,18 union all
select 5,2,6,45,42,12,17,13,19 union all
select 6,3,7,8,5,20,14,18,18 union all
select 7,3,8,12,14,13,18,19,16 union all
select 8,3,10,14,11,15,16,17,19
-->NULL按最大处理
select a.* from @T a inner join
(
select smallid,small=min(small)
from
(
select smallid,small=small1 from @T union all
select smallid,small=small2 from @T union all
select smallid,small=small3 from @T union all
select smallid,small=small4 from @T union all
select smallid,small=small5 from @T union all
select smallid,small=small6 from @T union all
select smallid,small=small7 from @T
) t
group by smallid
) b
on a.smallid=b.smallid
order by b.small
/*
smallid smallbigid small1 small2 small3 small4 small5 small6 small7
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
3 1 2 14 13 18 20 14 17
4 2 5 12 10 11 14 17 18
2 1 5 8 11 14 12 NULL 11
6 3 7 8 5 20 14 18 18
5 2 6 45 42 12 17 13 19
7 3 8 12 14 13 18 19 16
8 3 10 14 11 15 16 17 19
1 1 10 11 15 16 17 12 12
*/
-->NULL按最小处理
select a.* from @T a inner join
(
select smallid,small=min(isnull(small,-2147483648))
from
(
select smallid,small=small1 from @T union all
select smallid,small=small2 from @T union all
select smallid,small=small3 from @T union all
select smallid,small=small4 from @T union all
select smallid,small=small5 from @T union all
select smallid,small=small6 from @T union all
select smallid,small=small7 from @T
) t
group by smallid
) b
on a.smallid=b.smallid
order by b.small
/*
smallid smallbigid small1 small2 small3 small4 small5 small6 small7
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2 1 5 8 11 14 12 NULL 11
3 1 2 14 13 18 20 14 17
4 2 5 12 10 11 14 17 18
6 3 7 8 5 20 14 18 18
5 2 6 45 42 12 17 13 19
7 3 8 12 14 13 18 19 16
8 3 10 14 11 15 16 17 19
1 1 10 11 15 16 17 12 12
*/
--只排序不用显示:
create table T(smallid int,smallbigid int, small1 int, small2 int,
small3 int, small4 int, small5 int, small6 int, small7 int)
insert T select 1, 1, 10, 11,15, 16, 17, 12,12 --空值
insert T select 2, 1, 5, 8, 11, 14, 12, 11, 11 --空值
insert T select 3, 1, 2, 14, 13, 18, 20, 14, 17
insert T select 4, 2, 5, 12, 10, 11, 14,17, 18
insert T select 5, 2, 6, 45, 42, 12, 17, 13, 19
insert T select 6, 3, 7, 8, 5 ,20, 14, 18, 18
insert T select 7, 3, 8, 12, 14, 13, 18, 19, 16
insert T select 8, 3, 10, 14, 11, 15, 16, 17, 19
go
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7
from
T
order by (select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp) asc
--truncate table T
smallid smallbigid small1 small2 small3 small4 small5 small6 small7
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
3 1 2 14 13 18 20 14 17
4 2 5 12 10 11 14 17 18
2 1 5 8 11 14 12 11 11
6 3 7 8 5 20 14 18 18
5 2 6 45 42 12 17 13 19
7 3 8 12 14 13 18 19 16
8 3 10 14 11 15 16 17 19
1 1 10 11 15 16 17 12 12
(所影响的行数为 8 行)
----------
create table T(smallid int,smallbigid int, small1 int, small2 int,
small3 int, small4 int, small5 int, small6 int, small7 int)
insert T select 1, 1, 10, 11,15, 16, 17, 12,12 --空值
insert T select 2, 1, 5, 8, 11, 14, 12, 11, 11 --空值
insert T select 3, 1, 2, 14, 13, 18, 20, 14, 17
insert T select 4, 2, 5, 12, 10, 11, 14,17, 18
insert T select 5, 2, 6, 45, 42, 12, 17, 13, 19
insert T select 6, 3, 7, 8, 5 ,20, 14, 18, 18
insert T select 7, 3, 8, 12, 14, 13, 18, 19, 16
insert T select 8, 3, 10, 14, 11, 15, 16, 17, 19
go
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7,
[MIN]=(select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp)
from
T
order by [MIN] asc
--truncate table T
smallid smallbigid small1 small2 small3 small4 small5 small6 small7 MIN
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
3 1 2 14 13 18 20 14 17 2
4 2 5 12 10 11 14 17 18 5
2 1 5 8 11 14 12 11 11 5
6 3 7 8 5 20 14 18 18 5
5 2 6 45 42 12 17 13 19 6
7 3 8 12 14 13 18 19 16 8
8 3 10 14 11 15 16 17 19 10
1 1 10 11 15 16 17 12 12 10
(所影响的行数为 8 行)
6 3 7 8 5 20 14 18 18 (min=5)
4 2 5 12 10 11 14 17 18 (min=5)
2 1 5 8 11 14 12 11 11 (min=5)
1 1 10 11 15 16 17 12 12 (min=10)
8 3 10 14 11 15 16 17 19 (min=10)
min相等时排序依据是什么?
-->测试数据1: @T
declare @T table (smallid int,smallbigid int,small1 int,small2 int,small3 int,small4 int,small5 int,small6 int,small7 int)
insert into @T
select 1,1,10,11,15,16,17,12,12 union all
select 2,1,5,8,11,14,12,11,11 union all
select 3,1,2,14,13,18,20,14,17 union all
select 4,2,5,12,10,11,14,17,18 union all
select 5,2,6,45,42,12,17,13,19 union all
select 6,3,7,8,5,20,14,18,18 union all
select 7,3,8,12,14,13,18,19,16 union all
select 8,3,10,14,11,15,16,17,19
select a.* from @T a inner join
(
select smallid,small=min(small)
from
(
select smallid,small=small1 from @T union all
select smallid,small=small2 from @T union all
select smallid,small=small3 from @T union all
select smallid,small=small4 from @T union all
select smallid,small=small5 from @T union all
select smallid,small=small6 from @T union all
select smallid,small=small7 from @T
) t
group by smallid
) b
on a.smallid=b.smallid
order by b.small
/*
smallid smallbigid small1 small2 small3 small4 small5 small6 small7
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
3 1 2 14 13 18 20 14 17
4 2 5 12 10 11 14 17 18
2 1 5 8 11 14 12 11 11
6 3 7 8 5 20 14 18 18
5 2 6 45 42 12 17 13 19
7 3 8 12 14 13 18 19 16
8 3 10 14 11 15 16 17 19
1 1 10 11 15 16 17 12 12
*/
create table T(smallid int,smallbigid int, small1 int, small2 int,
small3 int, small4 int, small5 int, small6 int, small7 int)
insert T select 1, 1, 10, 11,15, 16, 17, 12,null --空值
insert T select 2, 1, 5, 8, 11, 14, 12, 11, null --空值
insert T select 3, 1, 2, 14, 13, 18, 20, 14, 17
insert T select 4, 2, 5, 12, 10, 11, 14,17, 18
insert T select 5, 2, 6, 45, 42, 12, 17, 13, 19
insert T select 6, 3, 7, 8, 5 ,20, 14, 18, 18
insert T select 7, 3, 8, 12, 14, 13, 18, 19, 16
insert T select 8, 3, 10, 14, 11, 15, 16, 17, 19
go
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7,
[MIN]=(select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp)
from
T
--truncate table T
smallid smallbigid small1 small2 small3 small4 small5 small6 small7 MIN
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 1 10 11 15 16 17 12 NULL 10
2 1 5 8 11 14 12 11 NULL 5
3 1 2 14 13 18 20 14 17 2
4 2 5 12 10 11 14 17 18 5
5 2 6 45 42 12 17 13 19 6
6 3 7 8 5 20 14 18 18 5
7 3 8 12 14 13 18 19 16 8
8 3 10 14 11 15 16 17 19 10
(所影响的行数为 8 行)
警告: 聚合或其它 SET 操作消除了空值。
create table T(smallid int,smallbigid int, small1 int, small2 int,
small3 int, small4 int, small5 int, small6 int, small7 int)
insert T select 1, 1, 10, 11,15, 16, 17, 12, 12
insert T select 2, 1, 5, 8, 11, 14, 12, 11, 11
insert T select 3, 1, 2, 14, 13, 18, 20, 14, 17
insert T select 4, 2, 5, 12, 10, 11, 14,17, 18
insert T select 5, 2, 6, 45, 42, 12, 17, 13, 19
insert T select 6, 3, 7, 8, 5 ,20, 14, 18, 18
insert T select 7, 3, 8, 12, 14, 13, 18, 19, 16
insert T select 8, 3, 10, 14, 11, 15, 16, 17, 19
go
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7,
[MIN]=(select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp)
from
T
smallid smallbigid small1 small2 small3 small4 small5 small6 small7 MIN
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 1 10 11 15 16 17 12 12 10
2 1 5 8 11 14 12 11 11 5
3 1 2 14 13 18 20 14 17 2
4 2 5 12 10 11 14 17 18 5
5 2 6 45 42 12 17 13 19 6
6 3 7 8 5 20 14 18 18 5
7 3 8 12 14 13 18 19 16 8
8 3 10 14 11 15 16 17 19 10
(所影响的行数为 8 行)
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7,
[MIN]=(select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp)
from
T
create table T(smallid int,smallbigid int, small1 int, small2 int,
small3 int, small4 int, small5 int, small6 int, small7 int)
select
smallid,smallbigid,small1,small2,small3,small4,small5,small6,small7,
[MIN]=select min(COL)
from
(select [COL]=small1 union all
select [COL]=small2 union all
select [COL]=small3 union all
select [COL]=small4 union all
select [COL]=small5 union all
select [COL]=small6 union all
select [COL]=small7)Tmp
from
T