多表查询中的分组依据或不同分组

时间:2016-06-30 09:07:28

标签: sql

我有如下查询,它返回正确的结果,但我想使用GROUP BY或DISTINCT来缩小lnk_ID上的选择范围(第3列)

我已经尝试过它们,但每次都失败了。

SELECT  dmg.dmg_FirstName,
        dmg.dmg_Surname,
        lnk.lnk_ID,
        dlk.dlk_AssessDate,
        dmg.dmg_Sex,
        trn.ScotHealthboard
FROM    DAILY_LINK dlk
JOIN    Absent ab ON ab.ScotRefID = dlk.dlk_ID
JOIN    Link lnk ON lnk.lnk_ID = ab.Person_ID
JOIN    Demographic dmg ON dmg.dmg_ID = lnk.lnk_dmgID
JOIN    Training trn ON trn.Trn_ID = ab.Training_ID
WHERE   dlk.dlk_AssessDate >= '2015/01/01' AND dlk.dlk_AssessDate <= '2015/12/01'
ORDER BY dmg.dmg_Surname, dmg.dmg_FirstName, lnk.lnk_ID;





Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Richard Airey   566105926   15/01/2015 00:00:00 Male    Tayside
Richard Airey   566105926   15/01/2015 00:00:00 Male    Tayside
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley

预期结果

Nuha    Abdelwahab  566106181   16/07/2015 00:00:00     Tayside
Patricia    Agnew   566106230   17/09/2015 00:00:00 Female  Dumfries and Galloway
Richard Airey   566105926   15/01/2015 00:00:00 Male    Tayside
Aileen  Allan   566105477   25/09/2015 00:00:00 Female  Forth Valley

以下失败

SELECT  dmg.dmg_FirstName,
        dmg.dmg_Surname,
        DISTINCT (lnk.lnk_ID),
        dlk.dlk_AssessDate,
        dmg.dmg_Sex,
        trn.ScotHealthboard
FROM    DAILY_LINK dlk
JOIN    Absent ab ON ab.ScotRefID = dlk.dlk_ID
JOIN    Link lnk ON lnk.lnk_ID = ab.Person_ID
JOIN    Demographic dmg ON dmg.dmg_ID = lnk.lnk_dmgID
JOIN    Training trn ON trn.Trn_ID = ab.Training_ID
WHERE   dlk.dlk_AssessDate >= '2015/01/01' AND dlk.dlk_AssessDate <= '2015/12/01'

SELECT  dmg.dmg_FirstName,
        dmg.dmg_Surname,
        lnk.lnk_ID,
        dlk.dlk_AssessDate,
        dmg.dmg_Sex,
        trn.ScotHealthboard
FROM    DAILY_LINK dlk
JOIN    Absent ab ON ab.ScotRefID = dlk.dlk_ID
JOIN    Link lnk ON lnk.lnk_ID = ab.Person_ID
JOIN    Demographic dmg ON dmg.dmg_ID = lnk.lnk_dmgID
JOIN    Training trn ON trn.Trn_ID = ab.Training_ID
WHERE   dlk.dlk_AssessDate >= '2015/01/01' AND dlk.dlk_AssessDate <= '2015/12/01'
GROUP BY lnk.lnk_ID;

2 个答案:

答案 0 :(得分:3)

如果您尝试从查询中删除重复的行,我相信您需要做的就是在单词SELECT之后放置关键字DISTINCT(即SELECT DISTINCT dmg.dmg_FirstName ...

答案 1 :(得分:2)

SELECT DISTINCT --< Here !
        dmg.dmg_FirstName,
        dmg.dmg_Surname,
        lnk.lnk_ID,
        dlk.dlk_AssessDate,
        dmg.dmg_Sex,
        trn.ScotHealthboard
FROM    DAILY_LINK dlk
JOIN    Absent ab ON ab.ScotRefID = dlk.dlk_ID
JOIN    Link lnk ON lnk.lnk_ID = ab.Person_ID
JOIN    Demographic dmg ON dmg.dmg_ID = lnk.lnk_dmgID
JOIN    Training trn ON trn.Trn_ID = ab.Training_ID
WHERE   dlk.dlk_AssessDate >= '2015/01/01' AND dlk.dlk_AssessDate <= '2015/12/01'
ORDER BY dmg.dmg_Surname, dmg.dmg_FirstName, lnk.lnk_ID;

OR

SELECT  MAX(dmg.dmg_FirstName  ) As dmg_FirstName, --< Aggregate
        MAX(dmg.dmg_Surname    ) As dmg_Surname,   --< all 
        lnk.lnk_ID,                                --< but GROUP BY expression
        MAX(dlk.dlk_AssessDate ) As dlk_AssessDate,
        MAX(dmg.dmg_Sex        ) As dmg_Sex,
        MAX(trn.ScotHealthboard) As ScotHealthboard
FROM    DAILY_LINK dlk
JOIN    Absent ab ON ab.ScotRefID = dlk.dlk_ID
JOIN    Link lnk ON lnk.lnk_ID = ab.Person_ID
JOIN    Demographic dmg ON dmg.dmg_ID = lnk.lnk_dmgID
JOIN    Training trn ON trn.Trn_ID = ab.Training_ID
WHERE   dlk.dlk_AssessDate >= '2015/01/01' AND dlk.dlk_AssessDate <= '2015/12/01'
GROUP BY lnk.lnk_ID;