01 1 1GBS130#   .00 NULL .00 .00
01 1 1GBS170#   .00 NULL .00 .00
01 1 1GBS200#   .00 NULL .00 .00
01 1 1GCA010#   .00 NULL .00 .00
02 1 1AAA010    18.00 NULL 16.00 124.00
NULL NULL NULL NULL NULL 1.00 NULL
NULL NULL NULL NULL NULL 7.00 NULL
~~~~     ~~~~     ~~~
注意这几个值,就是这里搞不定。

解决方案 »

  1.   

    select isnull(a.departmentid,c.departmentid),isnull(a.commoditytypeid,c.commoditytypeid),isnull(a.commodityid,c.commodityid),a.total_in_amount,a.total_out_amount,c.fresidualamount,a.total_in_money,a.total_out_money,c.fresidualmoney from #temp_sum a full outer join stock_finale c on a.departmentid=c.cdepartmentid and a.commoditytypeid=c.ccommoditytypeid and a.commodityid=c.ccommodityid
      

  2.   

    select a.departmentid,a.commoditytypeid,a.commodityid,a.total_in_amount,b.total_out_amount,c.fresidualamount,a.total_in_money,b.total_out_money,c.fresidualmoney from #in_stock a (full outer join #out_stock b on a.departmentid=b.departmentid and a.commoditytypeid=b.commoditytypeid and a.commodityid=b.commodityid) full outer join stock_finale on a.departmentid=c.departmentid and a.commoditytypeid=c.commoditytypeid and a.commodityid=c.commodityid务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'full' 附近有语法错误。
    full outer join是不是不能连续连接两个以上的表?
      

  3.   

    语法不对吧
    select a.b,b.t,c.x from test_aa a  full join test b on b.a=a.a full join test_ab c on c.a=a.a[ FROM { < table_source > } [ ,...n ] ] < table_source > ::=
        table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [ ,...n ] ) ]
        | view_name [ [ AS ] table_alias ] [ WITH ( < view_hint > [ ,...n ] ) ]
        | rowset_function [ [ AS ] table_alias ]
        | user_defined_function [ [ AS ] table_alias ]
        | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ]
        | < joined_table >< joined_table > ::=
        < table_source > < join_type > < table_source > ON < search_condition >
        | < table_source > CROSS JOIN < table_source >
        | [ ( ] < joined_table > [ ) ]< join_type > ::=
        [ INNER | { { LEFT | RIGHT | FULL } [OUTER] } ]
        [ < join_hint > 
      

  4.   

    我这里是outer连接,我的形式上跟你说的一样啊
      

  5.   

    用个判断语句,如果是空值则将它赋给""
    isnull(记录集,"")否则的话,空值&非空值,它会给你空值的答案,应该是这个问题
      

  6.   

    select a.departmentid,a.commoditytypeid,a.commodityid,a.total_in_amount,b.total_out_amount,c.fresidualamount,a.total_in_money,b.total_out_money,c.fresidualmoney from #in_stock a full outer join #out_stock b on a.departmentid=b.departmentid and a.commoditytypeid=b.commoditytypeid and a.commodityid=b.commodityid full outer join stock_finale on a.departmentid=c.departmentid and a.commoditytypeid=c.commoditytypeid and a.commodityid=c.commodityid
      

  7.   

    多谢两位!
    不好意思,还有一个概念性的问题帮忙说一下,我使用sum函数和group by来累加字段值,是否当group by 的时候会把数据重复累加?因为要做一个把多个表汇总成一个表的工作,没有经验,不好意思要多问几个问题。
      select b.cdepartmentid as departmentid,b.ccommoditytypeid as commoditytypeid,a.ccommodityid as commodityid,a.fsaleamount as total_out_amount,a.fsalemoney as total_out_money 
     -- into #temp_out_stock
      from sale_detail a 
      left join operation_head b on a.cno=b.cno and b.coperationtypeid='2'
     select b.cdepartmentid as departmentid,b.ccommoditytypeid as commoditytypeid,a.ccommodityid as commodityid,a.fmoveamount as total_out_amount,a.fmovemoney as total_out_money
      from move_detail a 
      left join operation_head b on a.cno=b.cno and b.coperationtypeid='3'
     select b.cdepartmentid as departmentid,b.cincommoditytypeid as commoditytypeid,a.ccommodityid as commodityid,a.fdaamount as total_out_amount,a.fdamoney as total_out_money 
      from damaged_detail a 
      left join operation_head b on a.cno=b.cno and b.coperationtypeid='6'
     -- select * from #out_stock  select departmentid,commoditytypeid,commodityid,sum(total_out_amount) as total_out_amount,sum(total_out_money) as total_out_money
      from #temp_out_stock
    --我把上面三个查询的结果放到了#temp_out_stock里面
    --但使用这个sum,和group by汇总的结果似乎把原来的数据重复累加了。
      group by departmentid,commoditytypeid,commodityid
    查询1的结果
    01 1 1AAA010    10.00 .00
    01 1 1AAA040    12.00 225.64
    2的结果:
    02 1 1AAA010    2.00 4.00
    02 1 1AAA010    3.00 6.00
    02 1 1AAA010    4.00 52.00
    02 1 1AAA010    3.00 39.00
    02 1 1AAA020    2.00 4.00
    02 1 1AAA030    1.00 6.00
    02 1 1AAA010    4.00 61.00
    02 1 1AAA020    5.00 16.00
    3。的结果
    NULL NULL 1AAA010    2.00 .00
    NULL NULL 1AAA020    4.00 .00
    NULL NULL 1AAA030    1.00 .00
    汇总的结果:
    NULL NULL 1AAA010    2.00 .00
    NULL NULL 1AAA020    4.00 .00
    NULL NULL 1AAA030    1.00 .00
    01 1 1AAA010    20.00 .00  (1)
    01 1 1AAA040    24.00 451.28(2)
    (1),(2)两处的结果好像是上面的结果重复累加了。
    拜托了!
      

  8.   

    不知道你想做什么,你的合并是什么意思?为什么不用union??
      

  9.   

    是这样的,我有很多表,基本上最后要合并的是三个形式相似的表,就是一个是表示入库,一个,出库,一个库存的,它们有相同的字段departmentid(部门),commoditytypeid(商品类别),commodityid(商品), 我现在想把这三个表合并到一起,就是如果是同一deparmentid,commoditytypeid,commodityid的就形成一个记录,其他字段为这三个表的其他字段,当某个表没有数值时就用0代替,就是要把三个所有记录资料合并在一个表里面。但我用group by 和sum的时候发现数据好像会重复累加 .
    能提供点别的思路也好,我写了三天sql来实现也觉得不行。
    谢谢。
      

  10.   

    如你所述,我觉得应该用union
    union的语义很简单,就是取两个结果集的合集
      

  11.   

    select deparmentid,commoditytypeid,commodityid from table1
    union
    select deparmentid,commoditytypeid,commodityid from table2
    union
    select deparmentid,commoditytypeid,commodityid from table3还缺什么,你看看再回复
      

  12.   

    我举个例子吧,呵呵,真麻烦你了。
    table a:
    departmentid   commoditytypeid  commodityid in_amount in_money
    01               1               0001         22       200
    01               1               0002          10      100
    02               1               0001         10        100
    talbe b:
    departmentid  commoditytypeid  commodityid   out_amount out_money
    01              1                   0001       20        300
    01               1                  0002       10        120
    table c:
    departmentid  commoditytypeid  commodityid   stock_amount tock_money
    01                1             0001              30         400
    02                1             0002           40           400
    01                0             0001       10   200   现在想合并为这样的表:
    departmentid commoditytypeid commodityid in_amount out_amount stock_amount in_money out_money stock_money
    0001     1       0001   20   20 30 200 300 400
    ..........
    所有在这三个表中的记录都要在总表里面显示,如果别的表没有相应记录则用0代替。
      

  13.   

    呵呵,这就只能用 join了,但是你为什么要用sum,group by 不用不就可以了吗?
      

  14.   

    我用group by和sum的目的是把相同的资料合并,比如:
    departmentid,commoditytypeid,commodityid相同的能合并的就合并。在形成这三个表之前我是用sum和group把一些资料合并的,我后面再用sum,group by是为了防止重复记录存在,也就是相同的要求合并起来。sum是因为统计数量累加和。
    我回去再好好想想,多谢你帮忙,有什么建议请帮我提提,我第一次处理我认为是很复杂的统计数据,有点莫不着门,呵呵,怕数据重复累加,那就失去意义了。
      

  15.   

    先对每个表用sum group,最后再用join
      

  16.   

    out join 多表如何写?
    一:from 表1 LEFT OUTER JOIN 表2,表3
    二:from 表1 LEFT OUTER JOIN 表2,LEFT OUTER JOIN 表3
    三:from 表1 LEFT OUTER JOIN 表2,表2 LEFT OUTER JOIN 表3可我试了还不行!!!!!!!