2017-09-15 39 views
1

以下任何帮助将非常感谢;如何在加入多个表后删除sql查询结果中的重复记录

使用以下脚本加入多个表以收集我需要的信息后,现在结果中有多个重复记录。

我无法使用截然不同,所以在导出之前如何摆脱select语句中的重复结果?

use Cohort 

select * 

from patients 

join immunisations 
on patients.patient_id = immunisations.patient_id 

join titles 
on patients.title_id = titles.title_id 

join courses 
on immunisations.course_id = courses.course_id 

join departments 
on patients.patient_id = departments.department_id 

join employees 
on patients.patient_id = employees.post_title_id 

问候

路易丝

+2

不同,因为你使用的是行不通的*。您需要指定要保留的列并再次尝试清除。 –

+0

在发布问题之前,您需要阅读SQL基础知识。了解如何使用GROUP BY子句:https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql – Tanner

+0

可能的重复[如何使用T-SQL Group By](https://stackoverflow.com/questions/2702/how-do-i-use-t-sql-group-by) – Tanner

回答

1

@Rossbush @SMor

嗨,你的2间,你已经帮我解决了,我深深地感激的问题。

这就是我所做的工作;

use Cohort 

select distinct 
    patients.first_name, 
    patients.last_name, 
    patients.dob, 
    titles.description, 
    courses.description, 
    departments.description, 
    immunisations.date_due, 
    immunisations.date_given, 
    immunisations.comments 
from patients 
    join immunisations on patients.patient_id = immunisations.patient_id 
    join titles on patients.title_id = titles.title_id 
    join courses on immunisations.course_id = courses.course_id 
    join departments on patients.patient_id = departments.department_id 
    join employees on patients.patient_id = employees.post_title_id 

非常感谢

路易丝