从一个表中求和
表结构如下,,id,type,point,staff_id,checkdate
1,'公共内容',5,'0000','2007-1-1'
2,'业务内容',4,'0000','2007-1-1'
3,'业务内容',2,'0000','2007-1-2'
4,'公共科目',2,'0000','2007-1-3'求得结果如下staff_id,公共内容,业务内容,公共科目,业务科目,领导加分
'0000',5,6,2,0,0
得有条件 checkdate >=2007-1-1 <2007-1-7除了用full outer join 还有什么列简单的语句不,,不能用游标和临时表

解决方案 »

  1.   

    不知道可不可以。反正我用一条SQL是做不到。
      

  2.   

    select staff_id,case type when '公共内容' then sum(point) else 0 end ,case type when '业务内容' then sum(point) esle 0 end ......
    from table
    group by staff_id
      

  3.   

    用嵌套Some Code To Sum where id in (select id form XX表 where checkdate >=2007-1-1 <2007-1-7)其实还是临时表,不过这张表是记录在内存里的凑合着吧
      

  4.   

    --如果“公共内容,业务内容,公共科目,业务科目,领导加分”這幾列是固定的,可以只用一條SQL語句,否則就要用動態SQL語句Select 
    staff_id,
    SUM(Case type When N'公共内容' Then point Else 0 End) As 公共内容,
    SUM(Case type When N'业务内容' Then point Else 0 End) As 业务内容,
    SUM(Case type When N'公共科目' Then point Else 0 End) As 公共科目,
    SUM(Case type When N'业务科目' Then point Else 0 End) As 业务科目,
    SUM(Case type When N'领导加分' Then point Else 0 End) As 领导加分
    From
    TableName
    Where checkdate >= '2007-1-1' And  checkdate < '2007-1-7'
    Group By staff_id

      

  5.   

    create table TA (id int ,type char(10),point int ,staff_id char(5) ,checkdate datetime )--插入测试数据insert into TA (id,type,point,staff_id,checkdate)
    select 1,'公共内容',5,'0000','2007-1-1' union 
    select 2,'业务内容',4,'0000','2007-1-1' union 
    select 3,'业务内容',2,'0000','2007-1-2' union 
    select 4,'公共科目',2,'0000','2007-1-3' union
    select 5,'业务科目',0,'0000','2007-1-2' union 
    select 6,'领导加分',0,'0000','2007-1-3' 
    declare @str varchar(8000)
    set @str='select staff_id ,'
    select @str=@str+quotename(rtrim(type))+'=sum(case when type='''+rtrim(type)+''' then point else 0 end),'
    from ta group by type order by type
    select @str=left(@str, len(@str)-1) ,@str=@str+' from tA where  checkdate >='+'''2007-1-1 ''' +'and checkdate<'+'''2007-1-7'''+ 'group by staff_id order by staff_id '
    exec(@str)不知道是否是你要的结果?