简体   繁体   English

连接表仅产生一行

[英]Join tables resulting only one row

i am trying to join the following two tables: 我正在尝试加入以下两个表:

       Table Patient                   |        Table incident
patient.id   patient.birthdate         |  incident.patientid       serviceid
1                  1/1/2000            |       1                        8
2                  1/1/1990            |       1                        8
3                  1/1/2005            |       2                        10
4                  1/1/1980            |       3                        11
5                  1/1/2000            |       3                        11
6                  1/1/1990            |       3                        11
7                  1/1/1980            |       6                        23
8                  1/1/2000            |       7                        8

in order to make an age seperation of all patients grouped by their serviceid. 为了按服务对象分组所有患者的年龄。

SELECT serviceid,
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 0 AND 15 THEN 1 ELSE 0 END) AS [Under 15],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 16 AND 18 THEN 1 ELSE 0 END) AS [16-18],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 19 AND 23 THEN 1 ELSE 0 END) AS [19-23],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 24 AND 30 THEN 1 ELSE 0 END) AS [24-30],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 31 AND 40 THEN 1 ELSE 0 END) AS [31-40],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 41 AND 50 THEN 1 ELSE 0 END) AS [41-50],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 51 AND 65 THEN 1 ELSE 0 END) AS [51-65],
        SUM(CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) > 65 THEN 1 ELSE 0 END) AS [>65]

from patient
inner join incident
on patient.id = incident.patientConcerned
group by serviceid

But what i am trying above, counts the age of all patients for ALL their incidents, meaning that i am not counting distinct patients. 但是我在上面尝试的是计算所有事件中所有患者的年龄,这意味着我没有在计算不同的患者。 (for example i am counting patient 1, twice and patient 3, three times) (例如,我对1号患者进行两次计数,对3号患者进行3次计数)

So i want to join these two tables, but only with one row. 所以我想加入这两个表,但只能一行。

How can i do that? 我怎样才能做到这一点?

Use Distinct operator. 使用Distinct运算符。 you query should be like following: 您查询的内容应如下所示:

   SELECT Distinct a.id, a.birthdate ,b.patient from
   patient a inner join incident b ON a.serviceid=b.serviceid

Instead of sum() use count(distinct) . 代替sum()使用count(distinct) Here is an example: 这是一个例子:

SELECT serviceid,
       COUNT(DISTINCT CASE WHEN FLOOR((CAST (GetDate() AS INTEGER) - CAST(patient.birthdate AS INTEGER)) /365.25 ) BETWEEN 0 AND 15
                           THEN Patient.Id 
                      END) AS [Under 15],
       . . .

而不是group by serviceid group by patient.patient_id而是group by serviceid group by patient.patient_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM