我有三个表分别为,A、B、C
A:(车的基本参数)
Id      bchao     
1        1号
2        2号
3        3号
4        4号B表:(这辆车每天的收入情况,流水账记录)
Id               bchao                  sr                   sj
1                 1号                   50                2011-07-25
2                 2号                   40                2011-07-21
3                 1号                   36                2011-08-09
4                 2号                   80                2011-08-11
5                 2号                   60                2011-08-19
C表:(记录这些车每月加油的情况)
Id               bchao                  jy                   sj
1                 1号                   50                2011-07-08
2                 3号                   100               2011-07-12
3                 1号                   20                2011-08-09
4                 2号                   80                2011-08-09
现在我想得到这些车一月的投入和产出情况,这个语句应该怎么写?
希望得到结果为:
Id               bchao                  sr          jy         sj
1                1号                    50          50        2011-07
2                2号                    40           0        2011-07
3                3号                    0           100       2011-07
4                4号                    0            0        2011-07
5                1号                    36           20       2011-08
6                2号                    140          80       2011-08
7                3号                     0           0        2011-08
8                4号                     0           0        2011-08

解决方案 »

  1.   

    晕,显示好像有问题,重发一次!
    我有三个表分别为,A、B、C
    A:(车的基本参数)
    Id      bchao     
    1        1号
    2        2号
    3        3号
    4        4号B表:(这辆车每天的收入情况,流水账记录)
    Id               bchao                sr                   sj
    1                 1号                   50                2011-07-25
    2                 2号                   40                2011-07-21
    3                 1号                   36                2011-08-09
    4                 2号                   80                2011-08-11
    5                 2号                   60                2011-08-19
    C表:(记录这些车每月加油的情况)
    Id               bchao                jy                   sj
    1                 1号                   50                2011-07-08
    2                 3号                   100               2011-07-12
    3                 1号                   20                2011-08-09
    4                 2号                   80                2011-08-09
    现在我想得到这些车一月的投入和产出情况,这个语句应该怎么写?
    希望得到结果为:
    Id               bchao               sr           jy         sj
    1                1号                    50           50       2011-07
    2                2号                    40           0        2011-07
    3                3号                    0            100      2011-07
    4                4号                    0            0        2011-07
    5                1号                    36           20       2011-08
    6                2号                    140          80       2011-08
    7                3号                     0           0        2011-08
    8                4号                     0           0        2011-08
      

  2.   


    ;with ach as
    (
        select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy 
        from b
        group by convert(varchar(7),sj,120),bchao
        union all
        select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy 
        from c
        group by convert(varchar(7),sj,120),bchao
    )select a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
    from (select distinct a.bchao,b.date from a cross join ach b) a
      left join (select date,bchao,sum(sr) sr,sum(jy) jy from ach group by date,bchao) b
      on a.bchao = b.bchao and a.date = b.date
      

  3.   


    ;with ach as
    (
        select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy 
        from b
        group by convert(varchar(7),sj,120),bchao
        union all
        select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy 
        from c
        group by convert(varchar(7),sj,120),bchao
    )
    --这里是将两表按年月及车牌号进行合并,其中收入占一列,加油的占另一列,方便统计
    --就是上访的sum() as ..,0 as ..  等select a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
    from (select distinct a.bchao,b.date from a cross join ach b) a
      -- 这个a表是为了得到所有车牌号所有年月的一个笛卡尔积,就是全排列,把参与统计的年月和车牌号全排列形成一个对照表
      left join (select date,bchao,sum(sr) sr,sum(jy) jy from ach group by date,bchao) b
      --别名b表对年月和车牌分组,将收入和加油的费用统计后去和对照表a进行左连接得到特定年月及车牌号的信息
      on a.bchao = b.bchao and a.date = b.date
      

  4.   

    恩,谢谢你的解释,我用你这代码调试了,但是提示with附近有语法错误!
      

  5.   

    select distinct a.bchao,b.date from a cross join ach b中的cross join ach b什么意思?
      

  6.   


    cross join 交叉连接!楼主可以看看join的相关内容!
    select date,bchao,sr,jy
      into #tb
    from
    (
        select convert(varchar(7),sj,120) date,bchao,sum(sr) as sr,0 as jy 
        from b
        group by convert(varchar(7),sj,120),bchao
        union all
        select convert(varchar(7),sj,120) date,bchao,0 as sr,sum(jy) as jy 
        from c
        group by convert(varchar(7),sj,120),bchao
    ) tselect a.bchao,a.date,isnull(b.sr,0) sr,isnull(b.jy,0) jy
    from (select distinct a.bchao,b.date from a cross join #tb b) a
      left join (select date,bchao,sum(sr) sr,sum(jy) jy from #tb group by date,bchao) b
      on a.bchao = b.bchao and a.date = b.datedrop table #tb
      

  7.   

    调试结果:sum or average aggregate 运算不能以 char 数据类型作为参数