0

以下是我的 ms access 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)]”,它会返回错误

4

1 回答 1

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];
于 2012-12-27T08:27:03.177 回答