0

如何确保我在父记录上获得唯一性?我想每天计算 mopid + 用户的数量。我怎样才能做到这一点?到目前为止,这是我的代码,我只是不太相信它不会在 mopid + user + day 上给我独特性。

SELECT TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD') DayWorked, MOPNOTES.MOPNOTEUSER, MOPNOTES.mopid, COUNT(*) AS DAILY 
FROM MOPUSER.MOPACTIVITY 
INNER JOIN MOPUSER.MOPNOTES 
  ON MOPACTIVITY.MOPID=MOPNOTES.MOPID
WHERE MOPNOTES.MOPNOTEDATE > TO_DATE('01-JUL-13', 'DD-MON-YY') AND MOPNOTES.MOPNOTEDATE< TO_DATE('01-AUG-13', 'DD-MON-YY')
AND MOPACTIVITY.MOPSERVICEIMPACTED <> 'VOICE'
AND MOPACTIVITY.MOPSERVICEIMPACTED <> 'PWR/ENV'
AND (MOPNOTES.MOPNOTEUSER LIKE '%Ramesh%'
OR MOPNOTES.MOPNOTEUSER LIKE '%Saravanan%'
OR MOPNOTES.MOPNOTEUSER LIKE '%Boominathan%'
OR MOPNOTES.MOPNOTEUSER LIKE '%Srinivasan%'
OR MOPNOTES.MOPNOTEUSER LIKE '%Sathya%')
GROUP BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
ORDER BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
4

1 回答 1

0

根据定义,GROUP BY 将为您按分组的列组合提供唯一的行。由于您按 mopid + user + day 分组,因此您将在 mopid + user + day 上具有唯一性。

为了说明,这个查询:

SELECT a, b
FROM ( SELECT MOD(LEVEL, 2) a, MOD(LEVEL, 4) b FROM DUAL CONNECT BY LEVEL < 11 )
GROUP BY a, b

...将为您提供与此查询相同的结果:

SELECT DISTINCT a, b
FROM ( SELECT MOD(LEVEL, 2) a, MOD(LEVEL, 4) b FROM DUAL CONNECT BY LEVEL < 11 )

如果您真的想确定,您可以验证以下两个查询之间的计数是否匹配:

SELECT COUNT(1) FROM (
SELECT TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD') DayWorked, MOPNOTES.MOPNOTEUSER, MOPNOTES.mopid, COUNT(*) AS DAILY 
    FROM ...[etc]...
    GROUP BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
    --ORDER BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
)

SELECT COUNT(1) FROM (
    SELECT DISTINCT ROWNUM, TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD') DayWorked, MOPNOTES.MOPNOTEUSER, MOPNOTES.mopid --, COUNT(*) AS DAILY 
    FROM ...[etc]...
    --GROUP BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
    --ORDER BY TO_CHAR(MOPNOTES.MOPNOTEDATE, 'YYYY-MM-DD'),MOPNOTES.MOPNOTEUSER,MOPNOTES.MOPID
)
于 2013-10-10T22:47:17.343 回答