对A表中的5个列进行编程,分别求出第1行,第2行,直到最后一行的最大值和最小值,分别插入到B表中的b1,b2列中。注意,是按行进行统计,范例如下:
表A中的数据结构如下:
a1 a2 a3 a4 a5
1 5 42 6 7
5 0 32 45 9
15 3 4 7 35
8 16 74 2 37
.
.
.
所求出的B表中的数据结构如下(设b1为最大值,b2为最小值):
b1 b2
42 1
45 0
35 3
74 2
.
.
.
请哪位帮一下忙,编一下,谢谢!
10 个解决方案
#1
用case when 嵌套判断就可以了. 不过似乎有点笨
#2
--在测试库下运行
use tempdb
--建测试表
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
--建一个有自增列的表B
select id=identity(int,1,1),* into B from A
--建函数
go
create function f_m(@id int)
returns @t table(mx int,mn int)
as
begin
declare @m table(num int)
insert @m select a1 from B where id=@id
insert @m select a2 from B where id=@id
insert @m select a3 from B where id=@id
insert @m select a4 from B where id=@id
insert @m select a5 from B where id=@id
insert @t select max(num),min(num) from @m
return
end
go
--建存放结果的表C
create table C(mx int,mn int)
go
--运用上面的函数把结果一条条插入结果表C中
declare @i int
declare @m int
select @m=max(id) from b
set @i=1
while @i<=@m
begin
insert C select * from f_m(@i)
set @i=@i+1
end
go
--查看结果
select * from C
go
--删除测试数据
drop table c
drop table a
drop table b
drop function f_m
/*结果
mx mn
----------- -----------
42 1
45 0
35 3
74 2
(所影响的行数为 4 行)*/
use tempdb
--建测试表
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
--建一个有自增列的表B
select id=identity(int,1,1),* into B from A
--建函数
go
create function f_m(@id int)
returns @t table(mx int,mn int)
as
begin
declare @m table(num int)
insert @m select a1 from B where id=@id
insert @m select a2 from B where id=@id
insert @m select a3 from B where id=@id
insert @m select a4 from B where id=@id
insert @m select a5 from B where id=@id
insert @t select max(num),min(num) from @m
return
end
go
--建存放结果的表C
create table C(mx int,mn int)
go
--运用上面的函数把结果一条条插入结果表C中
declare @i int
declare @m int
select @m=max(id) from b
set @i=1
while @i<=@m
begin
insert C select * from f_m(@i)
set @i=@i+1
end
go
--查看结果
select * from C
go
--删除测试数据
drop table c
drop table a
drop table b
drop function f_m
/*结果
mx mn
----------- -----------
42 1
45 0
35 3
74 2
(所影响的行数为 4 行)*/
#3
create table A(a1 int , a2 int , a3 int , a4 int , a5 int)
insert into A select
1 , 5 ,42, 6 , 7 union select
5 , 0 , 32 , 45 , 9 union select
15 , 3 , 4 , 7 , 35 union select
8 , 16 , 74 , 2 , 37
select(case when
(case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)-a5>0
then (case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)
else a5 end)
from A
insert into A select
1 , 5 ,42, 6 , 7 union select
5 , 0 , 32 , 45 , 9 union select
15 , 3 , 4 , 7 , 35 union select
8 , 16 , 74 , 2 , 37
select(case when
(case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)-a5>0
then (case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)
else a5 end)
from A
#4
只写了 max
拷贝粘贴太累了 哈哈
拷贝粘贴太累了 哈哈
#5
楼上的这个方法, 似乎还不如建立一个max/min函数方便?
create function dbo.f_max(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select max(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
create function dbo.f_min(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select min(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
-- 调用函数实现处理
select dbo.f_max(a1, a2, a3, a4, a5),
dbo.f_min(a1, a2, a3, a4, a5)
from tb
create function dbo.f_max(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select max(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
create function dbo.f_min(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select min(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
-- 调用函数实现处理
select dbo.f_max(a1, a2, a3, a4, a5),
dbo.f_min(a1, a2, a3, a4, a5)
from tb
#6
-- 写完函数才想起, 其实有简单的方法可以直接实现.
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
#7
use tempdb
go
--test data
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
go
-- select
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
go
-- drop test
drop table A
/*-- 结果
max min
----------- -----------
42 1
45 0
35 3
74 2
(4 行受影响)
--*/
go
--test data
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
go
-- select
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
go
-- drop test
drop table A
/*-- 结果
max min
----------- -----------
42 1
45 0
35 3
74 2
(4 行受影响)
--*/
#8
老大就是老大!
顶起来!
顶起来!
#9
学习了
#10
declare @t table(a1 int , a2 int , a3 int , a4 int , a5 int)
declare @t1 table(b1 int , b2 int)
insert into @t
select 1 , 5 , 42 , 6 , 7 union
select 5 , 0 , 32 , 45 , 9 union
select 15 , 3 , 4 , 7 , 35 union
select 8 , 16 , 74 , 2 , 37
declare @n int , @n1 int , @n2 int , @n3 int , @n4 int
declare @n_max int , @n_min int
declare cursor1 scroll cursor for select * from @t
open cursor1
fetch next from cursor1 into @n ,@n1,@n2,@n3,@n4
while @@fetch_status = 0
begin
select @n_max = @n
select @n_min = @n
if @n_max < @n1
begin
select @n_max = @n1
end
if @n_max < @n2
begin
select @n_max = @n2
end
if @n_max < @n3
begin
select @n_max = @n3
end
if @n_max < @n4
begin
select @n_max = @n4
end
-------------------min-----------------------
if @n_min > @n1
begin
select @n_min = @n1
end
if @n_min > @n2
begin
select @n_min = @n2
end
if @n_min > @n3
begin
select @n_min = @n3
end
if @n_min > @n4
begin
select @n_min = @n4
end
insert into @t1
select @n_max , @n_min
fetch next from cursor1 into @n ,@n1,@n2,@n3,@n4
end
close cursor1
deallocate cursor1
select * from @t1
#1
用case when 嵌套判断就可以了. 不过似乎有点笨
#2
--在测试库下运行
use tempdb
--建测试表
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
--建一个有自增列的表B
select id=identity(int,1,1),* into B from A
--建函数
go
create function f_m(@id int)
returns @t table(mx int,mn int)
as
begin
declare @m table(num int)
insert @m select a1 from B where id=@id
insert @m select a2 from B where id=@id
insert @m select a3 from B where id=@id
insert @m select a4 from B where id=@id
insert @m select a5 from B where id=@id
insert @t select max(num),min(num) from @m
return
end
go
--建存放结果的表C
create table C(mx int,mn int)
go
--运用上面的函数把结果一条条插入结果表C中
declare @i int
declare @m int
select @m=max(id) from b
set @i=1
while @i<=@m
begin
insert C select * from f_m(@i)
set @i=@i+1
end
go
--查看结果
select * from C
go
--删除测试数据
drop table c
drop table a
drop table b
drop function f_m
/*结果
mx mn
----------- -----------
42 1
45 0
35 3
74 2
(所影响的行数为 4 行)*/
use tempdb
--建测试表
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
--建一个有自增列的表B
select id=identity(int,1,1),* into B from A
--建函数
go
create function f_m(@id int)
returns @t table(mx int,mn int)
as
begin
declare @m table(num int)
insert @m select a1 from B where id=@id
insert @m select a2 from B where id=@id
insert @m select a3 from B where id=@id
insert @m select a4 from B where id=@id
insert @m select a5 from B where id=@id
insert @t select max(num),min(num) from @m
return
end
go
--建存放结果的表C
create table C(mx int,mn int)
go
--运用上面的函数把结果一条条插入结果表C中
declare @i int
declare @m int
select @m=max(id) from b
set @i=1
while @i<=@m
begin
insert C select * from f_m(@i)
set @i=@i+1
end
go
--查看结果
select * from C
go
--删除测试数据
drop table c
drop table a
drop table b
drop function f_m
/*结果
mx mn
----------- -----------
42 1
45 0
35 3
74 2
(所影响的行数为 4 行)*/
#3
create table A(a1 int , a2 int , a3 int , a4 int , a5 int)
insert into A select
1 , 5 ,42, 6 , 7 union select
5 , 0 , 32 , 45 , 9 union select
15 , 3 , 4 , 7 , 35 union select
8 , 16 , 74 , 2 , 37
select(case when
(case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)-a5>0
then (case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)
else a5 end)
from A
insert into A select
1 , 5 ,42, 6 , 7 union select
5 , 0 , 32 , 45 , 9 union select
15 , 3 , 4 , 7 , 35 union select
8 , 16 , 74 , 2 , 37
select(case when
(case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)-a5>0
then (case when (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end )-a4>0 then (case when (case when a1-a2>0 then a1 else a2 end)-a3>0 then (case when a1-a2>0 then a1 else a2 end) else a3 end ) else a4 end)
else a5 end)
from A
#4
只写了 max
拷贝粘贴太累了 哈哈
拷贝粘贴太累了 哈哈
#5
楼上的这个方法, 似乎还不如建立一个max/min函数方便?
create function dbo.f_max(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select max(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
create function dbo.f_min(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select min(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
-- 调用函数实现处理
select dbo.f_max(a1, a2, a3, a4, a5),
dbo.f_min(a1, a2, a3, a4, a5)
from tb
create function dbo.f_max(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select max(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
create function dbo.f_min(
@col1 int, @col2 int, @col3 int, @col4 int, @col5 int
)returns int
as
begin
return((select min(col) from(
select col=@col1 union
select @col2 union
select @col3 union
select @col4 union
select @col5)))
end
go
-- 调用函数实现处理
select dbo.f_max(a1, a2, a3, a4, a5),
dbo.f_min(a1, a2, a3, a4, a5)
from tb
#6
-- 写完函数才想起, 其实有简单的方法可以直接实现.
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
#7
use tempdb
go
--test data
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
go
-- select
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
go
-- drop test
drop table A
/*-- 结果
max min
----------- -----------
42 1
45 0
35 3
74 2
(4 行受影响)
--*/
go
--test data
create table A(a1 int,a2 int,a3 int,a4 int,a5 int)
insert A select 1, 5, 42, 6, 7
insert A select 5, 0, 32, 45, 9
insert A select 15, 3, 4, 7, 35
insert A select 8, 16, 74, 2, 37
go
-- select
select
[max]=(select max(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a),
[min]=(select min(col) from (select col=a.a1 union select a.a2 union select a.a3 union select a.a4 union select a.a5)a)
from A
go
-- drop test
drop table A
/*-- 结果
max min
----------- -----------
42 1
45 0
35 3
74 2
(4 行受影响)
--*/
#8
老大就是老大!
顶起来!
顶起来!
#9
学习了
#10
declare @t table(a1 int , a2 int , a3 int , a4 int , a5 int)
declare @t1 table(b1 int , b2 int)
insert into @t
select 1 , 5 , 42 , 6 , 7 union
select 5 , 0 , 32 , 45 , 9 union
select 15 , 3 , 4 , 7 , 35 union
select 8 , 16 , 74 , 2 , 37
declare @n int , @n1 int , @n2 int , @n3 int , @n4 int
declare @n_max int , @n_min int
declare cursor1 scroll cursor for select * from @t
open cursor1
fetch next from cursor1 into @n ,@n1,@n2,@n3,@n4
while @@fetch_status = 0
begin
select @n_max = @n
select @n_min = @n
if @n_max < @n1
begin
select @n_max = @n1
end
if @n_max < @n2
begin
select @n_max = @n2
end
if @n_max < @n3
begin
select @n_max = @n3
end
if @n_max < @n4
begin
select @n_max = @n4
end
-------------------min-----------------------
if @n_min > @n1
begin
select @n_min = @n1
end
if @n_min > @n2
begin
select @n_min = @n2
end
if @n_min > @n3
begin
select @n_min = @n3
end
if @n_min > @n4
begin
select @n_min = @n4
end
insert into @t1
select @n_max , @n_min
fetch next from cursor1 into @n ,@n1,@n2,@n3,@n4
end
close cursor1
deallocate cursor1
select * from @t1