向现有SQL添加额外语句

时间:2013-11-25 16:42:27

标签: sql

我正在尝试修改一个返回用户记录的事件数的SQL语句。目前的陈述是 -

SELECT 
    USERS.NAME, 
    Count(INCIDENTS_H.SERVICEREQNO) 
FROM Sostenuto.sunrise.INCIDENTS_H INCIDENTS_H 
INNER JOIN  Sostenuto.sunrise.USERS USERS 
    ON INCIDENTS_H.OWNERACCOUNT = USERS.SERVICEREQNO 
WHERE (INCIDENTS_H.ADDEDDATE >= {ts '2013-11-25 00:00:00'}) 
    AND (INCIDENTS_H.OPERATIONID = 102005166) 
    AND (INCIDENTS_H.OWNERGROUP = 123000012 
        OR INCIDENTS_H.OWNERGROUP=123000031 
        OR INCIDENTS_H.OWNERGROUP=123000047) 
    AND (INCIDENTS_H.ADDEDBY=INCIDENTS_H.OWNERACCOUNT) 
GROUP BY USERS.NAME

哪个工作正常。无论我需要在另一个表中添加另一个子句,我还需要包括 -

INCIDENTS.ADDEDBY = INCIDENTS_H.OWNERACCOUNT

然而,我正在努力修改原始声明以包含此内容。任何人都可以给我任何指示吗?

2 个答案:

答案 0 :(得分:0)

SELECT 
    USERS.NAME, 
    Count(INCIDENTS_H.SERVICEREQNO) 
FROM Sostenuto.sunrise.INCIDENTS_H INCIDENTS_H 
INNER JOIN  Sostenuto.sunrise.USERS USERS 
    ON INCIDENTS_H.OWNERACCOUNT = USERS.SERVICEREQNO
INNER JOIN INCIDENTS I
    ON INCIDENTS.ADDEDBY = INCIDENTS_H.OWNERACCOUNT
WHERE (INCIDENTS_H.ADDEDDATE >= {ts '2013-11-25 00:00:00'}) 
    AND (INCIDENTS_H.OPERATIONID = 102005166) 
    AND (INCIDENTS_H.OWNERGROUP = 123000012 
        OR INCIDENTS_H.OWNERGROUP=123000031 
        OR INCIDENTS_H.OWNERGROUP=123000047) 
    AND (INCIDENTS_H.ADDEDBY=INCIDENTS_H.OWNERACCOUNT) 
GROUP BY USERS.NAME

答案 1 :(得分:0)

尝试:

SELECT u.NAME, Count(h.SERVICEREQNO) 
FROM Sostenuto.sunrise.INCIDENTS_H h 
    Join Sostenuto.sunrise.USERS u  
       ON h.OWNERACCOUNT = u.SERVICEREQNO 
    Join Incidents i 
       On i.ADDEDBY = h.OWNERACCOUNT 
WHERE (h.ADDEDDATE>={ts '2013-11-25 00:00:00'}) 
AND (h.OPERATIONID=102005166) 
AND (h.OWNERGROUP=123000012 OR h.OWNERGROUP=123000031 OR h.OWNERGROUP=123000047) 
AND (h.ADDEDBY=h.OWNERACCOUNT) 
GROUP BY u.NAME