如果我有一个反规范化表是这样的:如何合并在TPH与空值的行,并计算相关领域
FinanceList (NetSales,Expenses,Receivables,...etc,Discriminator)
而且我还有一个表(PeriodType
),我归类根据一年两种方法:
Quarter
(4周期)1-
Period1
1月至三月Period2
2-从4至6月3-
Period3
从7至9月4-
Period4
从10至12月Yearly
(1周期)从1至12月
示例数据:
Year per TypeId NetSales Expenses Receivables CompanyId
2016 2 2 164547.0000 NULL NULL 3001
2016 2 2 NULL NULL 50601.0000 3001
2016 2 2 NULL 550.4110 NULL 3001
2016 3 2 222764.0000 NULL NULL 3001
2015 3 2 264843.0000 NULL NULL 3001
2015 2 2 NULL NULL 42049.0000 3001
2015 1 3 NULL NULL 32431.0000 3001
2015 2 2 NULL 614.6200 NULL 3001
2015 2 2 187112.0000 NULL NULL 3001
2014 1 3 NULL NULL 28033.0000 3001
2016 3 2 502757.0000 NULL NULL 3002
2016 3 2 NULL NULL 56407.0000 3002
2016 2 2 429821.0000 NULL NULL 3002
2016 2 2 NULL 516.0000 NULL 3002
2016 2 2 NULL NULL 70724.0000 3002
2015 2 2 NULL 6092.0000 NULL 3002
2015 2 2 NULL NULL 96377.0000 3002
2015 2 2 598416.0000 NULL NULL 3002
2015 3 2 677026.0000 NULL NULL 3002
2015 3 2 NULL NULL NULL 3002
2015 1 3 NULL NULL 92406.0000 3002
2014 1 3 NULL NULL 84243.0000 3002
现在我面临着两个问题:
- 因为我跟着TPH(许多空在那里),所以我想合并所有 行(
year,period,periodTypeId
)为每个公司在一行 。
EX:代替三排,每一个台(类型):
2016 2 2 164547.0000 NULL NULL 3001
2016 2 2 NULL NULL 50601.0000 3001
2016 2 2 NULL 550.4110 NULL 3001
2015 2 2 NULL NULL 42049.0000 3001
2015 2 2 NULL 614.6200 NULL 3001
2015 2 2 187112.0000 NULL NULL 3001
我想一排这样的:
2016 2 2 164547.0000 550.4110 50601.0000 3001
2015 2 2 187112.0000 614.6200 42049.0000 3001
- 如果我想获得一个等式计算如下
(The accumulated number of days in the quarter
(90 or 180 0r 270 or 360
)/(NetSales
/((Receivables in the specific quarter
+ Receivables in the previous yearly year
)/ 2))
因此,例如,如果要calc下在period2 of quarter
这个方程(3001
)公司它将是这样的:
180/(164547.0000/((50601.0000 +32431.0000)/2)) =45 (for year 2016)
180/(187112.0000/((42049.0000 +28033.0000)/2)) =34 (for year 2015)
我的查询:
SELECT a.[Year],d.period,d.PeriodTypeId, a.NetSales,a.Expenses,a.Receivables,
c.CompanyId
FROM FinanceList a INNER JOIN Company c
ON a.CompanyId = c.CompanyId
INNER JOIN ListPeriod d
ON d.FinanceListId = a.FinanceListId
WHERE a.[Year] IN (2016,2015) AND d.PeriodTypeId IN(2,3) --The User Enter only Two Years ,PeriodTypeId ==>2 means quarter ,3 means yearly
ORDER BY c.CompanyId, a.[Year] DESC
我想最终结果是这样两个记录每个公司:
Year per TypeId NetSales Expenses Receivables CompanyId equation
2016 2 2 164547.0000 550.4110 50601.0000 3001 45
2015 2 2 187112.0000 614.6200 42049.0000 3001 34
我使用'MIN'而不是'SUM' –