2012-12-27 55 views
0

下面是我的MS訪問的SQL查詢女士訪問SQL查詢語法錯誤

SELECT '2012/8'  AS [Period], 
     yearmonth.[Monthno (PS)] AS monthno, 
     [assignment_2012].[cluster code] AS cluster, 
     'Transfer out' AS Remark, 
     -1*Count(*)  AS [number], 
     '5'   AS [sorting] 
FROM [assignment_2012] 
INNER JOIN namesort ON [assignment_2012].post = namesort.post 
INNER JOIN yearmonth ON [assignment_2012].[year month]=yearmonth.[Year Month (HM)] 
INNER JOIN 
     (SELECT empid 
        FROM (SELECT empid, 
            [cluster], 
            Count(*) AS [empid no] 
          FROM (SELECT [ID] as empid, 
              [Cluster Code] as cluster 
            FROM [assignment_2012] 
              INNER JOIN yearmonth 
                ON [assignment_2012].[year month] 
                 = 
                 yearmonth.[Year Month (HM)] 
            WHERE yearmonth.[Monthno (PS)] = 243 
              AND [assignment_2012].hc_adj = 1 
              AND [assignment_2012].term <> 'Temporary' 
            UNION 
            SELECT empid, 
              [cluster] 
            FROM [2012] 
            WHERE monthno = 244 
              AND [2012].term <> 'Temporary' 
              AND (div <> 'XXX' 
                OR div IS NULL) 
              AND hc = 1) 
          GROUP BY [empid], 
             [cluster] 
          HAVING Count(*) = 1) 
        GROUP BY empid 
        HAVING Count(*) > 1) As Emps 
ON [assignment_2012]].EmpID = Emps.EmpID 
WHERE yearmonth.[Monthno (PS)] = 243 
     AND [assignment_2012].hc_adj = 1 
     AND yearmonth.[Monthno (PS)] BETWEEN namesort.monthno_start AND namesort.monthno_end 
     AND [assignment_2012].term <> 'Temporary' 
GROUP BY yearmonth.[Monthno (PS)], [assignment_2012].[cluster code] 

當我運行查詢,它提示消息「語法錯誤(缺少運算符)查詢表達式「[assignment_2012] .POST = namesort.post INNER JOING ........「

我發現它會返回錯誤,如果它包含」INNER JOIN yearmonth ON [assignment_2012]。[year month] = yearmonth。[Year Month(HM) ]「在sql語句中

+0

您是否試圖逐個刪除不同的選擇以精確定位錯誤的位置? – bAN

+0

是的,我曾試過。我發現如果在sql語句中包含「INNER JOIN yearmonth ON [assignment_2012]。[year month] = yearmonth。[Year Month(HM)]」,它會返回錯誤 –

回答

0

我修改了yearmonth表的INNER JOIN語句,現在它可以工作。

SELECT '2012/8' AS Period, YearMonth.[Monthno (PS)], assignment_2012.[cluster code] AS cluster, 'Transfer out' AS Remark, -1*Count(*) AS [number], '5' AS sorting 
FROM YearMonth INNER JOIN ((assignment_2012 INNER JOIN namesort ON assignment_2012.post = namesort.post) INNER JOIN (SELECT empid 
        FROM (SELECT empid, 
            [cluster], 
            Count(*) AS [empid no] 
          FROM (SELECT [HKID] as empid, 
              [Cluster Code] as cluster 
            FROM [assignment_2012] 
              INNER JOIN yearmonth 
                ON [assignment_2012].[year month] 
                 = 
                 yearmonth.[Year Month (HM)] 
            WHERE yearmonth.[Monthno (PS)] = 243 
              AND [assignment_2012].hc_adj = 1 
              AND [assignment_2012].term <> 'Temporary' 
            UNION 
            SELECT empid, 
              [cluster] 
            FROM [2012] 
            WHERE monthno = 244 
              AND [2012].term <> 'Temporary' 
              AND (div <> 'XXX' 
                OR div IS NULL) 
              AND hc = 1) 
          GROUP BY [empid], 
             [cluster] 
          HAVING Count(*) = 1) 
        GROUP BY empid 
        HAVING Count(*) > 1) AS Emps ON assignment_2012.ID = Emps.EmpID) ON YearMonth.[Year Month (HM)] = assignment_2012.[Year Month] 
WHERE (((assignment_2012.hc_adj)=1) AND ((assignment_2012.term)<>'Temporary')) 
GROUP BY YearMonth.[Monthno (PS)], assignment_2012.[cluster code];