人数统计的问题

tianxiawusuan 2010-02-03 11:36:30
最近写了一个人数统计的系统,要求是列及行都是动态生成的
比如:
数据库里面有个基础表A:

表A 姓名 部门 学历 出生年月
A 后勤 高中 1986-1-1
B 后勤 初中 1984-3-7
C 管理 本科 1987-2-1
D 操作 专科 1976-2-1
. . . .
. . . .
要求动态的生成一下效果的表:
部门 年龄段 高中 初中 本科.......
后勤 20-30 3 4 3 .......
30-40 3 9 0 .......
40-50 0 30 3 .......
管理 20-30 9 2 2 .......

请问怎么实现啊???
...全文
340 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianxiawusuan 2010-02-04
  • 打赏
  • 举报
回复
以前做的是简单的gridview数据绑定,这回学历是从数据库里面查询出来的,在现实的表格中以行的形式来显示,就不知道怎么绑定了??
tianxiawusuan 2010-02-04
  • 打赏
  • 举报
回复
up
tianxiawusuan 2010-02-04
  • 打赏
  • 举报
回复
呵呵。。没有错误了
这个功能的实现,在VS里面需要用到什么控件啊,貌似GRIDVIEW不能使用啊
sdnjiejie65 2010-02-04
  • 打赏
  • 举报
回复
--1。先运行这个函数
drop function ageTime
go
--获取年龄段
create function ageTime(@date varchar(10))
returns varchar(10)
as
begin
--所除的整数
declare @i_year int,@result int
select @i_year=datediff(year,@date,getdate())
select @result=@i_year/10
--if(@i_year%10>=0)
--begin
select @date=Convert(varchar(3),@result*10)+'-'+Convert(varchar(3),(@result+1)*10-1)
--end
--else
--begin
-- select @date=Convert(varchar(3),(@result-1)*10)+'-'+Convert(varchar(3),@result*10)
--end
return @date
end
go
-------------------------------------------------
-----------------------------
-------------------------------------------------

if object_id('v_hdemployee') is not null drop table v_hdemployee
go
--创建表,添加记录
create table v_hdemployee(name varchar(20),department varchar(20),LatterEducation varchar(20),Birthday varchar(10))
insert into v_hdemployee
select 'A','后勤','高中','1986-1-1' union all
select 'B','后勤','初中','1984-3-7' union all
select 'C','管理','专科','1987-2-1' union all
select 'D','操作','专科','1976-2-1' union all
select 'E','后勤','高中','1986-1-1' union all
select 'F','后勤','初中','1984-3-7' union all
select 'G','管理','专科','1987-2-1' union all
select 'H','操作','专科','1976-2-1'

select * from v_hdemployee
go

declare @sql varchar(8000)
select @sql='select department,年龄段'
select @sql=@sql+',sum(case LatterEducation when '''+LatterEducation+'''then 1 else 0 end) ['+LatterEducation+']'+char(10)+char(13)
from (select distinct LatterEducation from v_hdemployee) as a
select @sql=@sql+' from (select *,dbo.ageTime(Birthday) 年龄段 from v_hdemployee) as tb group by department,年龄段'

print @sql
exec @sql
sdnjiejie65 2010-02-04
  • 打赏
  • 举报
回复

--1。先运行这个函数
drop function ageTime
go
--获取年龄段
create function ageTime(@date varchar(10))
returns varchar(10)
as
begin
--所除的整数
declare @i_year int,@result int
select @i_year=datediff(year,@date,getdate())
select @result=@i_year/10
--if(@i_year%10>=0)
--begin
select @date=Convert(varchar(3),@result*10)+'-'+Convert(varchar(3),(@result+1)*10-1)
--end
--else
--begin
-- select @date=Convert(varchar(3),(@result-1)*10)+'-'+Convert(varchar(3),@result*10)
--end
return @date
end
go
-------------------------------------------------
-----------------------------
-------------------------------------------------

if object_id('v_hdemployee') is not null drop table v_hdemployee
go
--创建表,添加记录
create table v_hdemployee(name varchar(20),department varchar(20),LatterEducation varchar(20),Birthday varchar(10))
insert into v_hdemployee
select 'A','后勤','高中','1986-1-1' union all
select 'B','后勤','初中','1984-3-7' union all
select 'C','管理','专科','1987-2-1' union all
select 'D','操作','专科','1976-2-1' union all
select 'E','后勤','高中','1986-1-1' union all
select 'F','后勤','初中','1984-3-7' union all
select 'G','管理','专科','1987-2-1' union all
select 'H','操作','专科','1976-2-1'

select * from v_hdemployee
go

declare @sql varchar(8000)
select @sql='select department,年龄段'
select @sql=@sql+',sum(case LatterEducation when '''+LatterEducation+'''then 1 else 0 end) ['+LatterEducation+']'+char(10)+char(13)
from (select distinct LatterEducation from v_hdemployee) as a
select @sql=@sql+' from (select *,dbo.ageTime(Birthday) 年龄段 from v_hdemployee) as tb group by department,年龄段'

print @sql

--select department,年龄段,sum(case LatterEducation when '初中'then 1 else 0 end) [初中]
--,sum(case LatterEducation when '高中'then 1 else 0 end) [高中]
--,sum(case LatterEducation when '专科'then 1 else 0 end) [专科]
-- from (select *,dbo.ageTime(Birthday) 年龄段 from v_hdemployee) as tb group by department,年龄段
sdnjiejie65 2010-02-04
  • 打赏
  • 举报
回复
不好意思,昨天有点事先走了..什么语法错误...
ycg_893 2010-02-03
  • 打赏
  • 举报
回复
也可以用交叉表.
zhushoudong 2010-02-03
  • 打赏
  • 举报
回复
这个不是sql语句问题么 没什么程序实现的
readfuture 2010-02-03
  • 打赏
  • 举报
回复
友情 帮顶。
睡神在睡觉 2010-02-03
  • 打赏
  • 举报
回复
没啥好说的,视图
LIU312591 2010-02-03
  • 打赏
  • 举报
回复
提示你一下,将各个部门的select数据后,使用union关键字,拼接成一张数据库表。
比如:
select * from 表where 部门 = ‘’;
union
select * from 表where 管理 = ‘’;
union
select * from 表where 后勤 = ‘’;

其他只是技术问题了
mr_china 2010-02-03
  • 打赏
  • 举报
回复
用SQL语句多分几次组就行了,最好写成视图,可以随时修改以满足需求变化
xxlxmd 2010-02-03
  • 打赏
  • 举报
回复
额 你说的是程序 还是 SQL 语句问题啊
tianxiawusuan 2010-02-03
  • 打赏
  • 举报
回复
有没有人可以帮哈忙啊。。。谢谢啦
tianxiawusuan 2010-02-03
  • 打赏
  • 举报
回复
sdnjiejie65,你好..怎么显示有语法错误
tianxiawusuan 2010-02-03
  • 打赏
  • 举报
回复
我按zengzhan的方法写了一个存储过程,显示成功 。我在新建查询的地方执行这个存储过程的时候,没有出现表格,只显示执行成功,这是什么原因啊
tianxiawusuan 2010-02-03
  • 打赏
  • 举报
回复
我按zengzhan的方法写了一个存储过程,显示成功 。我在新建查询的地方执行这个存储过程的时候,没有出现表格,只显示执行成功,这是什么原因啊
xiongxyt2 2010-02-03
  • 打赏
  • 举报
回复
SQL语句按部门分组应该能实现,最好写一个视图
加载更多回复(19)

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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