0
select distinct
    Patient_Ref_master.Dept_ID as 'dept',
    Patient_Ref_master.Male_femal as 'Gender',
    count(Patient_Ref_master.Pat_ID) as 'count'
from 
    Patient_Ref_master
left join 
    Patient_Master on Patient_Master.Pat_Code=Patient_Ref_master.Pat_ID
where 
    (Patient_Ref_master.Age > 16 
     and dbo.Patient_Master.Pat_Sex = 2 
     and Patient_Ref_master.creation_Date = '2013/08/02') 
    or 
    (Patient_Ref_master.Age > 16 
     and dbo.Patient_Master.Pat_Sex = 1 
     and Patient_Ref_master.creation_Date = '2013/08/02') 
    or
    (Patient_Ref_master.Age >= 0 
     and Patient_Ref_master.Age <= 16 
     and dbo.Patient_Master.Pat_Sex = 2 
     and Patient_Ref_master.creation_Date = '2013/08/02') 
    or
    (Patient_Ref_master.Age >= 0 
     and Patient_Ref_master.Age <= 16 
     and dbo.Patient_Master.Pat_Sex = 1 
     and Patient_Ref_master.creation_Date = '2013/08/02') 
group by 
    Patient_Ref_master.Male_femal, Patient_Ref_master.Dept_ID

以上是我的查询,它返回一个表格如下

  Dept    Gender           Count
    102    Females   3
    102    Males     4
    103    Boys          2
    103    Females   2
    103    Girls     1
    103    Males     1
    104    Females   6
    104    Males     1

在这里,我按部门统计了男性,女性,女孩和男孩。但我希望输出以下列方式显示

 Dept    Males Females Boys Girls 
    102    3        2     5     5  
    103    4        5     2     6   
    104    2        1     1     5

这是按部门统计的男孩、女孩、男性和女性。我该怎么做才能得到类似上述模式的东西?Pivot可以选择吗?我从来没有用过Pivot

请帮忙。谢谢

4

2 回答 2

3

是的,PIVOT是解决方案

select *
from 
     ( 
        -- your query here
     ) d
    pivot (sum([count]) for gender in (females,males,boys,girls)) p

或更好

select *
from 
     ( 
         select 
              Patient_Ref_master.Dept_ID,
              Patient_Ref_master.Male_femal,      
         from Patient_Ref_master
         left join Patient_Master on Patient_Master.Pat_Code=Patient_Ref_master.Pat_ID
         where 
             -- your filter here
     ) d
    pivot (count(pat_code) for Male_femal in (females,males,boys,girls)) p

请参阅http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

于 2013-08-07T15:31:55.557 回答
2
;WITH MyCTE AS
(
    SELECT    DISTINCT
              Patient_Ref_master.Dept_ID AS 'dept',
              Patient_Ref_master.Male_femal AS 'Gender',
              COUNT(Patient_Ref_master.Pat_ID) AS 'count'
    FROM      Patient_Ref_master
              LEFT JOIN Patient_Master 
                  ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
    WHERE    (
                  Patient_Ref_master.Age > 16 
                  AND dbo.Patient_Master.Pat_Sex = 2 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age > 16 
                  AND dbo.Patient_Master.Pat_Sex = 1 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age >= 0 
                  AND Patient_Ref_master.Age <= 16 
                  AND dbo.Patient_Master.Pat_Sex = 2 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age >= 0 
                  AND Patient_Ref_master.Age <= 16 
                  AND dbo.Patient_Master.Pat_Sex = 1 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
    GROUP BY Patient_Ref_master.Male_femal,
             Patient_Ref_master.Dept_ID
)
SELECT     Dept,
           [Males], 
           [Females], 
           [Boys], 
           [Girls]
FROM       (
               SELECT Gender, 
                      Count ,
                      Dept
               FROM   MyCTE
           ) AS SourceTable
PIVOT
(
    MAX(Count)
    FOR Gender IN ([Males], [Females], [Boys], [Girls])
) AS PivotTable

WITH MyCTE AS... 块定义了一个公用表表达式。它是一种定义动态视图的方法。在这种情况下,我使用它是为了使代码更具可读性。您可以找到有关使用公用表表达式的更多信息

然后 PIVOT 代码是一种将表行转换为列的方法,这实际上是您所要求的。您可以省略 CTE 部分,并在 PIVOT 的 FROM 子句中添加您的代码,但它会有些不可读。使用 PIVOT 和 UNPIVOT中有关 PIVOT 的更多信息

不幸的是 PIVOT 的工作方式,您只能在其中定义三列

  1. 一个将是结果表的“ID”
  2. 一个将包含您要显示为行的值
  3. 最后一个将包含您需要的聚合

所以 PIVOT 就是这样,仅此而已。但是 PIVOT 仍然是一个表,您可以将它与其他表连接起来。

作为补充,我还必须注意,您可以定义多个 CTE,每个 CTE 都包含对前一个定义的引用。语法是:

;WITH CTE1 AS
(
  CTE1 SELECT
)
, CTE2
(
  CTE2 SELECT ... May refer to CTE1
)

所以,一个完整的答案是这样的:

;WITH MyCTE AS
(
    SELECT    DISTINCT
              Patient_Ref_master.Dept_ID AS 'dept',
              Patient_Ref_master.Male_femal AS 'Gender',
              COUNT(Patient_Ref_master.Pat_ID) AS 'count'
    FROM      Patient_Ref_master
              LEFT JOIN Patient_Master 
                  ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
    WHERE    (
                  Patient_Ref_master.Age > 16 
                  AND dbo.Patient_Master.Pat_Sex = 2 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age > 16 
                  AND dbo.Patient_Master.Pat_Sex = 1 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age >= 0 
                  AND Patient_Ref_master.Age <= 16 
                  AND dbo.Patient_Master.Pat_Sex = 2 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
             OR
             (
                  Patient_Ref_master.Age >= 0 
                  AND Patient_Ref_master.Age <= 16 
                  AND dbo.Patient_Master.Pat_Sex = 1 
                  AND Patient_Ref_master.creation_Date = '2013/08/02'
             )
    GROUP BY Patient_Ref_master.Male_femal,
             Patient_Ref_master.Dept_ID
)
,PivotCTE AS
(
    SELECT     Dept,
               [Males], 
               [Females], 
               [Boys], 
               [Girls]
    FROM       (
                   SELECT Gender, 
                          Count ,
                          Dept
                   FROM   MyCTE
               ) AS SourceTable
    PIVOT
    (
        MAX(Count)
        FOR Gender IN ([Males], [Females], [Boys], [Girls])
    ) AS PivotTable
)
SELECT    Dept,
          DeptName
          [Males], 
          [Females], 
          [Boys], 
          [Girls]
FROM      PivotCTE P
          JOIN DepartmentTable D
               ON P.Dept = D.Dept
于 2013-08-07T15:32:05.013 回答