如何组合这两个MS Access查询?

时间:2017-01-30 03:50:19

标签: sql ms-access ms-access-2007

SELECT 
    USERINFO.Name, USERINFO.SSN, USERINFO.Badgenumber, 
    FORMAT(CHECKINOUT.CHECKTIME,"yyyy") AS [YEAR], 
    FORMAT(CHECKINOUT.CHECKTIME,"mmmm") AS [MONTH], 
    FORMAT(CHECKINOUT.CHECKTIME,"dd") AS DAYS, 
    FORMAT((Min(CHECKINOUT.CHECKTIME)),"hh:mm am/pm") AS TIMEIN, 
    FORMAT(CHECKINOUT.CHECKTIME,"AMPM") AS [AM/PM], CHECKINOUT.CHECKTYPE
FROM 
    CHECKINOUT 
INNER JOIN 
    USERINFO ON CHECKINOUT.USERID = USERINFO.USERID
WHERE 
    (CHECKINOUT.CHECKTIME >= DATEVALUE(PERIOD_START) AND
     CHECKINOUT.CHECKTIME < DATEADD("d", 1, PERIOD_END) AND
    USERINFO.Name = Insert_Name) AND 
    CHECKINOUT.CHECKTYPE = "I"
GROUP BY 
    CHECKINOUT.USERID, USERINFO.Name, USERINFO.SSN, 
    DateValue(CHECKINOUT.CHECKTIME), FORMAT(CHECKINOUT.CHECKTIME,"AMPM"), 
    USERINFO.Badgenumber, FORMAT(CHECKINOUT.CHECKTIME,"mmmm"), 
    FORMAT(CHECKINOUT.CHECKTIME,"yyyy"), FORMAT(CHECKINOUT.CHECKTIME,"dd"), 
    CHECKINOUT.CHECKTYPE
HAVING 
    (((USERINFO.Badgenumber) < "8000"))
ORDER BY 
    USERINFO.Name, Min(CHECKINOUT.CHECKTIME);

此查询适用于timein

SELECT 
    USERINFO.Name, USERINFO.SSN, USERINFO.Badgenumber, 
    FORMAT(CHECKINOUT.CHECKTIME,"yyyy") AS [YEAR], 
    FORMAT(CHECKINOUT.CHECKTIME,"mmmm") AS [MONTH], 
    FORMAT(CHECKINOUT.CHECKTIME,"dd") AS DAYS, 
    FORMAT((Max(CHECKINOUT.CHECKTIME)),"hh:mm am/pm") AS TIMEOUT, 
    FORMAT(CHECKINOUT.CHECKTIME,"AMPM") AS [AM/PM], 
    CHECKINOUT.CHECKTYPE
FROM
    CHECKINOUT 
INNER JOIN 
    USERINFO ON CHECKINOUT.USERID = USERINFO.USERID
WHERE 
    (CHECKINOUT.CHECKTIME >= DATEVALUE(PERIOD_START) AND
     CHECKINOUT.CHECKTIME < DATEADD("d", 1, PERIOD_END) AND
    USERINFO.Name = Insert_Name) AND
    CHECKINOUT.CHECKTYPE = "O"
GROUP BY 
    CHECKINOUT.USERID, USERINFO.Name, USERINFO.SSN, 
    DateValue(CHECKINOUT.CHECKTIME), FORMAT(CHECKINOUT.CHECKTIME,"AMPM"), 
    USERINFO.Badgenumber, FORMAT(CHECKINOUT.CHECKTIME,"mmmm"), 
    FORMAT(CHECKINOUT.CHECKTIME,"yyyy"), FORMAT(CHECKINOUT.CHECKTIME,"dd"), 
    CHECKINOUT.CHECKTYPE
HAVING 
    (((USERINFO.Badgenumber) < "8000"))
ORDER BY 
    USERINFO.Name, Min(CHECKINOUT.CHECKTIME);

此查询用于超时,

如何将这两个查询组合在一起?

1 个答案:

答案 0 :(得分:1)

除了相应Calc 1子句中的单个条件外,这些查询看起来是相同的。第一个查询有:

WHERE

而第二个有这个

CHECKINOUT.CHECKTYPE = "I"

我认为你可以将这两个条件结合起来如下:

CHECKINOUT.CHECKTYPE = "O"

完整查询:

WHERE ... AND CHECKINOUT.CHECKTYPE IN ("I", "O")

出于提供信息的目的,如果两个查询的逻辑差别很大,但两个查询在SELECT USERINFO.Name, USERINFO.SSN, USERINFO.Badgenumber, FORMAT(CHECKINOUT.CHECKTIME,"yyyy") AS [YEAR], FORMAT(CHECKINOUT.CHECKTIME,"mmmm") AS [MONTH], FORMAT(CHECKINOUT.CHECKTIME,"dd") AS DAYS, FORMAT((MIN(CHECKINOUT.CHECKTIME)), "hh:mm am/pm") AS TIMEIN, FORMAT(CHECKINOUT.CHECKTIME,"AMPM") AS [AM/PM], CHECKINOUT.CHECKTYPE FROM CHECKINOUT INNER JOIN USERINFO ON CHECKINOUT.USERID = USERINFO.USERID WHERE CHECKINOUT.CHECKTIME >= DATEVALUE(PERIOD_START) AND CHECKINOUT.CHECKTIME < DATEADD("d", 1, PERIOD_END) AND USERINFO.Name = Insert_Name AND CHECKINOUT.CHECKTYPE IN ("I", "O") -- here is the change GROUP BY CHECKINOUT.USERID, USERINFO.Name, USERINFO.SSN, DateValue(CHECKINOUT.CHECKTIME), FORMAT(CHECKINOUT.CHECKTIME,"AMPM"), USERINFO.Badgenumber, FORMAT(CHECKINOUT.CHECKTIME,"mmmm"), FORMAT(CHECKINOUT.CHECKTIME,"yyyy"), FORMAT(CHECKINOUT.CHECKTIME,"dd"), CHECKINOUT.CHECKTYPE HAVING USERINFO.Badgenumber < "8000" ORDER BY USERINFO.Name, MIN(CHECKINOUT.CHECKTIME); 子句中具有相同的结构,我们可以在两个查询中找到SELECT