梦想与现实的落差,就是我们离成功的距离!!

博客园 首页 新随笔 联系 订阅 管理
  34 Posts :: 3 Stories :: 344 Comments :: 11 Trackbacks
有一个这样的表,内容如下:
ID     Year Month    Amount Project
1 2005    01 2000.00 差旅费
2 2005 01 3000.00 加班费
3 2005 01 5000.00 差旅费
4 2005 02 3000.00 医疗费
5 2005 02 4000.00 差旅费
6 2005 02 5000.00 加班费
7 2005 02 1000.00 医疗费
…… …… …… …… ……

用一个sql查询,可以用嵌套。表示成下面表格

Project  一月 二月 …… 十二月
差旅费 7000.00 4000.00 …… ……
加班费 3000.00 5000.00 …… ……
医疗费 0.00 4000.00 …… ……
…… …… …… …… ……
小弟我(其实我不小了啊,但还很菜啦,所以职能这么样了)真的是搞不出来,希望大家指点一二啊!
posted on 2005-09-14 15:42 叶漂 阅读(833) 评论(13)  编辑 收藏

评论

#1楼  2005-09-14 18:03 Heero [未注册用户]
select Project ,
(select sum(amount) from blogtest b where month=1 and b.project= a.project )as '一月',
(select sum(amount) from blogtest b where month=2 and b.project= a.project )as '二月'
from blogtest a
group by project
  回复  引用    

#2楼  2005-09-14 18:05 Heero [未注册用户]
这个好像能用
之后再把三月,四月...的补上就行了
  回复  引用    

#3楼  2005-09-15 15:26 叶漂 [未注册用户]
但是这个有点小问题,就拿第一个月来说,没有“医疗费”,按常理就该现实成0.00,但这个显示成NULL,希望可以用什么方法来解决下,当然还是谢谢你了
  回复  引用    

#4楼  2005-09-15 16:45 单摆      
不是有一个ISNULL函数吗?
  回复  引用  查看    

#5楼  2005-09-16 09:48 Heero [未注册用户]
可以在呈现表单的时候(比如说在asp.net中)
对NULL进行一下判断就可以了
ISDBNull()(印象中...-_-;)
  回复  引用    

#6楼  2005-11-30 09:48 不错 [未注册用户]
可以用SQL SERVER交叉数据报表的查询语句,后再SUM一次就可以了
在SQL SERVER帮助里查交叉数据报表
  回复  引用    

#7楼  2005-11-30 09:49 不错 [未注册用户]
是NULL用ISNULL(fieldname,0)不就行了?
  回复  引用    

#8楼  2005-11-30 10:11 不错 [未注册用户]
我动手写了一个,就用到简单的一个按project分组的交叉数据报表就可以
select project,
sum(case when month='01' then amount else 0 end) as '一月',
sum(case when month='02' then amount else 0 end) as '二月',
sum(case when month='03' then amount else 0 end) as '三月',
sum(case when month='04' then amount else 0 end) as '四月',
sum(case when month='05' then amount else 0 end) as '五月',
sum(case when month='06' then amount else 0 end) as '六月',
sum(case when month='07' then amount else 0 end) as '七月',
sum(case when month='08' then amount else 0 end) as '八月',
sum(case when month='09' then amount else 0 end) as '九月',
sum(case when month='10' then amount else 0 end) as '十月',
sum(case when month='11' then amount else 0 end) as '十一月',
sum(case when month='12' then amount else 0 end) as '十二月'
from table where year='2005' group by project

  回复  引用    

倘若月份字段换成动态的,该怎么写?
  回复  引用    

#10楼  2006-06-18 11:48 Matildawaltzer      
或许这个可以帮上你的忙!
http://matildawaltzer.cnblogs.com/archive/2006/06/07/419809.html
  回复  引用  查看    

#11楼  2006-06-29 17:15 qingsong [未注册用户]
@ZZABCZZABCAAAOS

说清楚一些,怎么要求动态?

  回复  引用    

#12楼  2007-06-21 15:23 shally [未注册用户]
传参数,当变量不就可以了?
  回复  引用    

#13楼  2008-03-21 18:06 沙影 [未注册用户]
呵呵 8楼的基本正确

  回复  引用    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交