2017-03-28 125 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创建演示。