不同于某些选择字段不起作用

时间:2017-05-03 23:54:11

标签: sql

我有以下查询从多个表中进行选择。

select distinct s.playerId PlayerID, h.name TeamName, 
    m.firstname FirstName, m.lastname LastName, h.year Year
from scoringnew s 
join
(select max(s.pts) pts,s.tmid from  scoringnew s group by s.tmid) t
on s.tmid = t.tmid and s.pts = t.pts
join master m on (m.playerId = s.playerId)
join ice_hockey_teams_new h on (h.tmid = s.tmid);

结果正确显示了PlayerID。但是如果我在select上添加更多列,它将显示重复PlayerID,如下所示:

select distinct s.playerId PlayerID, h.name TeamName, 
    m.firstname FirstName, m.lastname LastName, h.year Year, s.pts Points
from scoringnew s 
join
(select max(s.pts) pts,s.tmid from  scoringnew s group by s.tmid) t
on s.tmid = t.tmid and s.pts = t.pts
join master m on (m.playerId = s.playerId)
join ice_hockey_teams_new h on (h.tmid = s.tmid);

上述查询之间唯一不同的是,第二个查询还有一个选择字段s.pts Points,它在结果集中提供了重复的PlayerID。 我想知道为什么distinct并不能适用于所有情况。有什么我误解的吗?

重复的行显示如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

你不是要选择t.pts吗?

select distinct s.playerId PlayerID, h.name TeamName, 
m.firstname FirstName, m.lastname LastName,     h.year Year, t.pts Points
from scoringnew s 
join
(select max(s.pts) pts,s.tmid from  scoringnew s group by s.tmid) t
on s.tmid = t.tmid
join master m on (m.playerId = s.playerId)
join ice_hockey_teams_new h on (h.tmid = s.tmid);