表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
select name,sum(case Score when '胜' then 1 else 0 end) as 胜 ,
sum(case score when '负' then 1 else 0 end) as 负
from score group by name
运行结果:
时间 胜 负
2005-05-09 2 2
2005-05-10 1 2
4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记
录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
select (case when 语文 >=80 then '优秀'
when 语文 >=60 then '及格'
else '不及格' end ) 语文,
(case when 数学 >=80 then '优秀'
when 数学 >=60 then '及格'
else '不及格' end ) 数学,
(case when 英语 >=80 then '优秀'
when 英语 >=60 then '及格'
else '不及格' end ) 英语
from #tmp
从数据表中随机取出10条记录
select top 10 * from Phone order by newid()
--取表的所有字段
select name from syscolumns where id=object_id('T_GRDA')
SQL server存储过程分页
Create proc page
@pageindex int, --当前页数
@pagenum int --每页条数
as
select * from (select ROW_NUMBER() over(order by GRBH) as rownum,
* from T_GRDA) t where t.rownum>(@pageindex-1)*@pagenum and t.rownum<=@pageindex*@pagenum