Ms SQL SQL查询语法错误

时间:2012-12-27 07:18:54

标签: sql ms-access syntax-error

以下是我的ms访问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 ........”

如果在sql语句中包含“INNER JOIN yearmonth ON [assignment_2012]。[year month] = yearmonth。[Year Month(HM)]”,我发现它返回错误

1 个答案:

答案 0 :(得分:0)

我修改了年度表的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];