Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL如何透视6NF表_Sql_Pivot_6nf - Fatal编程技术网

SQL如何透视6NF表

SQL如何透视6NF表,sql,pivot,6nf,Sql,Pivot,6nf,对于一个简单的数据透视示例,SQL代码是什么样子的,其中有几个表是第六范式的 很多人都在谈论使用6NF表进行数据透视是多么容易和快速,但很难找到这样的例子 假设我有以下表格: Table: EntryCost EntryId Cost Table: EntryMonth EntryId Month Table: EntryDim1 EntryId Dim1 Table: EntryDim2 EntryId Dim2 在不使用MSSQL pivot或等效工具的情况下,我将如何对其进行透视?

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

很多人都在谈论使用6NF表进行数据透视是多么容易和快速,但很难找到这样的例子

假设我有以下表格:

Table: EntryCost
EntryId
Cost

Table: EntryMonth
EntryId
Month

Table: EntryDim1
EntryId
Dim1

Table: EntryDim2
EntryId
Dim2

在不使用MSSQL pivot或等效工具的情况下,我将如何对其进行透视?我说我想用维度和月份来汇总成本,我认为一般的方法是这样的:

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

好的,还有我的想法,但不确定我是否遗漏了什么,我对SQL没有那么熟练。我会让这个问题公开一段时间,让其他人插手。@Jepa我会说让我们等待其他答案。也可以在sum中使用子查询(因为6NF),但我认为这不是更好的方法