呵呵,不说表结构,怎么回答?!假设表结构字段有:
人,项目,时间,费用
那就很简单了select 人,项目,sum(费用) as 费用
from tablename
where 时间 between 一定时间内的开始时间 and 一定时间内的结束时间
group by 人,项目不知道这样是不是你要的。

解决方案 »

  1.   

    表结构是,只有一张表
    ID 姓名  项目 金额 时间select 人,项目,sum(费用) as 费用
    from tablename
    where 时间 between 一定时间内的开始时间 and 一定时间内的结束时间
    group by 人,项目
    运行后没有任何结果,请再解答
    希望的结果是
    姓名 项目1 项目2 项目n
    aaa  2000  3000   4000
    bbb  100   300    400
      

  2.   

    select 姓名,项目,sum(金额) as 金额
    from tablename
    where 时间 between '2000-1-1' and '2003-1-1'
    group by 姓名,项目
      

  3.   

    上面的在SQL Server中运行后没有任何结果,但是应该有数据的
    另外,排列的项目也不太合乎要求
    是不是没有办法啊
      

  4.   

    数据例如下
    ID 姓名  项目 金额 时间
    1  aaa   项目1 2000 2002-3-18
    2 aaa   项目1 2000 2002-3-18
    3 aaa   项目2 1000 2002-3-18
    4 bbb   项目3 1050 2002-3-19
    5 bbb   项目1 2050 2002-3-18
    6 bbb   项目1 2500 2002-3-20希望得到
    姓名 项目1 项目2 项目n
    aaa  2000  3000   4000
    bbb  100   300    400
      

  5.   

    Sorry,上面那句的确有结果,但是排列不合上面要求,是否有方法?select 姓名,项目,sum(金额) as 金额
    from tablename
    where 时间 between '2000-1-1' and '2003-1-1'
    group by 姓名,项目
      

  6.   

    select 姓名,sum(case when 项目='项目1' then 金额 end) as 项目1,
    sum(case when 项目='项目2' then 金额 end) as 项目2,....
    from tablename
    where 时间 between '2000-1-1' and '2003-1-1'
    group by 姓名
      

  7.   

    一个问题是项目是不定的,另一个显示出来一个姓名有多行记录出现我看是否能够把
    select 姓名,项目,sum(金额) as 金额
    from tablename
    where 时间 between '2000-1-1' and '2003-1-1'
    group by 姓名,项目的结果生成一个表或者视图,然后从其中再生成需要的格式的数据
      

  8.   

    select 姓名,sum(case when 项目='项目1' then 金额 end) as 项目1,
    sum(case when 项目='项目2' then 金额 end) as 项目2,....
    from tablename
    where 时间 between '2000-1-1' and '2003-1-1'
    group by 姓名项目可以确定为3项,但是查询出的结果同一姓名有多行记录,类似以下
    姓名 项目1 项目2 项目3
    aaa  1000   null  null
    aaa  null   4000  null
    bbb  1000   null  null
    bbb  null   4000  null
    怎样把同一姓名的记录统一到一行?大家帮忙
      

  9.   

    看一看http://www.csdn.net/expert/topic/508/508081.xml?temp=.723797
    应该对你有帮助。
      

  10.   

    create table test(id int identity(1,1),name char(3),project char(2),value int,time datetime)insert into test(name,project,value,time)
                values('aaa','p1',2000,'2002-3-18')
    insert into test(name,project,value,time)
                values('aaa','p1',2000,'2002-3-18')
    insert into test(name,project,value,time)
                values('aaa','p2',1000,'2002-3-18')
    insert into test(name,project,value,time)
                values('bbb','p3',1050,'2002-3-19')
    insert into test(name,project,value,time)
                values('bbb','p1',2050,'2002-3-18')
    insert into test(name,project,value,time)
                values('bbb','p1',2500,'2002-3-20')select name,sum(case when project='p1' then value end) as p1,
    sum(case when project='p2' then value end) as p2,
    sum(case when project='p3' then value end) as p3
    from test
    where time between '2000-1-1' and '2003-1-1'
    group by name
      

  11.   

    结果是:
    name p1  p2       p3
    aaa 4000 1000 NULL
    bbb 4550 NULL 1050
      

  12.   

    多谢,多谢!
    问题在于我是 group by 姓名,项目,应该是group by name
    呵呵,解决问题,多谢warning和其他参与的网友!
      

  13.   

    CSDN的给分机制看来有问题,有好几个问题都给分了,结果出来是O分,请
    warning(爱就爱了) 到另一个问题拿分吧