我希望你能帮助我。我在下面的查询成功运行并返回大约100行。
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME
但是,在返回的数据集中有两行包含相同的项目名称,即
Domain Project Last Accessed
PASSENGERS Alpha 2015-02-25 10:56:36
AIRPORTS Alpha 2015-01-09 11:11:50
我想从数据集中明确排除AIRPORTS Alpha行。阅读了其他帖子后,我尝试了下面的查询(及其变体),但我返回了0行,这不是理想的结果。
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE NOT EXISTS (
SELECT * FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.DOMAIN_NAME = 'AIRPORTS' AND SESSIONS_HISTORY.PROJECT_NAME = 'Alpha'
)
AND SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME
如何修改我的查询以排除AIRPORTS Alpha行?请注意,我不需要根据日期字段排除的解决方案,只需要DOMAIN_NAME和PROJECT_NAME的组合。提前谢谢。
答案 0 :(得分:0)
试试这个
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
and (DOMAIN_NAME <> 'AIRPORTS' or PROJECT_NAME <> 'Alpha')
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME
答案 1 :(得分:0)
为同一DOMAIN_NAME / PROJECT_NAME组合选择end_time = max(end_time)的行:
SELECT DOMAIN_NAME AS "Domain",
PROJECT_NAME AS "Project",
END_TIME AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY t1
WHERE USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
AND END_TIME = (SELECT MAX(END_TIME)
FROM DB.td.SESSIONS_HISTORY t2
WHERE USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
AND t1.DOMAIN_NAME = t2.DOMAIN_NAME
AND t1.PROJECT_NAME = t2.PROJECT_NAME)
ORDER BY DOMAIN_NAME, PROJECT_NAME
答案 2 :(得分:0)
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
AND DOMAIN_NAME <> 'AIRPORTS'
AND PROJECT_NAME <> 'Alpha'
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME
答案 3 :(得分:-1)
提供了两个对我有效的答案。
感谢@Arvo:
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
AND NOT (DOMAIN_NAME = 'AIRPORTS' AND PROJECT_NAME = 'Alpha')
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME
感谢@ASh同样有效:
SELECT DOMAIN_NAME AS "Domain", PROJECT_NAME AS "Project", MAX(END_TIME) AS "Last Accessed"
FROM DB.td.SESSIONS_HISTORY
WHERE SESSIONS_HISTORY.USER_NAME NOT IN ('UserA', 'UserB', 'UserC')
AND (DOMAIN_NAME <> 'AIRPORTS' or PROJECT_NAME <> 'Alpha')
GROUP BY DOMAIN_NAME, PROJECT_NAME
ORDER BY DOMAIN_NAME, PROJECT_NAME