求一条SQL句子:

jumin80 2008-07-10 01:56:03
有这么一个表:
id name sex grade
1 aa 1 69
2 aa2 2 69
3 aa3 2 50
4 aa4 1 89
5 aa5 2 77
6 aa6 1 75
7 aa7 1 50

等等这样的数据,数据量特别大,现在想统计这样的数据: (sex 表示性别: 1,男;2 女)

列出男的分数(grade)>=60,女的(grade)>=50 的人员名单,看有好的建议没?
...全文
79 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeff_wang 2008-07-10
  • 打赏
  • 举报
回复
如果是个常用操作,一定要建立Index on Sex
紫气东来_999 2008-07-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shoushii 的回复:]
SQL codeSELECT id,name,(CASE sex WHEN 1 THEN '男' ELSE '女' END),grade FROM Table WHERE Sex=1 AND grade>=60
UNION ALL
SELECT id,name,(CASE sex WHEN 1 THEN '男' ELSE '女' END),grade FROM Table WHERE Sex=2 AND grade>=50
[/Quote]

UNION 好过 OR
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xiaojing7 的回复:]
SQL codecreate Proc Proc_GetName
@BoyPoint int,
@GirlPoint int
as
begin transaction
Declare @sql varchar(1024)
set @sql='elect name as 名单 where 1=1 '

if(@BoyPoint is not null)
begin
set @sql=@sql+'and sex=1 and grade>='+@BoyPoint
end
if(@GirlPoint is not null)
begin
set @sql=@sql='and sex=2 and grade>='+@GirlPoint
end
exe…

很好!顶一下!我也喜欢用存储过程!被你先了一步!
[/Quote]
xiaojing7 2008-07-10
  • 打赏
  • 举报
回复
create Proc Proc_GetName
@BoyPoint int,
@GirlPoint int
as
begin transaction
Declare @sql varchar(1024)
set @sql='elect name as 名单 where 1=1 '

if(@BoyPoint is not null)
begin
set @sql=@sql+'and sex=1 and grade>='+@BoyPoint
end
if(@GirlPoint is not null)
begin
set @sql=@sql='and sex=2 and grade>='+@GirlPoint
end
exec(@sql)

if @@error <>0
begin
rollback transaction
end
else
begin
commit transaction
end


conan304 2008-07-10
  • 打赏
  • 举报
回复
declare @t table([id] int identity(1,1),[name] varchar(3),sex int,grade int)
insert @t([name],sex,grade) select 'aa',1,69
union all select 'aa2',2,69
union all select 'aa3',2,50
union all select 'aa4',1,89
union all select 'aa5',2,77
union all select 'aa6',1,75
union all select 'aa7',1,50

select * from @t
where grade>=60 or (sex=2 and grade>=50)

/*

(所影响的行数为 7 行)

id name sex grade
----------- ---- ----------- -----------
1 aa 1 69
2 aa2 2 69
3 aa3 2 50
4 aa4 1 89
5 aa5 2 77
6 aa6 1 75

(所影响的行数为 6 行)
*/
shoushii 2008-07-10
  • 打赏
  • 举报
回复
SELECT id,name,(CASE sex WHEN 1 THEN '男' ELSE '女' END),grade FROM Table WHERE Sex=1 AND grade>=60
UNION ALL
SELECT id,name,(CASE sex WHEN 1 THEN '男' ELSE '女' END),grade FROM Table WHERE Sex=2 AND grade>=50
whoami333 2008-07-10
  • 打赏
  • 举报
回复
select name from tale1 where (sex=1 and grade>=60) or (sex=2 and grade>=50)
hsabout 2008-07-10
  • 打赏
  • 举报
回复
select * from table where (sex='1' and grade>=60) or (sex='2' and grade>=50)
不知道ok否
sheng9hhd 2008-07-10
  • 打赏
  • 举报
回复
where ([sex]=1 and grade>=60) or ([sex]=2 and grade>=50)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧