1

我只是将基于 SQL 的查询转换为在 SQL Server 上成功运行的 Access。我已将所有内容都转换为 Access 兼容格式,除了括号数有问题。我收到以下错误“来自 CLAUSE 的语法错误”,请帮助

SELECT CINT(MID(TC2.CHILDCOUNTER, 7, LEN(TC2.CHILDCOUNTER)) ) AS PKCHILDID
,tc2.VisitType
    ,MAX(IIf( tktResults.TaskCounter IN ( '001410' ,'001463' ,'001431' ),  tktResults.Result, NULL) ) AS KWA_QuitOffered
    ,MAX(IIf( tktResults.TaskCounter IN ( '001411' ,'001464' ,'001432' ),   tktResults.Result, NULL) ) AS KWA_QuitReferral
FROM ((tblConsultations tc2
INNER JOIN tblChild tc 
  ON tc2.ChildCounter = tc.ChildCounter)
LEFT  JOIN tblDelivery td 
  ON td.ChildCounter = tc.ChildCounter)
LEFT JOIN (
SELECT ttr.ResultCounter
    ,ttr.ChildCounter
    ,tkt.VisitType
    ,ttr.Result
    ,ttr.TaskCounter
FROM tblTaskResults ttr
LEFT JOIN tlkpKeyTasks tkt ON tkt.TaskCounter = ttr.TaskCounter
    AND tkt.TaskCounter IN (
        '001410'
        ,'001463'
        ,'001431'
        ,'001411'
        ,'001464'
        ,'001432'
        )
) AS tktResults 
    ON tc2.VisitType = tktResults.VisitType
AND tktResults.ChildCounter = tc2.ChildCounter
WHERE tc2.VisitType in (1, 2, 3, 4)
   AND tc2.ConsDate > '20130127' AND tc2.ConsDate < '20130228'
GROUP BY CINT(MID(TC2.CHILDCOUNTER, 7, LEN(TC2.CHILDCOUNTER)))
                                            ,TC2.VISITTYPE
                                            ,TC.CENTRECODE
                                            ,TC2.CENTRECODE
                                            ,TC2.COUNCILCODE
                                            ,TC2.CONSDATE
                                            ,TC.FEEDING3MONTHS
                                            ,TC.FEEDING6MONTHS
                                            ,TC.FEEDING12MONTHS
                                            ,TC2.WEIGHT
                                            ,TC2.HEADCIRC
                                    ORDER BY CINT(MID(TC2.CHILDCOUNTER, 7,   LEN(TC2.CHILDCOUNTER)) ), TC2.VISITTYPE
4

3 回答 3

2

当您有多个表并且您有导致问题的不匹配的括号时,MS Access 需要 JOIN 周围的括号。查询应该是:

SELECT ....
FROM (((tblConsultations tc2
INNER JOIN tblChild tc 
  ON tc2.ChildCounter = tc.ChildCounter)
LEFT JOIN tblDelivery td 
  ON td.ChildCounter = tc.ChildCounter)
LEFT JOIN 
(
  SELECT ttr.ResultCounter
    ,ttr.ChildCounter
    ,tkt.VisitType
    ,ttr.Result
    ,ttr.TaskCounter
  FROM tblTaskResults ttr
  LEFT JOIN tlkpKeyTasks tkt 
    ON tkt.TaskCounter = ttr.TaskCounter
    AND tkt.TaskCounter IN ('001410','001463'
                            ,'001431','001411'
                            ,'001464','001432')
) AS tktResults 
  ON tc2.VisitType = tktResults.VisitType
  AND tktResults.ChildCounter = tc2.ChildCounter)
WHERE .....

正如我在上一条评论中建议的那样,您应该为子查询创建并保存一个单独的查询,然后加入该查询。子查询可以任意调用,例如调用 myQuery:

  SELECT ttr.ResultCounter
    ,ttr.ChildCounter
    ,tkt.VisitType
    ,ttr.Result
    ,ttr.TaskCounter
  FROM tblTaskResults ttr
  LEFT JOIN tlkpKeyTasks tkt 
    ON tkt.TaskCounter = ttr.TaskCounter
    AND tkt.TaskCounter IN ('001410','001463'
                            ,'001431','001411'
                            ,'001464','001432')

然后你将把它包含在你的 JOIN 中:

SELECT ....
FROM (((tblConsultations tc2
INNER JOIN tblChild tc 
  ON tc2.ChildCounter = tc.ChildCounter)
LEFT JOIN tblDelivery td 
  ON td.ChildCounter = tc.ChildCounter)
LEFT JOIN myQuery AS tktResults  -- this is the name of the saved query your create
  ON tc2.VisitType = tktResults.VisitType
  AND tktResults.ChildCounter = tc2.ChildCounter)
WHERE .....
于 2013-04-18T02:08:52.320 回答
0

将您的子查询转换为单独的命名查询,然后使用该命名查询代替子查询。还可以通过将内部连接查询另存为单独的查询来删除它:

所以先来看看这部分:

SELECT 
    CINT(MID(TC2.CHILDCOUNTER, 7, LEN(TC2.CHILDCOUNTER)) ) AS PKCHILDID
    ,tc2.VisitType
    ,MAX(IIf( tktResults.TaskCounter IN ( '001410' ,'001463' ,'001431' ),  tktResults.Result, NULL) ) AS KWA_QuitOffered
    ,MAX(IIf( tktResults.TaskCounter IN ( '001411' ,'001464' ,'001432' ),   tktResults.Result, NULL) ) AS KWA_QuitReferral
FROM tblConsultations tc2
   INNER JOIN tblChild tc 
     ON tc2.ChildCounter = tc.ChildCounter

并将其保存为查询,例如BaseQuery

然后拿这个:

SELECT 
    ttr.ResultCounter
    ,ttr.ChildCounter
    ,tkt.VisitType
    ,ttr.Result
    ,ttr.TaskCounter
FROM tblTaskResults ttr
    LEFT JOIN tlkpKeyTasks tkt 
        ON tkt.TaskCounter = ttr.TaskCounter 
           AND tkt.TaskCounter IN (
                '001410'
                ,'001463'
                ,'001431'
                ,'001411'
                ,'001464'
                ,'001432'
            )

并将其保存为另一个查询,例如SubQuery(使用更好的名称)

然后您的查询变为:

SELECT 
   CINT(MID(TC.CHILDCOUNTER, 7, LEN(TC.CHILDCOUNTER)) ) AS PKCHILDID
   ,tc.VisitType
   ,MAX(IIf( tktResults.TaskCounter IN ( '001410' ,'001463' ,'001431' ),  tktResults.Result, NULL) ) AS KWA_QuitOffered
    ,MAX(IIf( tktResults.TaskCounter IN ( '001411' ,'001464' ,'001432' ),   tktResults.Result, NULL) ) AS KWA_QuitReferral    
FROM BaseQuery tc
    LEFT JOIN tblDelivery td 
      ON td.ChildCounter = tc.ChildCounter)
    LEFT JOIN SubQuery tktResults 
      ON tc.VisitType = tktResults.VisitType AND tktResults.ChildCounter = tc.ChildCounter)
WHERE tc.VisitType in (1, 2, 3, 4)
   AND tc.ConsDate > '20130127' AND tc.ConsDate < '20130228'
GROUP BY 
    CINT(MID(TC.CHILDCOUNTER, 7, LEN(TC.CHILDCOUNTER)))
    ,TC.VISITTYPE
    ,TC.CENTRECODE
    ,TC.CENTRECODE
    ,TC.COUNCILCODE
    ,TC.CONSDATE
    ,TC.FEEDING3MONTHS
    ,TC.FEEDING6MONTHS
    ,TC.FEEDING12MONTHS
    ,TC.WEIGHT
    ,TC.HEADCIRC
ORDER BY CINT(MID(TC.CHILDCOUNTER, 7,   LEN(TC.CHILDCOUNTER)) ), TC.VISITTYPE

请注意 tc2.VisitType 和 tc2.ChildCounter 变为 tc.VisitType 和 tc.ChildCounter,因为它包含在 BaseQuery 中

于 2013-04-18T02:49:42.040 回答
0

谢谢大家,我只需稍加修改就可以做到。实际上,我在 TaskCounter 中缺少 CINT 函数,该函数在原始表中是字符串格式。我还将 date 中的 between 子句更改为 > 和 <。我的最终查询如下:

SELECT CINT(MID(TC2.CHILDCOUNTER, 7, LEN(TC2.CHILDCOUNTER)) ) AS PKCHILDID
,tc2.VisitType
    ,MAX(IIf( tktResults.TaskCounter IN ( '001410' ,'001463' ,'001431' ),  tktResults.Result, NULL) ) AS KWA_QuitOffered
    ,MAX(IIf( tktResults.TaskCounter IN ( '001411' ,'001464' ,'001432' ),   tktResults.Result, NULL) ) AS KWA_QuitReferral
FROM ((tblConsultations tc2
INNER JOIN tblChild tc 
  ON tc2.ChildCounter = tc.ChildCounter)
LEFT  JOIN tblDelivery td 
  ON td.ChildCounter = tc.ChildCounter)
LEFT JOIN (
SELECT ttr.ResultCounter
    ,ttr.ChildCounter
    ,tkt.VisitType
    ,ttr.Result
    ,ttr.TaskCounter
FROM tblTaskResults ttr
LEFT JOIN tlkpKeyTasks tkt ON CINT(tkt.TaskCounter) = CINT(ttr.TaskCounter)
WHERE tkt.TaskCounter IN (
        '001410'
        ,'001463'
        ,'001431'
        ,'001411'
        ,'001464'
        ,'001432'
        )
) AS tktResults 
    ON tc2.VisitType = tktResults.VisitType
AND tktResults.ChildCounter = tc2.ChildCounter
WHERE tc2.VisitType in (1, 2, 3, 4)
   AND tc2.ConsDate > '19980127' AND tc2.ConsDate < '20130228'
GROUP BY CINT(MID(TC2.CHILDCOUNTER, 7, LEN(TC2.CHILDCOUNTER)))
                                            ,TC2.VISITTYPE
                                            ,TC.CENTRECODE
                                            ,TC2.CENTRECODE
                                            ,TC2.COUNCILCODE
                                            ,TC2.CONSDATE
                                            ,TC.FEEDING3MONTHS
                                            ,TC.FEEDING6MONTHS
                                            ,TC.FEEDING12MONTHS
                                            ,TC2.WEIGHT
                                            ,TC2.HEADCIRC
                                    ORDER BY CINT(MID(TC2.CHILDCOUNTER, 7,  LEN(TC2.CHILDCOUNTER)) ), TC2.VISITTYPE
于 2013-04-18T03:22:12.733 回答