我的查询重复记录

时间:2016-04-26 18:31:48

标签: mysql sql oracle

我正在尝试构建一个查询来获取报告,但是当我创建条件时,我发现3列与其余结果重复。

我错过了什么?

列为A.MailTo,A.Texta.openTIMEa.closedtime

SELECT DISTINCT U.FirstName, 
                U.LastName,
                U.LogonID,
                S.NAME Skillset,
                A.MailTo,A.Text,
                a.openTIME,a.closedtime 
FROM CLS.USERS U join CLS.Skillsets S on U.CCMSID =S.CCMSID
                 join CLS.CONTACTS C on S.ID=C.SKILLSET 
                 join CLS.Actions  A on A.agent=C.agent
WHERE a.source=40 
 and cast(a.closedtime as date)= cast(CURRENT_TIMESTAMP as date)-1 

2 个答案:

答案 0 :(得分:0)

您可以接收重复列的唯一方法是使用不同的别名多次调用列时,但是,听起来您的意思是“返回重复的行”。如果它们在重复项中都是相同的信息,那么你可以抛出一个

group by u.logonID

制作代码:

SELECT DISTINCT U.FirstName, 
            U.LastName,
            U.LogonID,
            S.NAME Skillset,
            A.MailTo,A.Text,
            a.openTIME,a.closedtime 
FROM CLS.USERS U join CLS.Skillsets S on U.CCMSID =S.CCMSID
             join CLS.CONTACTS C on S.ID=C.SKILLSET 
             join CLS.Actions  A on A.agent=C.agent
WHERE a.source=40 
   and cast(a.closedtime as date)= cast(CURRENT_TIMESTAMP as date)-1 
group by u.logonID

它应删除您可能拥有的任何重复项。

答案 1 :(得分:0)

看起来每个联系人都有很多动作。您可以像这样显示他们的计数(如果ID是Actions的主键):

SELECT * from (
SELECT      U.FirstName, 
            U.LastName,
            U.LogonID,
            S.NAME Skillset,
            count(A.ID) agentCount
FROM CLS.USERS U join CLS.Skillsets S on U.CCMSID =S.CCMSID
             join CLS.CONTACTS C on S.ID=C.SKILLSET 
             join CLS.Actions  A on A.agent=C.agent
WHERE a.source=40 
and cast(a.closedtime as date)= cast(CURRENT_TIMESTAMP as date)-1
group by   U.FirstName, 
            U.LastName,
            U.LogonID,
            S.NAME
) order by agentCount desc;