如何重写SQL查询以在Microsoft Access 2003中使用:T SQL转换

时间:2010-01-10 15:29:34

标签: sql tsql ms-access

我想为Microsoft Access 2003重写此查询:


SELECT t1.PERSONID
       ,t1.CARDEVENTDATE
       ,MIN(t1.CARDEVENTTIME1) AS Intime
       ,MAX(t2.CARDEVENTTIME1) AS Outtime 

FROM (   SELECT PERSONID
                , CARDEVENTDATE
                , FUNCTIONKEY
                , CONVERT(VARCHAR(10), SUBSTRING(CARDEVENTTIME, 1, 2) + ':' + SUBSTRING(CARDEVENTTIME, 3, 2) + ':' + SUBSTRING(CARDEVENTTIME, 5, 2), 8) 
                 AS CARDEVENTTIME1  
         FROM T_CARDEVENT 
         WHERE      (FUNCTIONKEY = 'A')) AS t1 
         LEFT OUTER JOIN (SELECT PERSONID
                                 ,CARDEVENTDATE, CARDEVENTDAY, FUNCTIONKEY
                                 ,CONVERT(VARCHAR(10), SUBSTRING(CARDEVENTTIME, 1, 2) + ':' + SUBSTRING(CARDEVENTTIME, 3, 2) + ':' + SUBSTRING(CARDEVENTTIME, 5, 2), 8) 
                                  AS CARDEVENTTIME 
                      FROM T_CARDEVENT AS T_CARDEVENT_3) 
           AS t2 
           ON t1.PERSONID = t2.PERSONID 
           AND t1.CARDEVENTDATE = t2.CARDEVENTDATE   
           GROUP BY t1.PERSONID, t1.CARDEVENTDATE )

以上在SQL Server中有效,但在Access中我试图运行此查询。 convert 中产生错误。

如何修改我的查询以适应Access 2003? Access 2003中TSQL's Convert函数的合适等价物是什么?

2 个答案:

答案 0 :(得分:1)

如果您使用SQL Server兼容的语法,则下面的示例可能适用。

在t2中,您似乎在CARDEVENTTIME中缺少1,所以我将其添加到CARDEVENTTIME1。您最近似乎还有一个额外的括号。

SELECT t1.PERSONID
       ,t1.CARDEVENTDATE
       ,MIN(t1.CARDEVENTTIME1) AS Intime
       ,MAX(t2.CARDEVENTTIME1) AS Outtime 

FROM (   SELECT PERSONID
                , CARDEVENTDATE
                , FUNCTIONKEY
                , CDate(Mid(CARDEVENTTIME, 1, 2) + ':' + Mid(CARDEVENTTIME, 3, 2) + ':' + Mid(CARDEVENTTIME, 5, 2)) 
                 AS CARDEVENTTIME1  
         FROM T_CARDEVENT 
         WHERE      (FUNCTIONKEY = 'A')) AS t1 
         LEFT OUTER JOIN (SELECT PERSONID
                                 ,CARDEVENTDATE, CARDEVENTDAY, FUNCTIONKEY
                                  , CDate(Mid(CARDEVENTTIME, 1, 2) + ':' + Mid(CARDEVENTTIME, 3, 2) + ':' + Mid(CARDEVENTTIME, 5, 2)) 
                                  AS CARDEVENTTIME1
                      FROM T_CARDEVENT AS T_CARDEVENT_3) 
           AS t2 
           ON t1.PERSONID = t2.PERSONID 
           AND t1.CARDEVENTDATE = t2.CARDEVENTDATE   
           GROUP BY t1.PERSONID, t1.CARDEVENTDATE 

答案 1 :(得分:0)

你能不能使用普通的Access函数Str(...)?

更新:这里没有Access可以尝试,但是没有一个参数告诉Access只使用sql作为pass throught(不修改它)?