如果我有一個反規範化表是這樣的:如何合併在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' –