SQL:将多个行和列组合到单行的问题

时间:2014-05-24 13:04:23

标签: sql sql-server join

第一次海报,长时间潜伏,在这里。

我在查询时遇到一些问题(SQL Server)。我正在从几个表中收集事务数据,将它们连接在一起,并且当它们在某些字段中具有值并且共享相同的id(paymentid时,我们应该将行组合成单个行(select dt.transid, dt.accountingdate, dp.paymentdate, dp.paidamount, (case when dt.amounttypeid = 1 then dt.transamount else 0 end) 'capital', (case when dt.amounttypeid = 2 then dt.transamount else 0 end) 'interest', dp.paymentid from dat_trans dt join cfg_amounttype_desc cad on dt.amounttypeid = cad.amounttypeid join cfg_transtype_desc ctd on dt.transtypeid = ctd.transtypeid join dat_payment dp on dp.paymentid = dt.paymentid where dt.transamount > 0 and dt.paymentid in (4,6,7) and dt.transtypeid in (4,16, 42, 90, 121, 128, 129) ,见下文)。

此查询正在从多个表中收集数据:

transid     accountingdate  paymentdate     paidamount  capital   interest    paymentid
1           2014-02-01      2014-01-01      2000        1600      0           1
2           2014-02-01      2014-01-01      2000        0         200         1        
3           2014-02-01      2014-01-01      2000        200       0           1

4           2014-03-02      2014-02-01      1800        1600      0           2
5           2014-03-02      2014-02-01      1800        0         0           2        
6           2014-03-02      2014-02-01      1800        200       0           2

7           2014-04-03      2014-03-01      600         0         100         3
8           2014-04-03      2014-03-01      600         0         0           3        
9           2014-04-03      2014-03-01      600         500       0           3

和结果(此处为可见性分隔的行组):

accountingdate  paymentdate     paidamount  capital   interest    paymentid
2014-02-01      2014-01-01      2000        1800      200         1
2014-03-02      2014-02-01      1800        1700                  2
2014-04-03      2014-03-01      600         500       100         3

我的目标是将数据列在列,资本和兴趣中,如下所示:

accountingdate  paymentdate     paidamount  capital   interest    paymentid
2014-02-01      2014-01-01      2000        1800                  1
2014-02-01      2014-01-01      2000                  200         1

- (transid在这里留下,它只用于测试)并将被排除在最终结果中。

我试图通过使用此网站上的示例(union,pivot / unpivot,cte's)来总结资本兴趣,但我的结果如下所示:

{{1}}

也就是说,资本确实加起来但仍有两行而不是一行。

所以,基本上我试图为每次付款创建一行,并总结几列中的金额(以及稍后将添加的其他列,如每笔付款的费用信息)。我感谢任何可以帮助我解决这个问题的建议。我希望我已经包含了所需的所有信息。

提前致谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

select paymentid, accountingdate, paymentdate,
       sum(paidamount) paidamount,
       sum(capital) capital,
       sum(interest) interest
from (
      select 
         dt.transid, dt.accountingdate, dp.paymentdate, dp.paidamount, 
         (case when dt.amounttypeid = 1 then dt.transamount else 0 end) 'capital',
         (case when dt.amounttypeid = 2 then dt.transamount else 0 end) 'interest',
         dp.paymentid
      from 
         dat_trans dt            
      join 
         cfg_amounttype_desc cad on dt.amounttypeid = cad.amounttypeid
      join 
         cfg_transtype_desc ctd on dt.transtypeid = ctd.transtypeid                
      join 
         dat_payment dp on dp.paymentid = dt.paymentid               
      where 
         dt.transamount > 0 and dt.paymentid in (4,6,7)
         and dt.transtypeid in (4,16, 42, 90, 121, 128, 129)
      )
group by paymentid, accountingdate, paymentdate

select 
   dt.accountingdate, dp.paymentdate, 
   sum(dp.paidamount) paidamount, 
   sum((case when dt.amounttypeid = 1 then dt.transamount else 0 end)) capital,
   sum((case when dt.amounttypeid = 2 then dt.transamount else 0 end)) interest,
   dp.paymentid
from 
   dat_trans dt            
join 
   cfg_amounttype_desc cad on dt.amounttypeid = cad.amounttypeid
join 
   cfg_transtype_desc ctd on dt.transtypeid = ctd.transtypeid                
join 
   dat_payment dp on dp.paymentid = dt.paymentid               
where 
   dt.transamount > 0 and dt.paymentid in (4,6,7)
   and dt.transtypeid in (4,16, 42, 90, 121, 128, 129)
group by dt.accountingdate, dp.paymentdate, dp.paymentid