日期:2014-05-19  浏览次数:20574 次

查询比较问题
select   isnull(count(hth),0)   as   sl,isnull(sum(gcts),0)   as   gcts,isnull(sum(dkje),0)   as   dkje,hzjxsdm   from   ht   where   yxbz= '1 '   and     convert(char(6),isnull(gsldshrq, ' '),112)= '200707 '     and   isnull(bgsj, ' ') <> 'group   by   hzjxsdm
得到以下数据:
sl             gcts   (数量)               dkje   (金额)               hzjxsdm(单位代码)
1 1 169000.00             031202
1 1 202000.00             031701
1 1 248000.00               035101
1 2 374000.00               035901
1 1 199000.00             053101
1 1 126000.00             053904
1 1 181000.00                 059901
1 5 589000.00                   075501
2 2 374000.00                     079801
3 10 2000000.00                   079901
1 1 189000.00                     091201
1 1 223000.00     093101
1 1 243000.00   093102
1 1 153000.00     093805
7 9 1779000.00 095101


            select   isnull(count(hth),0)   as   sl,isnull(sum(gcts),0)   as   gcts,isnull(sum(dkje),0)   as   dkje,hzjxsdm     from   ht   where   yxbz= '0 '                          
  and   (hth   like   '%*01 '   and   isnull(fpbz, ' ') <> '1 ')   and   convert(char(6),isnull(gsldshrq, ' '),112)= '200707 '   group   by   hzjxsdm  


1 1 170000.00                 031202
1 1 223000.00                 031701
1 1 248000.00                 035101
1 2 374000.00   035901
1 1 199000.00   053101
1 1 126000.00   053904
1 1 181000.00                   059901
1 5 589000.00                   075501
2 2 374000.00                   079801
3 10 2000000.00 079901
1 1 188000.00                   091201
1 1 223000.00                   093101
1 1 243000.00     093102
1 1 156000.00                   093805
7 9 1959000.00 095101

怎么把两个查询的结果按字段相减啊(关键字hzjxsdm)

------解决方案--------------------
Select A.sl - B.sl As sl, A.gcts - B.gcts As gcts, A.dkje - B.dkje As dkje, A.hzjxsdm
From
(
select isnull(count(hth),0) as sl,isnull(sum(gcts),0) as gcts,isnull(sum(dkje),0) as dkje,hzjxsdm from ht where yxbz= '1 ' and convert(char(6),isnull(gsldshrq, ' '),112)= '200707 ' and isnull(bgsj, '