I am having a bit of trouble getting accurate results from a query that looks to me as if it should work. RDBMS is MySQL.
I am trying to get the max last login date from a union select on three databases for Atlassian products JIRA, Confluence and Stash. There should only be a single entry in the results for each user (the max(Last_Login) is supposed to get the maximum last login date and show a result if that max(Last_Login) is older than today - 90 days.
I summarize the output of the union query then select from that aggregate so that I can get the max login date that is 90 days ago or prior.
The problem: The query seems to work for the most part, but there are a handful of cases where a user shows activity in Confluence, Stash or JIRA interfaces that is more recent than what I have here.
Here is the query I am attempting to use:
select
User_Name,
First_Name,
Last_Name,
max(Last_Login) as Last_Login
FROM
(
SELECT
u.user_name as User_Name,
u.first_name as First_Name,
u.last_name Last_Name,
ifnull(from_unixtime((cast(a.attribute_value AS UNSIGNED)/1000), '%Y-%m-%d'), '1970-01-01') AS Last_Login
FROM jira.cwd_user u
LEFT JOIN jira.cwd_user_attributes a ON u.id = a.user_id AND a.attribute_name = 'login.lastLoginMillis'
JOIN jira.cwd_directory d ON u.directory_id = d.id
where u.active = 1
UNION
select
u.user_name as User_Name,
u.first_name as First_Name,
u.last_name Last_Name,
ifnull(from_unixtime((cast(ca.attribute_value AS UNSIGNED)/1000), '%Y-%m-%d'), '1970-01-01') AS Last_Login
FROM confluence.cwd_user u
LEFT JOIN confluence.cwd_user_attribute ca ON u.id = ca.user_id AND ca.attribute_name = 'login.lastLoginMillis'
JOIN confluence.cwd_directory d ON u.directory_id = d.id
where u.active = 'T'
UNION
select
u.user_name as User_Name,
u.first_name as First_Name,
u.last_name Last_Name,
ifnull(from_unixtime((cast(sa.attribute_value AS UNSIGNED)/1000), '%Y-%m-%d'), '1970-01-01') AS Last_Login
FROM stash.cwd_user u
LEFT JOIN stash.cwd_user_attribute sa ON u.id = sa.user_id AND sa.attribute_name = 'login.lastLoginMillis'
JOIN stash.cwd_directory d ON u.directory_id = d.id
where u.is_active = 'T'
) as z
group by User_Name, First_Name, Last_Name
having (max(Last_Login)) <= date_sub(now(), interval 90 day)
order by User_Name asc, Last_Login desc