我正在处理一个SQL查询,其中用户的最大Audit_Date不在范围内,因为他们已经很长时间没有使用系统了。我是这样尝试的:
SELECT DISTINCT
UserID
,max(Audit_Date)
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON
RV_USERS.UserID=RV_AUDIT.UserID
group BY RV_USERS.UserID
AND --it doesn't like the "and" here
not exists(
select *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON ON RV_USERS.UserID=RV_AUDIT.UserID
where
Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
我尝试使用not exists, like the example,但在这种情况下不起作用。在关键字“与”附近,不存在之前,我得到了错误的语法。我需要对此进行查看,以至于我认为变量和临时表都不可用。它会在水晶报表中使用,并在中央管理控制台中安排。
** Update1:每个答案都尝试过: 选择地区 “ RV_USERS”。“ UserID” ,“ RV_AUDIT”。“审核日期” 来自“ RV_USERS”“ RV_USERS” INNER JOIN“ RV_AUDIT”“ RV_AUDIT”在“ RV_USERS”上。“ UserID” =“ =” RV_AUDIT“。” UserID“
按“ RV_USERS”分组。“ UserID”,“ RV_AUDIT”。“ Audit_Date”
拥有 max(“ RV_AUDIT”。“ Audit_Date”)<'2019-05-29 00:00:00'
和 “ RV_USERS”。UserID=“我”
即使我从5月开始使用该系统,它也会返回5月的日期。我通过删除“最大日期”部分进行了检查,看看我的日期是今天。
** Update2:尝试了其他答案:
SELECT DISTINCT
"RV_USERS"."UserID"
,max("RV_AUDIT"."Audit_Date")
FROM "RV_USERS" "RV_USERS"
INNER JOIN "RV_AUDIT" "RV_AUDIT" ON "RV_USERS"."UserID"="RV_AUDIT"."UserID"
WHERE
not exists(
select *
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" not between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY "RV_USERS"."UserID"
这没有返回任何东西,但是我们知道有些经理没有使用该系统。
**每个答案更新3:
SELECT DISTINCT
u."UserID"
,max(a."Audit_Date")
FROM "RV_USERS" u
INNER JOIN "RV_AUDIT" a ON u."UserID"=a."UserID"
WHERE
u.UserID not in(
select u2.UserID
FROM "RV_USERS" u2
INNER JOIN "RV_AUDIT" a2 ON u2."UserID"=a2."UserID"
where
a2."Audit_Date" between '2019-05-27 00:00:00' and '10/31/2019'
)
group BY u."UserID"
答案 0 :(得分:1)
我建议使用having
子句:
SELECT UserID, max(Audit_Date)
FROM RV_USERS RV_USERS INNER JOIN
RV_AUDIT RV_AUDIT
ON RV_USERS.UserID = RV_AUDIT.UserID
GROUP BY RV_USERS.UserID
HAVING max(Audit_Date) < '2019-05-29';
您的查询似乎比必要的要复杂得多。
答案 1 :(得分:1)
您有多个错误,以下是更正的代码:
SELECT DISTINCT
RV_U.UserID
, max(RV_U.Audit_Date)
FROM RV_USERS RV_U
INNER JOIN RV_AUDIT RV_A ON RV_U.UserID=RV_A.UserID
WHERE
NOT EXISTS(
SELECT *
FROM RV_USERS RV_USERS
INNER JOIN RV_AUDIT RV_AUDIT ON RV_USERS.UserID=RV_AUDIT.UserID
WHERE Audit_Date not between '2019-05-29 00:00:00' and '10/29/2019'
)
GROUP BY RV_U.UserID;
NNER JOIN RV_AUDIT RV_AUDIT ON ON
RV_USERS.UserID=RV_AUDIT.UserID
有两次ON
。GROUP BY
应该放在最后AND
应替换为WHERE
尝试实施此更改。
答案 2 :(得分:0)
用and
子句替换where
并最后放置group by以使上述查询至少运行