2

如果我有格式的数据;

Account  | Period     | Values
Revenue  | 2013-01-01 | 5432
Revenue  | 2013-02-01 | 6471
Revenue  | 2013-03-01 | 7231
Costs    | 2013-01-01 | 4321
Costs    | 2013-02-01 | 5672
Costs    | 2013-03-01 | 4562

我想得到像这样的结果;

Account  | Period     | Values
Margin   | 2013-01-01 | 1111
Margin   | 2013-02-01 |  799
Margin   | 2013-03-01 | 2669
M%       | 2013-01-01 |  .20
M%       | 2013-02-01 |  .13
M%       | 2013-03-01 |  .37

其中保证金 = 收入 - 成本和 M% 是(收入 - 成本)/每个期间的收入。

我可以看到实现这一点的各种方法,但都非常难看,我想知道这些多行计算是否有优雅的通用方法。

谢谢

编辑

其中一些计算可能会变得非常复杂,例如

自由现金流 = 保证金 - 运营支出 - 资本支出 + 营运资本变化 + 支付的利息

所以我希望有一种不需要大量加入自身的通用方法。

谢谢

4

4 回答 4

2

好的,然后 Max 结束 Case 语句,如下所示:

with RevAndCost as (revenue,costs,period)
as
(

    select "Revenue" = Max(Case when account="Revenue" then Values else null end),
           "Costs" = MAX(Case when account="Costs" then values else null end),
           period
            from data
    group by period

)

select Margin = revenue-costs,
       "M%" = (revenue-costs)/nullif(revenue,0)
from RevAndCost
于 2013-05-20T15:54:16.270 回答
1

在这里,我使用公用表表达式在数据表的两个实例之间进行完全外连接,以将收入和成本拉入 1 个表,然后从该 CTE 中进行选择。

with RevAndCost as (revenue,costs,period)
as
(
    select  ISNULL(rev.Values,0) as revenue, 
            ISNULL(cost.values,0) as costs, 
        ISNULL(rev.period,cost.period)
    from data rev full outer join data cost
    on rev.period=cost.period
)

select Margin = revenue-costs,
   "M%" = (revenue-costs)/nullif(revenue,0)
from RevAndCost
于 2013-05-20T15:40:24.020 回答
1

我会这样做:

SELECT r.PERIOD, r.VALUES AS revenue, c.VALUES AS cost,
r.VALUES - c.VALUES AS margin, (r.VALUES - c.VALUES) / r.VALUES AS mPct
FROM 
    (SELECT PERIOD, VALUES FROM t WHERE
    ACCOUNT = 'revenue') r INNER JOIN
    (SELECT PERIOD, VALUES FROM t WHERE
    ACCOUNT = 'costs') c ON
    r.PERIOD = c.PERIOD
于 2013-05-20T15:44:58.030 回答
1

使用联合的完全自联接

Select 'Margin' Account, 
   coalesce(r.period, c.period) Period,
   r.Values - c.Values Values
From myTable r
   Full Join Mytable c
      On c.period = r.period
Union
Select 'M%' Account, 
   coalesce(r.period, c.period) Period,
   (r.Values - c.Values) / r.Values Values
From myTable r
   Full Join Mytable c
      On c.period = r.period
于 2013-05-20T15:35:56.860 回答