3

这是我的查询:

SELECT r.CALLID  AS MultiRES,
c.CallDate AS CallDate,
cr.Institution AS Institution,
cr.Branch AS Branch
FROM tblResolution r
INNER JOIN tblcall c ON c.CallID=c.CallID AND c.CallDate=c.CallDate
INNER JOIN tblCaller cr ON cr.Institution = cr.Institution AND cr.Branch=cr.Branch
GROUP BY r.CALLID HAVING COUNT(*) > 1;

但是当我运行它时出现错误,我知道我需要用 group by 子句做一些事情,我只是不知道把它放在哪里。

编辑:刚刚弄清楚出了什么问题:

SELECT r.CALLID  AS MultiRES,
c.CallDate AS CallDate,
cr.Institution AS Institution,
cr.Branch AS Branch
FROM tblResolution r
INNER JOIN tblcall c ON c.CallID=c.CallID AND c.CallDate=c.CallDate
INNER JOIN tblCaller cr ON cr.Institution = cr.Institution AND cr.Branch=cr.Branch
GROUP BY c.CallDate,cr.Institution,cr.Branch, r.CALLID HAVING COUNT(*) > 1;
4

1 回答 1

2

select 子句中的所有内容都需要在 group by 子句中,除非它是聚合函数(例如countor sum):

SELECT r.CALLID  AS MultiRES,
c.CallDate AS CallDate,
cr.Institution AS Institution,
cr.Branch AS Branch
FROM tblResolution r
INNER JOIN tblcall c ON c.CallID=c.CallID AND c.CallDate=c.CallDate
INNER JOIN tblCaller cr ON cr.Institution = cr.Institution AND cr.Branch=cr.Branch
GROUP BY r.CALLID, c.CallDate, cr.Institution, cr.Branch
HAVING COUNT(*) > 1;
于 2012-04-07T16:46:54.010 回答