内部联接与WHERE子句影响第二次联接

时间:2013-03-28 14:40:40

标签: sql syntax-error inner-join where-clause

我有这个查询,顶级内连接工作正常,但是当我添加第二个连接时,我会在'INNER'附近收到错误的语法,任何人都知道为什么?

SELECT  Time_ID,
    Site_Type_ID,
    SUM (staging.dbo.incoming_measure.ring_time) AS Ring_Time,
    SUM (staging.dbo.incoming_measure.hold_time) As Hold_Time,
    SUM (staging.dbo.incoming_measure.talk_time) AS Talk_Time,
    SUM (staging.dbo.incoming_measure.acw_time) AS ACW_Time
FROM staging.dbo.incoming_measure
      INNER JOIN
   (SELECT Time_ID FROM datamartend.dbo.Time_Dim ) TimeID
      ON TimeID.Time_ID = incoming_measure.StartTime

      INNER JOIN 
   (SELECT Site_Type_ID, Site_Type_Code FROM datamartend.dbo.Site_Type_dim) SiteID
      ON SiteID.Site_Type_ID = incoming_measure.DBID

WHERE StartTimeDate BETWEEN StartTimeDate AND EndTimeDate  
   AND
WHERE SiteId.Site_type_code = incoming_measure.DBID

GROUP BY time_id, site_type_id

1 个答案:

答案 0 :(得分:4)

您必须将WHERE移到GROUP BY

之上
WHERE StartTimeDate BETWEEN StartTimeDate AND EndTimeDate
AND SiteId.Site_type_code = incoming_measure.DBID

Group by time_id, site_type_id