我有两表A,B
A表中:                 B表中:
A_CODE  A_COUNT     B_CODE   B_COUNT
M0001   20          M0001     30
M0001    30         
M0001    80
M0002     50
请问我如何得到:
CODE  COUNT
M0001  100
M0002  50

解决方案 »

  1.   

    --试试下边这个语句.select CODE,sum(A_COUNT) as COUNT
    from
    (
    select A_CODE,A_COUNT from 表A
    union all
    select B_CODE,B_COUNT*-1 from 表B) t
    group by A_CODE
      

  2.   

    --更改下:select A_CODE as CODE,sum(A_COUNT) as COUNT
    from
    (
    select A_CODE,A_COUNT from 表A
    union all
    select B_CODE,B_COUNT*-1 from 表B) t
    group by A_CODE