2017-09-15 40 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 

非常感謝

路易絲