0

对于一个简单的透视示例,SQL 代码是什么样的,其中有几个表处于第六范式?

很多人都在谈论使用 6NF 表如何轻松快速地进行旋转,但真的很难找到这样的例子。

可以说我有以下表格:

Table: EntryCost
EntryId
Cost

Table: EntryMonth
EntryId
Month

Table: EntryDim1
EntryId
Dim1

Table: EntryDim2
EntryId
Dim2

我将如何在不使用 MSSQL PIVOT 或同等产品的情况下进行旋转?说我想将成本与侧面的维度和沿列的月份进行汇总

4

1 回答 1

0

我认为一般方法是这样的:

select
    D1.Dim1, D2.Dim2,
    sum(case when M.Month = 1 then C.Cost end) as [1],
    sum(case when M.Month = 2 then C.Cost end) as [2],
    sum(case when M.Month = 3 then C.Cost end) as [3],
    sum(case when M.Month = 4 then C.Cost end) as [4],
    sum(case when M.Month = 5 then C.Cost end) as [5],
    sum(case when M.Month = 6 then C.Cost end) as [5],
    sum(case when M.Month = 7 then C.Cost end) as [7],
    sum(case when M.Month = 8 then C.Cost end) as [8],
    sum(case when M.Month = 9 then C.Cost end) as [9],
    sum(case when M.Month = 10 then C.Cost end) as [10],
    sum(case when M.Month = 11 then C.Cost end) as [11],
    sum(case when M.Month = 12 then C.Cost end) as [12]
from EntryCost as C
    left outer join EntryMonth as M on M.EntryID = C.EntryID
    left outer join EntryDim1 as D1 on D1.EntryID = C.EntryID
    left outer join EntryDim2 as D2 on D2.EntryID = C.EntryID
group by D1.Dim1, D2.Dim2
于 2013-10-14T13:22:53.230 回答