34,837
社区成员




drop table A
create table A(id nvarchar(10),name nvarchar(10),sex char(2))
insert into A select 'aaa','刘备','男' union select 'bbb','赵云','男'
union select 'ccc','孙权','男' union select 'ccc','小乔','女'
select px from # where name='小乔'
select * from # where px between (select px from # where name='小乔')-1 and (select px from # where name='小乔')+1
select [Sex],count([Sex]) as cnt from # group by [sex]
drop table A
if(object_id('表A') is not null) drop table 表A
create table 表A(id varchar(100),name varchar(100),sex varchar(100))
insert into 表A
select 'aaa','刘备','男' union all
select 'bbb','赵云','男' union all
select 'ccc','马超','男' union all
select 'ddd','孔明','男' union all
select 'eee','小乔','女' union all
select 'fff','大乔','女' union all
select 'ggg','孙尚香','女' union all
select 'hhh','张飞','男' union all
select 'jjj','黄忠','男' union all
select 'kkk','关羽','男' union all
select 'lll','甘宁','男' union all
select 'mmm','孙权','男'
if(object_id('getVal') is not null) drop function getVal
create function getVal (@nameVal varchar(100))
returns @retFindReports table (行号 int,男多女多 varchar(100))
AS
begin
--取得记录号
declare @name varchar(100),@xh int,@xh_out varchar(100)
set @xh=1
declare getXh_Cursor cursor for
select name from 表A
open getXh_Cursor
fetch next from getXh_Cursor
into @name
while @@FETCH_STATUS = 0
begin
if(@name=@nameVal)
break
set @xh=@xh+1
fetch next from getXh_Cursor
into @name
end
close getXh_Cursor
deallocate getXh_Cursor
--取得人数判断
declare @male int,@female int,@mf varchar(100)
select @male=sum(case when sex='男' then 1 else 0 end),@female=sum(case when sex='女' then 1 else 0 end) from 表A
if @male=@female
set @mf='男女数据一样多'
else if @male>@female
set @mf='男数据多'
else
set @mf='女数据多'
INSERT @retFindReports
select @xh,@mf
RETURN
end
go
select * from dbo.getVal('小乔')
--
行号 男多女多
5 男数据多
-->生成测试数据
declare @tb table([ID] nvarchar(3),[Name] nvarchar(3),[Sex] nvarchar(1))
Insert @tb
select N'aaa',N'刘备',N'男' union all
select N'bbb',N'赵云',N'男' union all
select N'ccc',N'马超',N'男' union all
select N'ddd',N'孔明',N'男' union all
select N'eee',N'小乔',N'女' union all
select N'fff',N'大乔',N'女' union all
select N'ggg',N'孙尚香',N'女' union all
select N'hhh',N'张飞',N'男' union all
select N'jjj',N'黄忠',N'男' union all
select N'kkk',N'关羽',N'男' union all
select N'lll',N'甘宁',N'男' union all
select N'mmm',N'孙权',N'男'
select id from @tb where [name] = '小乔'
/*
eee
*/
select px= count(1) from @tb where id<= (select id from @tb where [name]='小乔')
/*
5
*/
select * from
(
select px =(select count(1) from @tb where id<=t.id),t.* from @tb t
)A
where px between (select count(1) from @tb where id <=(select id from @tb where [name]='小乔'))-2
and (select count(1) from @tb where id <=(select id from @tb where [name]='小乔'))+2
and [name]!='小乔'
/*
px ID Name Sex
----------- ---- ---- ----
3 ccc 马超 男
4 ddd 孔明 男
5 eee 小乔 女
6 fff 大乔 女
7 ggg 孙尚香 女
*/
select sum (case [Sex] when '男' then 1 else 0 end) as '男',
sum (case [Sex] when '女' then 1 else 0 end) as '女',
case when sum (case [Sex] when '男' then 1 else 0 end) >= sum (case [Sex] when '男' then 1 else 0 end) then '男多于女' else '女多于男' end as '结果'
from @tb
/*
男 女 结果
----------- ----------- --------
9 3 男多于女
*/
drop table A
create table A(id nvarchar(10),name nvarchar(10),sex char(2))
insert into A select 'aaa','刘备','男' union select 'bbb','赵云','男'
union select 'ccc','孙权','男' union select 'ccc','小乔','女'
select px from # where name='小乔'
select * from # where px between (select px from # where name='小乔')-1 and (select px from # where name='小乔')+1
select [Sex],count([Sex]) as cnt from # group by [sex]
select oid as '第几条' from (
select row_number() over (order by id) as oid,* from 表
) t where Name='小乔'
select id from 表A where [name]='小乔'
select top 2 * from 表A where id < (select id from 表A where [name]='小乔') order by id desc
union all
select top 2 * from 表A where id > (select id from 表A where [name]='小乔') order by id asc
select [Sex],count([Sex]) as cnt from 表A group by [sex]