读书人

查询比较有关问题

发布时间: 2012-01-23 21:57:28 作者: rapoo

查询比较问题
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(单位代码)
11169000.00 031202
11202000.00 031701
11248000.00 035101
12374000.00 035901
11199000.00 053101
11126000.00 053904
11181000.00 059901
15589000.00 075501
22374000.00 079801
3102000000.00 079901
11189000.00 091201
11223000.00 093101
11243000.00 093102
11153000.00 093805
791779000.00095101


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


11170000.00 031202
11223000.00 031701
11248000.00 035101
12374000.00 035901
11199000.00 053101
11126000.00 053904
11181000.00 059901
15589000.00 075501
22374000.00 079801
3102000000.00079901
11188000.00 091201
11223000.00 093101
11243000.00 093102
11156000.00 093805
791959000.00095101

怎么把两个查询的结果按字段相减啊(关键字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, ' ') <> ' ' group by hzjxsdm
) A
Inner Join


(
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
) B
On A.hzjxsdm = B.hzjxsdm

读书人网 >SQL Server

热点推荐