我的表是这样的:工资项目    简写    金额    月分房补         fb     100     201101
保险         bx     200     201101
房补         fb     100     201102
保险         bx     200     201102每月的项目都是一样的,当然不只两条哈,sql语句要求查询出来的结果是这样的月份         房补       保险
201101       100         200
201102       100         200请高手帮帮忙,谢过!

解决方案 »

  1.   

    我的表是这样的:create table tableA
    (工资项目 varchar(20), 简写 varchar(20), 金额  int,月分 varchar(10)
    )
    insert into tableA values('房补', 'fb', 100, '201101')
    insert into tableA values('保险', 'bx', 200, '201101')
    insert into tableA values('房补', 'fb', 100, '201102')
    insert into tableA values('保险', 'bx', 200, '201102')
    declare @sql varchar(max)
    set @sql='select 月分'
    select @sql=@sql+',sum(case 工资项目 when '''+工资项目+''' then 金额 end) as ['+工资项目+']'
    from(select distinct 工资项目 from tableA)a
    SET @SQL=@SQL+' FROM TABLEA GROUP BY 月分'
    print @sql
    exec (@sql)
      

  2.   

    select 月分,sum(case 工资项目 when '保险' then 金额 end) as [保险],sum(case 工资项目 when '房补' then 金额 end) as [房补] FROM TABLEA GROUP BY 月分
    非动态sql就是这个
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2012-05-24 15:12:10
    -- Version:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([工资项目] varchar(4),[简写] varchar(2),[金额] int,[月分] int)
    insert [tb]
    select '房补','fb',100,201101 union all
    select '保险','bx',200,201101 union all
    select '房补','fb',100,201102 union all
    select '保险','bx',200,201102
    --------------开始查询--------------------------
    declare @sql varchar(8000)
    set @sql = 'select 月分 '
    select @sql = @sql + ' , max(case 工资项目 when ''' + 工资项目 + ''' then 金额 else 0 end) [' + 工资项目 + ']'
    from (select distinct 工资项目 from tb) as a
    set @sql = @sql + ' from tb group by 月分'
    exec(@sql) ----------------结果----------------------------
    /* 月分          保险          房补
    ----------- ----------- -----------
    201101      200         100
    201102      200         100(2 行受影响)*/
      

  4.   


    select 月分,房补,保险
    from tba
    pivot (max(金额) for 工资项目 in([房补],[保险])) as d 
      

  5.   


    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    create table [test]([工资项目] varchar(4),[简写] varchar(2),[金额] int,[月分] int)
    insert [test]
    select '房补','fb',100,201101 union all
    select '保险','bx',200,201101 union all
    select '房补','fb',100,201102 union all
    select '保险','bx',200,201102declare @str varchar(2000)
    set @str=''
    select @str=@str+','+[工资项目]+'=max(case when [工资项目]='
           +QUOTENAME([工资项目],'''')+' then [金额] else 0 end)'
    from [test] group by [工资项目]
    exec('select [月分]'+@str+' from test group by [月分]')/*
    月分 保险 房补
    201101 200 100
    201102 200 100
    */
      --动态的吧,假如你的项目太多了,还需要打那么多字
      

  6.   

    只能用过程来完成的吗?能不能用一条sql语句完成啊?申明一下,有哪些工资项目是不知的,只知道每月项目都一样。
      

  7.   


    未知的那就只能我这个了,我把他给你封装成存储过程,你直接调用,跟SQL语句一样,只是遍历方式和调用方式不一样
      

  8.   


    --> 测试数据:[test]
    if object_id('[test]') is not null 
    drop table [test]
    go
    create table [test](
    [工资项目] varchar(4),
    [简写] varchar(2),
    [金额] int,
    [月分] int
    )
    go
    insert [test]
    select '房补','fb',100,201101 union all
    select '保险','bx',200,201101 union all
    select '房补','fb',100,201102 union all
    select '保险','bx',200,201102
    goif OBJECT_ID('pro_test')is not null
    drop proc pro_test
    go
    create proc pro_test
    as
    declare @str varchar(2000)
    set @str=''
    select 
         @str=@str+','+[工资项目]+
         '=max(case when [工资项目]='
         +QUOTENAME([工资项目],'''')+
         ' then [金额] else 0 end)'
    from 
         [test] 
    group by 
         [工资项目]
    exec('select [月分]'+@str+' from test group by [月分]')
    go--执行存储过程pro_test:
    exec pro_test
    go
    --结果集:
    /*
    月分 保险 房补
    ----------------------
    201101 200 100
    201102 200 100
    */
      

  9.   

    select n.month,n.money as '房补',ff.money as '保险' from (select * from fb f where name = '房补') n left join fb ff on ff.month=n.month and ff.ename = 'bx'