简体   繁体   中英

SQL query syntax on JIRA

The below query is used for "List of users ordered by last login and include user id, email and at least one Group that they belong to" in JIRA.....

SELECT cu.user_name
, dateadd(second,cast(cast(cua.attribute_value as nvarchar(255)) as bigint)/1000,'19700101 00:00:00:000') 'last Login'
, cu.lower_display_name
, cu.email_address
, (SELECT MAX(parent_name) FROM [jira].[dbo].[cwd_membership] cm WHERE cm.child_name = cu.user_name AND cm.parent_name NOT IN ('jira-sysadmins','jira-administrators','jira-users','jira-developers')) 'Group'

FROM cwd_user cu INNER JOIN cwd_user_attributes cua
ON cu.id = cua.user_id
AND cua.attribute_name = 'login.lastLoginMillis'
AND cu.active = '1'
order by 2 desc

Sample Result set for Last Login

last Login
1/8/2014 6:43:07 PM
1/8/2014 6:23:10 PM
1/7/2014 9:11:56 PM

Can anyone please edit this query by adding a condition on "last Login" so that it only returns results from the past 90 days (like "last Login"> sysdate - 90)

I don't know which database you're using? SqlServer?
In this case you can use DATEDIFF() function, adding the clause :
AND DATEDIFF(GETDATE(), dateadd(second,cast(cast(cua.attribute_value as nvarchar(255)) as bigint)/1000,'19700101 00:00:00:000') < 90

I didn't try it (no sqlserver available on my computer), but I think you're pretty close

Since you already have the conversion a clean way is to use a subquery:

SELECT * FROM 
(

    SELECT cu.user_name
    , dateadd(second,cast(cast(cua.attribute_value as nvarchar(255)) as bigint)/1000,'19700101 00:00:00:000') AS [last Login]
    , cu.lower_display_name
    , cu.email_address
    , (SELECT MAX(parent_name) FROM [jira].[dbo].[cwd_membership] cm WHERE cm.child_name = cu.user_name AND cm.parent_name NOT IN ('jira-sysadmins','jira-administrators','jira-users','jira-developers')) 'Group'

    FROM cwd_user cu INNER JOIN cwd_user_attributes cua
    ON cu.id = cua.user_id
    AND cua.attribute_name = 'login.lastLoginMillis'
    AND cu.active = '1'
)
WHERE [last Login] > DATEADD(day,-90,GETDATE())
order by [last login] desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM