选择查询以获取特定金额

时间:2016-11-24 13:40:36

标签: sql sql-server group-by

我想在第二张表(odemetutari)中减去第一张表(tutar)付款金额中的总金额 - 付费金额

如何为网格视图制作此图并显示tckimlikno,adisoyadi,计算新金额

SELECT  sum(tutar) AS IcraTutari ,tckimlikno
  FROM Om_icra_infos
  where tckimlikno=tckimlikno
  group by  tckimlikno

首先在这里找到与tckimlikno相同的tutar

和第二个

SELECT sum(odemetutari)
FROM Om_icraodeme_infos
where tckimlikno=tckimlikno

我想为他的每一行显示第一个tutar - 第二个odemetutari = calculatedamount并在新选择的视图中显示

像这样:

tckimlikno  calculatedamount
1111111111  111,22   ( tutar-odemetutari)

2 个答案:

答案 0 :(得分:0)

select a.tckimlikno,sum(tutar)-sum(odemetutari) as calculatedamount from
Om_icra_infos a join Om_icraodeme_infos b on a.tckimlikno=b.tckimlikno group by a.tckimlikno;

(or)

select t1.tckimlikno,IcraTutari-icraodeme as calculatedamount from
(SELECT  tckimlikno,sum(tutar) AS IcraTutari FROM Om_icra_infos where tckimlikno=tckimlikno  group by  tckimlikno)as t1 join 
(SELECT tckimlikno,sum(odemetutari) as icraodeme FROM Om_icraodeme_infos where tckimlikno=tckimlikno   group by  tckimlikno) as t2
on t1.tckimlikno=t2.tckimlikno groyup by t1.tckimlikno;

答案 1 :(得分:0)

@lener的解决方案计算不正确,因为2次需要a.tutar。 我用这个查询用交叉连接修复它,也许对于其他程序员需要:

select a.adisoyadi,a.tckimlikno,(select  v1.tutar - v2.odemetutari as IcraBakiyesi from (Select   sum(tutar) as tutar from Om_icra_infos WHERE tckimlikno=tckimlikno ) as v1 CROSS JOIN (Select    sum(odemetutari) as odemetutari from Om_icraodeme_infos  WHERE tckimlikno=tckimlikno )as v2) as icraBakiyesi from
Om_icra_infos a join Om_icraodeme_infos b on a.tckimlikno=b.tckimlikno  group by a.tckimlikno,a.adisoyadi