有表owner和表money表owner
xm    bh  xsrq
张三  1   2004-1-1
李四  2   2004-2-1
王五  3   2004-3-1表money
fzbh kxmc  fs    je     日期
1    轿车  现金  10000  2004-1-1
1    卡车  现金  20000  2004-2-1
2    货车  按揭  30000  2004-3-1
2    货车  欠款  40000  2004-4-13    火车  现金  50000  2004-5-1
3    飞机  按揭  60000  2004-6-1
3    卡车  欠款  70000  2004-7-1要得到下表,该如何写SQL
2004-01-01 到 2004-04-01的数据xm    bh  轿车  卡车  货车  火车  飞机  现金  按揭  欠款
张三  1   10000 20000                   30000
李四  2               70000                   30000 40000
合计      10000 20000 70000             30000 30000 40000

解决方案 »

  1.   

    如果你的“kxmc”字段是已知有限的,可以用一个sql搞定,如果不是的话,只能用存储过程了。建议这样的问题,到sql server版块去问,那里有很多高手。
      

  2.   

    回:回复人: comerliang(天地良心) 
    kxmc是已知有限的,那要怎么写
      

  3.   

    一个交叉表的例子,if object_id('corsstab') is not null
    drop proc corsstab
    go
    create procedure corsstab 
    @strtabname as varchar(50) = 't1', --此处放表名
    @strcol as varchar(50) = 'dq',     --表头分组依据字段
    @strgroup as varchar(50) = 'cpxh',--分组字段
    @strnumber as varchar(50) = 'sl',    --被统计的字段
    @strsum as varchar(10) = 'sum'                     --运算方式
    asdeclare @strsql as varchar(1000), @strtmpcol as varchar(100)
    execute ('declare corss_cursor cursor for select distinct ' + @strcol + ' from ' + @strtabname + ' for read only ') --生成游标
    begin
      set nocount on 
      set @strsql ='select ' + @strgroup + ', ' + @strsum + '(' + @strnumber + ') as [' + @strsum + ' of ' + @strnumber + ']' --查询的前半段  open corss_cursor
      while (0=0)
      begin
        fetch next from corss_cursor --遍历游标,将列头信息放入变量@strtmpcol
        into @strtmpcol
        if (@@fetch_status<>0) break
              set @strsql = @strsql + ', ' + @strsum + '(case ' + @strcol + ' when ''' + @strtmpcol + ''' then ' + @strnumber + ' else null end) as [' + @strtmpcol + ' ' + @strcol + ']' --构造查询
      end
            set @strsql = @strsql + ' from ' + @strtabname + ' group by ' + @strgroup --查询结尾  exec(@strsql) --执行 
      if @@error <>0 return @@error --如果出错,返回错误代码
      close corss_cursor 
      deallocate corss_cursor return 0 --释放游标,返回0表示成功end
    goexec  corsstab
      

  4.   

    这类表是多维表,我的很多客户也有这个需求,一般对于事实表很容易出,横纵向都无限大的多维表很难出。你可以把这种客户需求分解成几个表,或者出一个事实表然后让他用excel的透视表转换成多维表,不过最好的方法还是在TStringGrid中写一个透视表转换功能,我现在扣。
      

  5.   

    kxmc是已知有限的,那要怎么写?到sql版块search一下,好多类似的帖子呢。
      

  6.   

    --environment in SQL Server 2000
    use pubs
    create table owner
    (
    xm varchar(10),
    bh int,
    xsrq datetime
    )insert into owner
    select '张三' , 1 ,  '2004-1-1'
    union all
    select '李四' , 2  , '2004-2-1'
    union all
    select '王五' , 3 ,  '2004-3-1'create table money
    (
    fzbh int,
    kxmc varchar(10),
    fs varchar(10),
    je int,
    date datetime
    )
    insert into money
    select 1 ,   '轿车' ,'现金' , 10000 , '2004-1-1'
    union all
    select 1 ,   '卡车' , '现金' , 20000 , '2004-2-1'
    union all
    select 2 ,   '货车',  '按揭'  ,30000 , '2004-3-1'
    union all
    select 2 ,   '货车' , '欠款' , 40000 , '2004-4-1'
    union all
    select 3 ,   '火车' , '现金' , 50000 , '2004-5-1'
    union all
    select 3 ,   '飞机' , '按揭' , 60000 , '2004-6-1'
    union all
    select 3 ,   '卡车' , '欠款',  70000 , '2004-7-1'select * from money
    select * from owner--drop table money
    --drop table owner
      

  7.   

    select fzbh,sum(case kxmc when '轿车' then je else 0 end)as '轿车',
           sum(case kxmc when '卡车' then je else 0 end)as '卡车',
           sum(case kxmc when '货车' then je else 0 end)as '货车',
           sum(case kxmc when '火车' then je else 0 end)as '火车',
           sum(case kxmc when '飞机' then je else 0 end)as '飞机',
           sum(case fs when '现金' then je else 0 end)as '现金',
           sum(case fs when '按揭' then je else 0 end)as '按揭',
           sum(case fs when '欠款' then je else 0 end)as '欠款'
    from money 
    where date between '2004-01-01' and '2004-04-01' 
    group by fzbh 
    --------------------------
    result:
    fzbh        轿车          卡车          货车          火车          飞机          现金          按揭          欠款          
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    1           10000       20000       0           0           0           30000       0           0
    2           0           0           70000       0           0           0           30000       40000(所影响的行数为 2 行)
    --其余的你自己改改, 心情不太好