Sql server 从表中获取正确的行数

Sql server 从表中获取正确的行数,sql-server,Sql Server,我的问题是: SELECT DISTINCT Patient_Ref_master.Dept_ID AS 'Dept_ID' , ( SELECT DISTINCT COUNT(Patient_Ref_master.Sr_No) FROM Patient_Ref_master LEFT JOIN Patient_Master ON Patient_Mas

我的问题是:

SELECT  DISTINCT
        Patient_Ref_master.Dept_ID AS 'Dept_ID' ,
        ( SELECT DISTINCT
                    COUNT(Patient_Ref_master.Sr_No)
          FROM      Patient_Ref_master
                    LEFT JOIN Patient_Master ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
          WHERE     Patient_Ref_master.creation_Date = '2013/08/02'
                    AND Patient_Master.Age >= 0
                    AND Patient_Master.Age <= 16
                    AND Patient_Master.Pat_Sex = 1
        ) AS 'Boys' ,
        ( SELECT DISTINCT
                    COUNT(Patient_Ref_master.Sr_No)
          FROM      Patient_Ref_master
                    LEFT JOIN Patient_Master ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
          WHERE     Patient_Ref_master.creation_Date = '2013/08/02'
                    AND Patient_Master.Age >= 0
                    AND Patient_Master.Age <= 16
                    AND Patient_Master.Pat_Sex = 2
        ) AS 'Girls' ,
        ( SELECT DISTINCT
                    COUNT(Patient_Ref_master.Sr_No)
          FROM      Patient_Ref_master
                    LEFT JOIN Patient_Master ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
          WHERE     Patient_Ref_master.creation_Date = '2013/08/02'
                    AND Patient_Master.Age > 16
                    AND Patient_Master.Pat_Sex = 2
        ) AS 'Females' ,
        ( SELECT DISTINCT
                    COUNT(Patient_Ref_master.Sr_No)
          FROM      Patient_Ref_master
                    LEFT JOIN Patient_Master ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
          WHERE     Patient_Ref_master.creation_Date = '2013/08/02'
                    AND Patient_Master.Age > 16
                    AND Patient_Master.Pat_Sex = 1
        ) AS 'Males'
FROM    Patient_Ref_master
        LEFT JOIN Patient_Master ON Patient_Master.Pat_Code = Patient_Ref_master.Pat_ID
        LEFT JOIN Gender_master ON Gender_master.Code = Patient_Master.Pat_Sex
        LEFT JOIN Dept_Master ON Dept_Master.Dept_code = Patient_Ref_master.Dept_ID
WHERE   Patient_Ref_master.creation_Date = '2013/08/02'
GROUP BY Patient_Master.Pat_Sex ,
        Patient_Ref_master.Dept_ID
我的病人总数是20人,这反映在每一行。我怎样才能根据部门的不同,实现男孩、女孩、男性和女性的完美计数

病人参考主

Sr_No   int
Receipt_no  varchar(50)
old_new int
Pat_catagory    int
Fees    decimal(10, 2)
Pat_ID  int
Doc_ID  int
Dept_ID int
Age_Catagory    int
Age decimal(10, 7)
creation_Date   datetime
created_By  int
Qty decimal(5, 2)
Actual_Amnt decimal(7, 4)
Receipt_type    int
Receipt_total_Price decimal(7, 4)
Receipt_suggestion_Type int
Receipt_actual_suggestion   int
病人管理员

Pat_Code    int
Pat_FName   varchar(30)
Pat_MName   varchar(30)
Pat_SName   varchar(30)
Pat_Sex varchar(10)
Pat_Addr    varchar(100)
Pat_Mob_No  varchar(13)
age int
creation_Date   datetime
created_By  int

虽然没有数据样本我没有信心,但请尝试以下几点:

;with cte as (
    SELECT prm.Dept_ID, p.[Group], prm.Sr_No
    FROM Patient_Ref_master prm
        JOIN Patient_Master pm ON pm.Pat_Code = prm.Pat_ID
        CROSS APPLY (
            select
                case
                    when pm.Age between 0 and 16 and pm.Pat_Sex = 1 then 'Boys'
                    when pm.Age between 0 and 16 and pm.Pat_Sex = 2 then 'Girls'
                    when pm.Age > 16 and pm.Pat_Sex = 2 then 'Females'
                    when pm.Age > 16 and pm.Pat_Sex = 1 then 'Males'
                end as [Group]
        ) p
    WHERE prm.creation_Date = '2013/08/02'
)
select Dept_ID, [Boys], [Girls], [Females], [Males]
from cte
    pivot (count(Sr_No) for [Group] in ([Boys],[Girls],[Females],[Males])) p

我删除了
left join
Gender\u master
Dept\u master
,因为我看不到它们的用途。此外,我还将联接从
更改为
患者参考主表
患者参考主表
之间的内部联接。请描述患者参考主表和患者主表。答案将是一系列的案例陈述,但如果不了解表结构,就很难编写它们。正如上面的评论所述,如果没有表定义,我们无法确定问题。此外,考虑包括表CordNX(20行)的转储。显然,您的一行超出了查询选择条件。请参阅我的编辑,我将提供表架构。
;with cte as (
    SELECT prm.Dept_ID, p.[Group], prm.Sr_No
    FROM Patient_Ref_master prm
        JOIN Patient_Master pm ON pm.Pat_Code = prm.Pat_ID
        CROSS APPLY (
            select
                case
                    when pm.Age between 0 and 16 and pm.Pat_Sex = 1 then 'Boys'
                    when pm.Age between 0 and 16 and pm.Pat_Sex = 2 then 'Girls'
                    when pm.Age > 16 and pm.Pat_Sex = 2 then 'Females'
                    when pm.Age > 16 and pm.Pat_Sex = 1 then 'Males'
                end as [Group]
        ) p
    WHERE prm.creation_Date = '2013/08/02'
)
select Dept_ID, [Boys], [Girls], [Females], [Males]
from cte
    pivot (count(Sr_No) for [Group] in ([Boys],[Girls],[Females],[Males])) p