如总部数据库:ZBDATA
下面有三个下属分部:XSDATA1、XSDATA2、XSDATA3
他们中数据表的数据结构完全一样,现把他们放在同一台服务器中。
他们中有一个数据月份收入汇总表YFHZB(yf, srfl, zsr)-- 月份、收入分类、总收入要求:
每个月想做一份报表给领导看,报表格式:
       总部   分部1    分部2   分部3
分类1  收入   收入     收入    收入
分类2 ...
分类3
分类4
分类5
...
即从四个结构完全相同的数据库(ZBDATA,XSDATA1,XSDATA2,XSDATA3)中的YFHZB中提取出数据,然后放在一起显示出来给领导看!

解决方案 »

  1.   

    declare @month int
    select t0.zsr as 总部,t1.zsr as 分部1
           t2.zsr as 分部2,t3.zsr as 分部3
    from t0 join t1 on t1.srfl=t0.srfl
    join t2 on t2.srfl=t0.srfl
    join t3 on t3.srfl=t0.srfl
    where t0.yf=@month
    and   t1.yf=t0.yf
    and   t2.yf=t0.yf
    and   t3.yf=t0.yf
      

  2.   

    TO: filebat(Mark)
    你的方法应该不合我的要求,一方法你没有分类,我要的有分类,别一方面,你连接的不同的数据库也有问题,应该用zbdata..yfhzb之类才行
      

  3.   

    to hqh_soft:
    你自己已经分好类了, 那个代码应该没有问题(有的话,也只是从内连接改成外连接)对于跨数据库的, 你自己用sp_addlinkedserver连接一下就可以了.再有问题的话,你就要贴数据和表结构出来了.
      

  4.   

    select 
    from ZBDATA..YFHZB zb,XSDATA1..YFHZB  xs1,
        XSDATA2..YFHZB xs2,XSDATA3..YFHZB  xs3
    where zb.srfl=xs1.srfl
    and zb.srfl=xs2.srfl
    and zb.srfl=xs3.srfl
    and zb.yf=200507
    and xs1.yf=200507
    and xs2.yf=200507
    and xs3.yf=200507
      

  5.   

    跟filebat(Mark) 差不多,
    只是感觉在表里下条件比用连接效率高一些。
      

  6.   

    在1楼中加上 group by 分组
    也就是:
    select sum() as 总部,sum() as 分部1,sum() as 分部2,sum() as 分部3
    from ……
    on……//见上楼
    group by (你的分类)
      

  7.   

    to: bugchen888(臭虫) 
    注意:下属分部有可能某一月份没有数据,
    如分部3是从6月份用的系统,6月份以后才有数据,如果我想查5月份的数据,则全部查询不出来了
      

  8.   

    好像是一个标准的行转列的问题。像是oracle中的矩阵报表
      

  9.   

    是不是在多个数据库中不是关键吧。
    假设是在同一数据库上的三个表要汇总:
    select 'zbdata' Org,* from zbdata
    union select 'xsdata1' Org,* from xsdata1
    union select 'xsdata2' Org,* from xsdata2
    select yf,srfl,
    max(case Org when 'zbdata'  then zsr end) [zbdata],
    max(case Org when 'xsdata1'  then zsr end) [xsdata1],
    max(case Org when 'xsdata2' then zsr end) [xsdata2] 
      from (select 'zbdata' Org,* from zbdata
    union select 'xsdata1' Org,* from xsdata1
    union select 'xsdata2' Org,* from xsdata2) hz 
     group by yf,srfl
    having yf = 'Jan'select yf,srfl,
    max(case Org when 'zbdata'  then zsr end) [zbdata],
    max(case Org when 'xsdata1'  then zsr end) [xsdata1],
    max(case Org when 'xsdata2' then zsr end) [xsdata2] 
      from (select 'zbdata' Org,* from zbdata
    union select 'xsdata1' Org,* from xsdata1
    union select 'xsdata2' Org,* from xsdata2) hz 
     group by yf,srfl
    having yf = 'Feb'select yf,srfl,
    max(case Org when 'zbdata'  then zsr end) [zbdata],
    max(case Org when 'xsdata1'  then zsr end) [xsdata1],
    max(case Org when 'xsdata2' then zsr end) [xsdata2] 
      from (select 'zbdata' Org,* from zbdata
    union select 'xsdata1' Org,* from xsdata1
    union select 'xsdata2' Org,* from xsdata2) hz 
     group by yf,srfl
    having yf = 'Mar'/**************************************************************
    下面是执行的结果Org     yf   zsr         srfl  
    ------- ---- ----------- ----- 
    xsdata1 Feb  170         type2
    xsdata1 Jan  150         type1
    xsdata1 Mar  160         type1
    xsdata1 Mar  180         type2
    xsdata2 Feb  280         type1
    xsdata2 Jan  200         type3
    xsdata2 Jan  260         type2
    xsdata2 Mar  250         type1
    xsdata2 Mar  270         type2
    zbdata  Feb  60          type1
    zbdata  Feb  80          type2
    zbdata  Jan  50          type1
    zbdata  Jan  70          type2yf   srfl  zbdata      xsdata1     xsdata2     
    ---- ----- ----------- ----------- ----------- 
    Jan  type1 50          150         NULL
    Jan  type2 70          NULL        260
    Jan  type3 NULL        NULL        200警告: 聚合或其它 SET 操作消除了空值。
    yf   srfl  zbdata      xsdata1     xsdata2     
    ---- ----- ----------- ----------- ----------- 
    Feb  type1 60          NULL        280
    Feb  type2 80          170         NULL警告: 聚合或其它 SET 操作消除了空值。
    yf   srfl  zbdata      xsdata1     xsdata2     
    ---- ----- ----------- ----------- ----------- 
    Mar  type1 NULL        160         250
    Mar  type2 NULL        180         270警告: 聚合或其它 SET 操作消除了空值。
    **************************************************************/如果你的四个数据库是在同一个服务器上:
    稍微改一下..YFHZB不就行了:
    select yf,srfl,
    max(case Org when 'zbdata'  then zsr end) [zbdata],
    max(case Org when 'xsdata1'  then zsr end) [xsdata1],
    max(case Org when 'xsdata2' then zsr end) [xsdata2] 
      from (select 'zbdata' Org,* from zbdata..YFHZB
    union select 'xsdata1' Org,* from xsdata1..YFHZB
    union select 'xsdata2' Org,* from xsdata2..YFHZB) hz 
     group by yf,srfl
    having yf = 'Jan'
      

  10.   

    关于行列转换
    http://dev.csdn.net/article/67/67161.shtm
      

  11.   

    to: QQ454831(神仙) 
    首先谢谢你!
    行列转换用交叉表可以实现,这个我知道!
      

  12.   

    呵呵你的要求又变成了?不知我上面的是否符合要求啊新要求要比较。同时列出不就比较了?加个or条件不就完了
    select yf,srfl,
    max(case Org when 'zbdata'  then zsr end) [zbdata],
    max(case Org when 'xsdata1'  then zsr end) [xsdata1],
    max(case Org when 'xsdata2' then zsr end) [xsdata2] 
      from (select 'zbdata' Org,* from zbdata
    union select 'xsdata1' Org,* from xsdata1
    union select 'xsdata2' Org,* from xsdata2) hz 
     group by yf, srfl
    having yf = 'Feb' 
        or yf = 'Jan'
     order by srfl/**************************************************
    结果:yf   srfl  zbdata      xsdata1     xsdata2     
    ---- ----- ----------- ----------- ----------- 
    Feb  type1 60          NULL        280
    Jan  type1 50          150         NULL
    Feb  type2 80          170         NULL
    Jan  type2 70          NULL        260
    Jan  type3 NULL        NULL        200警告: 聚合或其它 SET 操作消除了空值。
    **************************************************/
      

  13.   

    解决办法:
    建一个数据表存放需统计的各数据库信息,如
    数据库名 显示名称
    ZBDATA   总部
    XSDATA1   第一分部
    XSDATA2   第一分部
    XSDATA3   第一分部
    XSDATA4   第一分部
    先分组统计,再用交叉分组方式显示出来