避免基于PostgreSQL中的多个列的重复条目

时间:2014-11-13 09:32:33

标签: postgresql duplicates

我正试图摆脱以下结果集中的重复条目:

ID  Name    missionID   teamID  eventTimestamp  
25  C   1000415147500265653 1   02-11-2014 14:04  
25  C   1000415147500265653 1   02-11-2014 14:04  
25  C   1000415147500265653 1   02-11-2014 14:04  
25  C   1000415147500265653 1   02-11-2014 14:04  
25  C   1000415147500265653 2   02-11-2014 14:06  
25  C   1000415147500265653 2   02-11-2014 14:06  
25  C   1000415147500265653 2   02-11-2014 14:06  
25  C   1000415147500265653 2   02-11-2014 14:07  
25  C   1000415147500265653 2   02-11-2014 14:07  
25  C   1000415147500265653 1   02-11-2014 14:08  
25  C   1000415147500265653 1   02-11-2014 14:08  

对于targetID,targetName,missionID,teamID和eventTimestamp的每个组合,我只想要1个条目。所以我希望结果集看起来像这样,而不是上面的结果:

targetID    targetName  missionID   teamID  eventTimestamp  
25  C   1000415147500265653 1   02-11-2014 14:04  
25  C   1000415147500265653 2   02-11-2014 14:06  
25  C   1000415147500265653 2   02-11-2014 14:07  
25  C   1000415147500265653 1   02-11-2014 14:08  

如你所见。可以有其他条目具有相同的targetID,targetName,missionID,teamID组合,但具有不同的时间戳 - 我需要保留这些条目,所以我不能在此上运行最大或最小日期。我尝试使用不同的,排名和分组,但似乎没有任何工作,我不断获得重复的条目。如果我在日期使用max或min,我只会为每个targetID,targetName,missionID,teamID获得一个条目 - 这不是我想要的

我的原始查询只是为了得到重复的结果集:

select targetID, targetName, missionID, teamID, eventTimestamp from (
select a.characterID, a.targetID, a.targetName, a.missionID, b.teamID as teamID, a.eventTimestamp from events_live a
left join (
select distinct characterID, teamID from events_live where eventName = 'missionStarted' or eventName = 'characterCreated') b on a.characterID = b.characterID 

where eventName ='missionObjective' and a.eventTimestamp > '2014-10-31' and a.eventTimestamp < '2014-11-07' and objectiveName = 'Capture' and (targetID ='25' or targetID='26' or targetID ='27' or targetID = '28' or targetID = '29') and b.teamID is not null
order by missionID, targetID, eventTimestamp
)t1

order by missionID, targetID, eventTimestamp

我已尝试使用targetID,targetName,missionID,teamID,eventTimestamp在原始文件上进行另一个select选项 - 我仍然会获得重复项。我试过在eventTimestamp上运行distinct,但它仍然没有给我我想要的东西

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT targetID, targetName, missionID, teamID, date_trunc('minute',eventTimestamp ) AS eventTimestamp
FROM (
    SELECT a.characterID, a.targetID, a.targetName, a.missionID, b.teamID as teamID, a.eventTimestamp 
    FROM events_live a
    LEFT JOIN (
        SELECT distinct characterID, teamID from events_live 
        WHERE eventName = 'missionStarted' 
        OR eventName = 'characterCreated') b 
    ON a.characterID = b.characterID     
    WHERE eventName ='missionObjective' 
    AND a.eventTimestamp > '2014-10-31' 
    AND a.eventTimestamp < '2014-11-07' 
    AND objectiveName = 'Capture' 
    AND (
        targetID ='25' 
        OR targetID='26' 
        OR targetID ='27' 
        OR targetID = '28' 
        OR targetID = '29') 
    AND b.teamID IS NOT NULL
    ORDER BY missionID, targetID, eventTimestamp) t1
GROUP BY targetID, targetName, missionID, teamID, eventTimestamp    
ORDER BY missionID, targetID, eventTimestamp

编辑:date_trunc(&#39;分钟&#39;,eventTimestamp)应该做的伎俩