访问查询语法

时间:2013-04-18 02:02:28

标签: sql ms-access

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

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

3 个答案:

答案 0 :(得分: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 .....

答案 1 :(得分: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中

答案 2 :(得分:0)

谢谢你们,我能够做很少的修改。实际上我在TaskCounter中缺少CINT函数,它在原始表中是字符串格式。我还将日期中的条款更改为&gt;和&lt;。 我的最终查询如下:

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