2016-11-20 20 views
0

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

enter image description here


示例數據:

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 quarter90 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 

回答

1

我認爲這是可以通過聚合函數來實現。使用公共表格表達式來選擇搜索的時間段和年份及其前幾年。處理結果和計算的返回數據。 注意:我只有以下查詢語法測試。

WITH data AS (
    SELECT a.[Year], 
     d.period, 
     d.PeriodTypeId, 
     SUM(a.NetSales) AS NetSales, 
     SUM(a.Expenses) AS Expenses, 
     SUM(a.Receivables) AS 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) 
     OR a.[Year] IN (2016 - 1, 2015 - 1) AND d.PeriodTypeId = 3 -- previous years 
    GROUP BY a.[Year], d.period, d.PeriodTypeId, c.CompanyId 
) 
SELECT [Year], 
     period, 
     PeriodTypeId, 
     NetSales, 
     Expenses, 
     Receivables, 
     CompanyId, 
     CASE 
     WHEN PeriodTypeId = 2 
     THEN (period * 90)/(NetSales/((Receivables + (SELECT Receivables 
                  FROM data T2 
                  WHERE T2.[Year]  = T1.[Year] - 1 
                   AND T2.period  = 1 
                   AND T2.PeriodTypeId = 3 
                   AND T2.CompanyId = T1.CompanyId) /* Receivables previous year */)/2)) 
     ELSE NULL 
     END AS equation 
    FROM data T1 
WHERE [Year] IN (2016, 2015) AND PeriodTypeId IN(2, 3) 
ORDER BY CompanyId, [Year] DESC 
+0

我使用'MIN'而不是'SUM' –