0

我正在尝试使用两个表来获取结果集

  1. 追踪平衡
  2. 老化

表的结构是

老化

loc    |    glAcct    |    Amount
-----------------------------------
1013   |     1101     |    200
-----------------------------------
1211   |     1102     |    300
-----------------------------------

试算表

loc   |    g1101    |   g1102
------------------------------
1013  |     20      |    30
------------------------------
1211  |     10      |    40
-----------------------------

现在我要做的是将行与我现在想要的列匹配

loc   |   glAcct    |    agigingAmount |  Trialbalance Amount
--------------------------------------------------------------
1013  |     1101    |      200         |        20
--------------------------------------------------------------
1211  |     1102    |      300         |        40
-------------------------------------------------------------     

我尝试过类似的案例陈述

select ag.loc,ag.glAcct,
case
   when ag.glAcct = '1101' then select g1101 from trialbalance 
   when ag.glAcct = '1102' then select gl101 from trialbalance 
end  

 from trialbalance tb,Aging ag where ag.loc = tb.loc

但它不工作。

提前致谢。

4

2 回答 2

2

你几乎在那里:

SELECT ag.loc, ag.glAcct, ag.Amount AS agigingAmount
CASE ag.glAcct
   WHEN '1101' THEN tb.g1101
   WHEN '1102' THEN tb.g1102
END AS [Trialbalance Amount]
FROM Aging ag
INNER JOIN trialbalance tb on ag.loc = tb.loc
于 2012-09-14T07:39:16.030 回答
1

试试这个:

SELECT ag.loc,
ag.glAcct, 
ag.Amount AS agigingAmount,
TrialbalanceAmount=
CASE ag.glAcct
   WHEN '1101' THEN tb.g1101
   WHEN '1102' THEN tb.g1102
END
FROM Aging ag,trialbalance tb 
WHERE ag.loc = tb.loc;

这是一个工作示例

于 2012-09-14T07:56:44.893 回答