declare @count int
创新互联主要从事成都做网站、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务柴桑,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
set @count = (select COUNT(1)/3 as count from #temp)
select top (@count) * from #temp
--剩余的sql自己写写就ok了 我没明白你具体要做什么就不往下写了
---把getdate()替换成你的日期字段就可以了, between and的2个日期分别是你说的2个年头年尾的情况
select case when getdate()=cast('2014/1/1' as datetime) then datepart(week,getdate()) else 1 end as 第几周
--from table_name
where getdate() between (case when datepart(weekday,cast('2014/1/1' as datetime))1 then
cast('2014/1/1' as datetime)-datepart(weekday,cast('2014/1/1' as datetime))+2
end )
and
(case when datepart(weekday,cast('2014/12/31' as datetime))6 then
cast('2014/12/31' as datetime)-datepart(weekday,cast('2014/12/31' as datetime))+1
end )
select 部门字段名, sum(case sex when ‘男’ then 1 else 0 end ) as '男' ,女=sum(case sex when ‘女’then 1 else 0 end)
from student group by 部门字段名
别的都一样了~
在select中,用 case when 情况一 then 值1 when 情况二 then 值2 esle 值3 end 这种结构。不会用case的话,百度一下吧。
sqlserver 分组函数,请参阅:
sqlserver分组开窗函数的用法
用这些函数,分组后,可以实现分组内部编号排序,例:
create table A ([姓名] nvarchar(20),[订单数] int,[订单日期] datetime )
go
insert into A ([姓名],[订单数],[订单日期]) values ('',1900,'2014-5-6')
insert into A ([姓名],[订单数],[订单日期]) values ('',1800,'2018-5-6')
insert into A ([姓名],[订单数],[订单日期]) values ('',1800,'2018-5-6')
insert into A ([姓名],[订单数],[订单日期]) values ('小张',100,'2013-5-6')
insert into A ([姓名],[订单数],[订单日期]) values ('小明',2600,'2013-1-6')
insert into A ([姓名],[订单数],[订单日期]) values ('小明',1800,'2013-5-6')
insert into A ([姓名],[订单数],[订单日期]) values ('小李',888,'2017-3-6')
go
/*row_number 返回分组后的连续排序,不会出现重复的排序值*/
select row_number() over(partition by [姓名] order by [订单日期] desc ) as keyId,* from A
/*rank 返回分组后的连续排序,会出现跳跃排序值*/
select rank() over(partition by [姓名] order by [订单日期] desc ) as keyId,* from A
/*dense_rank 返回分组后的连续排序,不会出现跳跃排序值,但是会出现重复的排序值*/
select dense_rank() over(partition by [姓名] order by [订单日期] desc ) as keyId,* from A
go
truncate table A
drop table A
sqlserver能按部门排序列出,但无法按此格式列出。总体上说,你这属于界面表达这一层次的问题,应编写界面程序实现。数据库的任务一般还是纯粹的数据存取这个层次,所以这个概念要清晰,即不同任务分别适合放在哪个层次是很重要的。