fund表
ID     accountid      支出资金      收入资金
1          10            99.1         null
2          11            99.1         null
3          12            null          80fund表
accountid      accountnum 
10              中国银行         
11              人民银行     
12                现金 
...                ...显示的效果
ID     accountid       支出资金      收入资金    中国银行     人民银行      现金     ......                      
 1       10             99.1         null       1               0          0       ......                                  
 2       11             99.1         null       0               1           0      ......  
 3       12             null          80        0               0          1       ......
....  有2个表,一个是 account, fund,  accountid是关联的/
我要显示的效果是   accountnum 字段横这排。 
现金,中国银行,其他 等等。 下面的 0和1 的意思就是   fund.accountid  =account.accountid 就显示问1, 不等于就显示为 0。

解决方案 »

  1.   

    declare @sql varchar(8000)
    set @sql=''select @sql=@sql+',['+accountnum+']=(case accountid when '+rtrim(accountid)+' then 1 else 0 end)'
    from fund表 order by accountidset @sql='select *'+@sql+' from fund表'exec(@sql)
      

  2.   

    select A.*,
    case when B.accountnum='中国银行' then 1 else 0 end as [中国银行],
    case when B.accountnum='人民银行' then 1 else 0 end as [人民银行],
    case when B.accountnum='现金' then 1 else 0 end as [现金],
    .....
    from account A
    left join fund B
    on A.accountid=B.accountid
      

  3.   

    select a.id,a.accountid ,a.支出资金,a.收入资金,
    (case when b.accountnum='中国银行' then 1 else 0 end ) as 中国银行,
    (case when b.accountnum='人民银行' then 1 else 0 end ) as 人民银行,
    (case when b.accountnum=' 现金' then 1 else 0 end ) as  现金
    from  fund a inner join account b on
    a.accountid=b.accountid
    这可是邹老大教我的啊
      

  4.   

    create table account(accountID int,Bank varchar(10))
    insert into account
    select 10, '中国银行' union all
    select 11,'人民银行'  union all
    select 12,'现金' Declare @SQL varchar(8000)
    select @SQL=''
    select @SQL=@SQL+','+quotename(Bank)+'=(case account.AccountID when '+cast(AccountID as varchar(10))+' then ''√'' else ''×'' end )'
    from Account
    order by AccountID desc
    exec('select fund.* '+@sql+' from account ,fund where account.accountid=fund.accountid')drop table fund
    drop table account/结果1 10 99.10 NULL × × √
    2 11 99.10 NULL × √ ×
    3 12 NULL 80.00 √ × ×
      

  5.   

    declare @account table(ID int,accountid int,支出资金  money,收入资金 money)
    insert into @account
    select 1,10,99.1,null
    union all
    select 2,11,99.1,null
    union all
    select 3,12,null,80declare @fund table(accountid int,accountnum nvarchar(50))
    insert into @fund
    select 10,N'中国银行'
    union all
    select 11,N'人民银行'
    union all
    select 12,N'现金'
    select * from @account
    select * from @fund
    declare @s varchar(8000)
    set @s=''
    select @s=@s+',(case when accountid='+cast(accountid as varchar)+' then 1 else 0 end) as '+accountnum
    from @fund order by accountid
    set @s='Select accountid,支出资金,收入资金'+@s+
    ' from @account'
    exec(@s)