2017-03-28 142 views
0

我有幾列我想放入數據透視表的數據。這是該數據目前看起來像一次下面的查詢是RAN:多個集合數據透視表

SELECT 
    t.clinic, 
    t.fiscal_year, 
    t.total, 
    n.new_pats, 
    (t.total - n.new_pats) AS active 
FROM (SELECT DISTINCT 
    clinic, 
    fiscal_year, 
    COUNT(DISTINCT patient_id) AS total 
FROM transactions t 
JOIN period p 
    ON (t.date_entered BETWEEN p.period_start AND p.period_end) 
GROUP BY clinic, fiscal_year) t 
JOIN (SELECT DISTINCT 
    clinic, 
    per.fiscal_year, 
    COUNT(DISTINCT patient_id) AS new_pats 
FROM patient pat 
JOIN period per 
    ON (pat.first_visit_date BETWEEN per.period_start AND per.period_end) 
GROUP BY clinic, fiscal_year) n 
    ON (t.clinic = n.clinic AND t.fiscal_year = n.fiscal_year) 

enter image description here

而且我想通過自己的行三個集合體被分解數據和年作爲它看起來像這樣的列:

enter image description here

這也可以被分解爲每人每年合計3個單獨的列,但是這就是我的理想目標是。我之前並沒有將SQL數據透視表放在一起,並且處於茫然之中。有沒有更好的方式去以所需的方式格式化這些數據?

+0

請勿將其放入實際表格中。這將會是多餘的(你正在複製現有的數據)並且不靈活(例如,如果年份發生變化,或者即使原始表格中的數據發生變化,情況也會發生變化)。相反,使用「取消」當前查詢的視圖或存儲過程(或者通過實際取消它或將當前查詢重新加工爲每個診所/年/聚合的聯合查詢),然後進行調整。 – ZLK

回答

0

試試這個:

CREATE TABLE #ActualData 
(
    Clinic varchar(50), 
    fiscal_year int, 
    total int, 
    new_pats int, 
    active int 
) 

INSERT INTO #ActualData VALUES ('A', 2016, 3538,1787,1751), ('A', 2017, 1218,373,845), ('B', 2009, 1,2,3), ('B', 2010, 1,2,3) 

SELECT * FROM #ActualData ad 

;WITH temps AS 
    (
     SELECT Clinic, fiscal_year, aggregateF, Number FROM 
     (
     SELECT ad.Clinic, ad.fiscal_year, ad.total,ad.new_pats,ad.active FROM #ActualData ad 
    ) src 
     UNPIVOT 
     (
     Number FOR aggregateF IN (total,new_pats ,active) 
    ) unpvt 
    ) 
SELECT Clinic, aggregateF, [2009],[2010],[2016],[2017] 
FROM 
(
    SELECT t.Clinic, t.aggregateF, t.fiscal_year, t.Number FROM temps t 
) src 
PIVOT 
(
    MIN (Number) FOR fiscal_year IN ([2009],[2010],[2016],[2017]) 
) pvt 
ORDER BY pvt.Clinic, pvt.aggregateF DESC 

DROP TABLE #ActualData 
1

你可以試試這個

SELECT clinic, 
     aggregate_field, 
     ISNULL([2009],0) AS [2009], 
     ISNULL([2010],0) AS [2010], 
     ISNULL([2016],0) AS [2016], 
     ISNULL([2017],0) AS [2017] 
FROM (SELECT clinic, 
       fiscal_year, 
       aggregate_field, 
       value 
     FROM (
       ------- This part is your original query ----------------- 
       SELECT t.clinic, 
         t.fiscal_year, 
         t.total, 
         n.new_pats, 
         (t.total - n.new_pats) AS active 
       FROM (SELECT DISTINCT clinic, 
             fiscal_year, 
             Count(DISTINCT patient_id) AS total 
         FROM transactions t 
           JOIN period p 
           ON (t.date_entered BETWEEN 
             p.period_start AND p.period_end) 
         GROUP BY clinic, fiscal_year) t 
         JOIN (SELECT DISTINCT clinic, 
              per.fiscal_year, 
              Count(DISTINCT patient_id) AS new_pats 
          FROM patient pat 
            JOIN period per 
             ON (pat.first_visit_date BETWEEN 
              per.period_start AND per.period_end) 
          GROUP BY clinic, fiscal_year) n 
         ON (t.clinic = n.clinic 
           AND t.fiscal_year = n.fiscal_year) 
       ------------------------------------------------------------- 
       ) 
       unpivot_source 
       UNPIVOT (value 
         FOR aggregate_field IN (total, 
               new_pats, 
               active)) unpivot_result) AS 
     pivot_source 
     PIVOT (Max(value) 
      FOR fiscal_year IN ([2009], 
           [2010], 
           [2016], 
           [2017])) AS pivot_result 
ORDER BY clinic, aggregate_field DESC 

我在這裏http://rextester.com/MFHWV68715創建演示。