使用Pivot进行年龄范围问题

时间:2016-07-29 15:36:43

标签: sql sql-server tsql pivot pivot-table

我正在使用一个数据透视表来对年龄栏中的年龄段进行分组,并且所有列都没有按照应有的方式填充。我正在考虑LOS(住院时间)的总和,年龄范围总数应该等于TotalLOS,但目前不是。请帮忙!!

这是T-SQL查询:

SELECT *
FROM
(
SELECT [Hosp Name], 
    Cnty, 
    Max([Hosp County]) as [FirstOfHosp_County],
    Max(Hosp) as UID, 
    LOS,
    Age,
    Sum(LOS) as TotalLOS,
    CASE    
        WHEN Age <=17 THEN '0-17'
        WHEN Age Between 18 and 64 THEN '18-64'
        WHEN Age >=65 THEN '65 Up'
        ELSE NULL
    END AS AGERANGE
FROM [S1:  Basic Source Query]
GROUP BY [Hosp Name], Cnty, Age, [Hosp County], Hosp, LOS
) AS S

PIVOT 
(       
    Sum(S.LOS)
    for S.AGERANGE 
    in ([0-17], [18-64],[65 Up])
) as pvt

我正在从Access转换此查询。这是Access查询:

TRANSFORM Sum([S1:  Basic Source Query].LOS) AS Days
SELECT [S1:  Basic Source Query].[Hosp Name],
[S1:  Basic Source Query].Cnty,
FirstOfHosp County:  First([S1:  Basic Source Query].[Hosp County])
UID:  First([S1:  Basic Source Query].Hosp)
Total:  Sum([S1:  Basic Source Query].LOS)
FROM [S1:  Basic Source Query]
GROUP BY [S1:  Basic Source Query].[Hosp Name],
[S1:  Basic Source Query].Cnty
PIVOT Switch([Age] Between 0 And 17,"0-17",[Age] Between 18 And 64,"18-64",[Age]>=65,"65 Up");

这是数据透视之前的Access数据示例。它没有所有列。

Hosp      Hosp Name             LOS     Age
HOSP301   Apple Hospital        16      92  
HOSP301   Apple Hospital        5       34  
HOSP301   Apple Hospital        14      85  
HOSP301   Apple Hospital        7       21  
HOSP301   Apple Hospital        4       79  
HOSP301   Apple Hospital        2       26  
HOSP301   Apple Hospital        2       19  

一旦Access查询运行,就会将其转换为:

UID       Hosp Name             TotalLOS    0-17    18-64     65 Up
HOSP301   Apple Hospital        130                  117      13
HOSP301   Apple Hospital        5                    5  
HOSP301   Apple Hospital        722         60       455      207
HOSP301   Apple Hospital        23          17                6
HOSP301   Apple Hospital        15                            15
HOSP301   Apple Hospital        6                    6
HOSP301   Apple Hospital        3           3
HOSP301   Apple Hospital        32          1        31

这是我得到的错误的SQL结果。 Age Ranges下的数字应与TotalLOS列匹配:

UID     Hosp Name       Cnty    FirstOfHosp_County    Age   TotalLOS    0-17    18-64   65 Up
HOSP301 Apple Hospital  Apple   Apple                 57    24                  4   
HOSP301 Apple Hospital  Apple   Apple                 68    13                          13
HOSP301 Apple Hospital  Apple   Apple                 69    12                  4   
HOSP301 Apple Hospital  Apple   Apple                 71    10                  10  
HOSP301 Apple Hospital  Apple   Apple                 73    8                   2   
HOSP301 Apple Hospital  Apple   Apple                 74    7                   7   
HOSP301 Apple Hospital  Apple   Apple                 75    6                   6   
HOSP301 Apple Hospital  Apple   Apple                 79    34                  17  
HOSP301 Apple Hospital  Bacon   Apple                 63    2            2  
HOSP301 Apple Hospital  Bagwin  Apple                 68    16                  16  

因此,您可以看到范围中的数字始终与TotalLOS列中的数字相匹配。我相信我没有正确地将它从Access转换为T-SQL。

1 个答案:

答案 0 :(得分:0)

不确定您的实际问题是什么,但如果您将SQL更改为类似的内容,它可能会更好:

SELECT [Hosp Name], 
    Cnty, 
    Max([Hosp County]) as [FirstOfHosp_County],
    Max(Hosp) as UID, 
    LOS,
    Sum(LOS) as TotalLOS,
    Sum(CASE WHEN Age <=17 THEN LOS else 0 end) as [0-17],
    Sum(CASE WHEN Age Between 18 and 64 THEN LOS else 0 end) as [18-64],
    Sum(CASE WHEN Age >=65 THEN LOS else 0 end) as [65 Up]
FROM [S1:  Basic Source Query]
GROUP BY [Hosp Name], Cnty, [Hosp County], Hosp, LOS

编辑:删除了年龄,它作为一个列毫无意义。