2013-08-07 75 views
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 

明智的上面是我的查詢如下計數病人部門在SQL Server

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

請幫忙。 感謝

回答

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 AS MyCTE ...塊定義一個公共表表達式。這是一個定義類似於即時視圖的平均值。在這種情況下,我使用它來使代碼更具可讀性。您可能會發現更多信息Using Common Table Expressions

然後,PIVOT代碼是一種將錶行轉換爲列的方式,這實際上就是您要求的。你可能已經省略了CTE部分,並且在PIVOT的FROM子句中你可以添加你的代碼片段,但它有些不可讀。在Using PIVOT and 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 
+0

真棒,它的工作。只是一個小的要求。我是這種主軸編碼的新手。你能解釋一下代碼的工作原理嗎,我是說從第1行開始。 – Ankur

+0

請參閱我編輯的解決方案。 –

+0

太好.. !!!非常感謝。 – Ankur

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