27,581
社区成员




if object_id('tb') is not null
drop table tb
go
create table tb(f1 int,id int,flag int)
go
--生成测试数据
insert into tb select
811,1, 0 union all select
811,2, 1 union all select
811,3, 0 union all select
811,4, 1 union all select
900,5, 0 union all select
900,6, 0 union all select
900,7, 0
go
if object_id('getID') is not null
drop function dbo.getID
go
create function dbo.getID(@Num int)
returns int
as
begin
declare @id as int
select top 1 @id=id from tb where f1=@num order by flag desc,id asc
return @id
end
go
SELECT distinct f1,dbo.getID(f1) id from tb
/*
f1 id
----------- -----------
811 2
900 5
*/
CREATE TABLE testA (f1 INT, id INT, flag BIT)
INSERT INTO testA VALUES(811, 1, 0 )
INSERT INTO testA VALUES(811, 2, 1 )
INSERT INTO testA VALUES(811, 3, 0 )
INSERT INTO testA VALUES(811, 4, 1 )
INSERT INTO testA VALUES(900, 5, 0 )
INSERT INTO testA VALUES(900, 6, 0 )
INSERT INTO testA VALUES(900, 7, 0 )
GO
CREATE FUNCTION test_fun
(
@f1 INT
)
RETURNS table
AS
return(
SELECT TOP 1 id FROM dbo.testA WHERE f1 = @f1 ORDER BY flag DESC,id
)
GO
SELECT * from dbo.test_fun(811)
SELECT * from dbo.test_fun(900)
drop table testA
drop function test_fun
/*
id
-----------
2
(1 行受影响)
id
-----------
5
(1 行受影响)
*/
declare @tb table
(
f1 int,
id int,
flag int
)
insert into @tb
select 811 , 1, 0 union all
select 811 , 2 , 1 union all
select 811 , 3 , 0 union all
select 811 , 4 , 1 union all
select 900 , 5 , 0 union all
select 900 , 6 , 0 union all
select 900 , 7 , 0
declare @i int
set @i = 811
select case when exists(select top 1 id from @tb where f1 = @i and flag = 1 order by id)
then (select top 1 id from @tb where f1 = @i and flag = 1 order by id)
else (select top 1 id from @tb where f1 = @i and flag = 0 order by id) end
declare @table table (f1 int, id int, flag int)
insert into @table
select 811 , 1 , 0 union all
select 811 , 2 , 1 union all
select 811 , 3 , 0 union all
select 811 , 4 , 1 union all
select 900 , 5 , 0 union all
select 900 , 6 , 0 union all
select 900 , 7 , 0
declare @f1 int
set @f1=811
select * from (
select *,row_number()over(partition by f1 order by flag desc,id asc) as rn
from @table ) x
where rn=1 and f1=@f1
if object_id('tb') is not null
drop table tb
go
create table tb(f1 int,id int,flag int)
go
--生成测试数据
insert into tb select
811,1, 0 union all select
811,2, 1 union all select
811,3, 0 union all select
811,4, 1 union all select
900,5, 0 union all select
900,6, 0 union all select
900,7, 0
go
if object_id('getID') is not null
drop function dbo.getID
go
create function dbo.getID(@Num int)
returns int
as
begin
declare @id as int
if @num = 811
select top 1 @id=id from tb where f1=@num and flag=1 order by id
if @num =900
select top 1 @id=id from tb where f1=@num order by flag desc,id
return @id
end
go
select distinct dbo.getID(f1) from tb
/*
----------- 结果
2
5
*/
CREATE TABLE testA (f1 INT, id INT, flag BIT)
INSERT INTO testA VALUES(811, 1, 0 )
INSERT INTO testA VALUES(811, 2, 1 )
INSERT INTO testA VALUES(811, 3, 0 )
INSERT INTO testA VALUES(811, 4, 1 )
INSERT INTO testA VALUES(900, 5, 0 )
INSERT INTO testA VALUES(900, 6, 0 )
INSERT INTO testA VALUES(900, 7, 0 )
SELECT * FROM testA
GO
CREATE FUNCTION test_fun
(
@f1 INT
)
RETURNS INT
AS
BEGIN
DECLARE @id INT
SELECT TOP 1 @id = id FROM dbo.testA WHERE f1 = @f1 ORDER BY flag DESC,id
RETURN @id
END
GO
SELECT dbo.test_fun(811)
SELECT dbo.test_fun(900)
create function test(@f int)
returns int
as
begin
select top 1 @f=id
from tb
where f1=@f
order by flag desc
return @f
end