This is a page that lists example SQL queries that some JIRA users might find useful, mainly for reporting purposes.
Fixed by and Cascading Field Value
If you have a JIRA installation that uses a Cascading Select List custom field to track the version/build that the issue has been fixed in, and would like to find issues that were fixed by a particular user in a particular version/build, you can use the SQL query below. Please note that the SQL does not filter out reopened issues, but returns issues that were resolved at least once. Due to this, duplicates are also possible in the generated result set.
select jiraissue.*
from jiraissue,
OS_HISTORYSTEP,
customfieldvalue,
customfieldoption
where OS_HISTORYSTEP.ENTRY_ID = jiraissue.id
AND OS_HISTORYSTEP.ACTION_ID = <action_id>
AND OS_HISTORYSTEP.CALLER = <user_name>
AND customfieldvalue.issue = jiraissue.id
AND customfieldvalue.PARENTKEY = <parent_key>
AND customfieldvalue.stringvalue = customfieldoption.id
AND customfieldoption.customvalue like '<cf_value>';
Where
- <user_name> - the username of the desired user
- <action_id> - the id of your transition into the fixed state (may need multiple)
- <parent_key> - the id of the Level 1 option in customfieldoption - E.g. 10040
- <cf_value> - the Level 2 value of the cascading field. E.g 'realease%'
Find Fixed For versions for an issue
If you want to fins out the Versions an Issue has been marked "Fix For" you can run the following query
select version.id, vname
from version,
nodeassociation,
jiraissue
where ASSOCIATION_TYPE = 'IssueFixVersion'
AND SINK_NODE_ID = version.id
AND SOURCE_NODE_ID = jiraissue.id
AND pkey = '<issue_key>';
Where
- <issue_key> - the key of an issue. E.g. TEST-10
Find all issues changed by a user after a certain date
If you want to find out all the issues that a particular user has changed use the following query
SELECT DISTINCT(j.id) FROM jiraissue j, changegroup g
WHERE j.id = g.issueid
AND g.author = '<user name>'
AND g.created > '<date>';
Where
- <date> - the earliest desired date (The date should be in the format 'yyyy-mm-dd hh:mm:s'. E.g '2005-10-06 14:40:28')
- <username> - the name of the desired user
Find Statuses of all issues in a project on a given date
You can use this SQL to retreive the status of all issues on a give date in a give project: Note. This was tested under MySQL
SELECT JI.pkey, STEP.STEP_ID
FROM (SELECT STEP_ID, ENTRY_ID
FROM OS_CURRENTSTEP
WHERE OS_CURRENTSTEP.START_DATE < '<your date>'
UNION SELECT STEP_ID, ENTRY_ID
FROM OS_HISTORYSTEP
WHERE OS_HISTORYSTEP.START_DATE < '<your date>'
AND OS_HISTORYSTEP.FINISH_DATE > '<your date>' ) As STEP,
(SELECT changeitem.OLDVALUE AS VAL, changegroup.ISSUEID AS ISSID
FROM changegroup, changeitem
WHERE changeitem.FIELD = 'Workflow'
AND changeitem.GROUPID = changegroup.ID
UNION SELECT jiraissue.WORKFLOW_ID AS VAL, jiraissue.id as ISSID
FROM jiraissue) As VALID,
jiraissue as JI
WHERE STEP.ENTRY_ID = VALID.VAL
AND VALID.ISSID = JI.id
AND JI.project = <proj_id>;
Where
- <your date> is the date you want to check
- <proj_id> is the project you want to check
Find Status counts for a Project on a given date
Or you can find out the counts on specific date: Note. This was tested under MySQL
SELECT count(*), STEP.STEP_ID
FROM (SELECT STEP_ID, ENTRY_ID
FROM OS_CURRENTSTEP
WHERE OS_CURRENTSTEP.START_DATE < '<your date>'
UNION SELECT STEP_ID, ENTRY_ID
FROM OS_HISTORYSTEP
WHERE OS_HISTORYSTEP.START_DATE < '<your date>'
AND OS_HISTORYSTEP.FINISH_DATE > '<your date>' ) As STEP,
(SELECT changeitem.OLDVALUE AS VAL, changegroup.ISSUEID AS ISSID
FROM changegroup, changeitem
WHERE changeitem.FIELD = 'Workflow'
AND changeitem.GROUPID = changegroup.ID
UNION SELECT jiraissue.WORKFLOW_ID AS VAL, jiraissue.id as ISSID
FROM jiraissue) As VALID,
jiraissue as JI
WHERE STEP.ENTRY_ID = VALID.VAL
AND VALID.ISSID = JI.id
AND JI.project = <proj_id>
Group By STEP.STEP_ID;
Where
- <your date> is the date you want to check
- <proj_id> is the project you want to check
Find how Many Issue Moved into States for a given Period
Use this SQL to find out how many issues were Created, Resolved, ..., Closed during a given period. Note that if an issue moves through more than 1 transition, it will be counted more than once. Note. This was tested under MySQL
SELECT NEWSTRING AS Status, count(*) AS Number
FROM changeitem, changegroup, jiraissue
WHERE changeitem.field = 'Status'
AND changeitem.groupid = changegroup.id
AND changegroup.issueid = jiraissue.id
AND jiraissue.project = <project_id>
AND changegroup.CREATED >= '<date_from>'
AND changegroup.CREATED < '<date_to>'
Group By NEWSTRING
UNION
SELECT 'Created' As Status, count(*) AS Number
FROM jiraissue
WHERE jiraissue.CREATED >= '<date_from>'
AND jiraissue.CREATED < '<date_to>'
AND jiraissue.project = <project_id>;
Where
- <date_from> is the date you want to check from
- <date_to> is the date you want to check to
- <project_id> is the project you want to check
Get Components for an Issue
Get all the Components for an Issue
SELECT jiraissue.pkey, component.cname
FROM nodeassociation, component, jiraissue
WHERE component.ID = nodeassociation.SINK_NODE_ID
AND jiraissue.id = nodeassociation.SOURCE_NODE_ID
AND nodeassociation.ASSOCIATION_TYPE = 'IssueComponent'
AND pkey = '<issue_key>';
Find date that Closed issues were closed
Find out the date an issue was Closed for all currnetly closed issues.
select pKey, OS_CURRENTSTEP.STATUS, OS_CURRENTSTEP.START_DATE from jiraissue, OS_CURRENTSTEP where issuestatus = 6 AND OS_CURRENTSTEP.ENTRY_ID = jiraissue.WORKFLOW_ID;
Simple join - jiraissue and jiraaction
select * from jiraissue left join jiraaction on jiraissue.id = jiraaction.issueid;
Simple join - jiraissue and changegroup
select * from jiraissue left join changegroup on jiraissue.id = changegroup.issueid;
Simple join - Changegroup and changeitem
select * from changegroup left join changeitem on changegroup.id = changeitem.groupid;
Simple join - jiraissue and os_currentstep
select * from jiraissue left join OS_CURRENTSTEP on jiraissue.WORKFLOW_ID = OS_CURRENTSTEP.ENTRY_ID;
Simple join - jiraissue and os_historystep
select * from jiraissue left join OS_HISTORYSTEP on jiraissue.WORKFLOW_ID = OS_HISTORYSTEP.ENTRY_ID;
Comments (18)
Aug 13, 2006
William Crighton says:
jira user's full name and email: select psname.propertyvalue &...jira user's full name and email:
select ps_name.propertyvalue fullname
, ps_email.propertyvalue email
from propertystring ps_name
, propertystring ps_email
, propertyentry pe_name
, propertyentry pe_email
, userbase ub
where ub.id = pe_name.entity_id
and ub.id = pe_email.entity_id
and pe_name.id = ps_name.id
and pe_email.id = ps_email.id
and pe_name.entity_name = 'OSUser'
and pe_name.property_key = 'fullName'
and pe_email.entity_name = 'OSUser'
and pe_email.property_key = 'email'
Dec 04, 2006
Matt Doar says:
This page would also be a good place to describe how to enable logging of all sq...This page would also be a good place to describe how to enable logging of all sql queries against a Jira database. That way, I can just perform some action from the browser and deduce what happened underneath it all. For confluence, the steps appear to be:
#log4j.logger.net.sf.hibernate.SQL=DEBUG, confluencelog
#log4j.additivity.net.sf.hibernate.SQL=false
#log4j.logger.net.sf.hibernate.type=DEBUG, confluencelog
#log4j.additivity.net.sf.hibernate.type=false
Dec 04, 2006
Matt Doar says:
For mysql, you can always add log=/var/lib/mysql/foo.log to the mysqld section i...For mysql, you can always add log=/var/lib/mysql/foo.log to the mysqld section in /etc/my.cnf, but it would still be more useful to coordinate this output with other log messages from Jira.
Jan 07, 2007
Michelle de Guzman says:
A user has contributed the following regarding CSV imports and the Fix For Versi...A user has contributed the following regarding CSV imports and the Fix For Version field:
Greetings - I had an issue in which I had imported a bunch of issues via CSV, but their "fix version" was not set. The following are my notes during the fix process, which describe the process by which issues, imported into Jira with no "fix version" set, can be set to a reasonable value that works for your system. I am posting this here in hopes that it might help someone, because it was one step I had to take to get all these wonderful charting reports working for me. There are probably many better ways to accomplish what I did below. All errors are mine, and all comments and suggestions are of course welcome.
The reader should also note:
First, all issues with no fix version must be found. This sql queries against jira issue, checking for id's that do not show up in the nodeassociation table with the "IssueFixVersion" flag.
Notes:
Take a closer look at the to_char functionality. Select from jiraissue, and limit the id's to a reasonable range for readability:
This SQL returns this:
The calculated values "June 2006", "December 2006" will serve as the version, later when we write to the nodeassociation table.
Now, make it easy for ourselves by creating a view:
You can query against the view as though it were a table:
Looking ahead, next, we need to somehow link this version text to the vname in the projectversions table, and grab the projectversion table id (assuming all the versions have been already entered), and use this as the "fix version". Then, we can write the issue id, and fix version id into the nodeassociation table in this sort of format:
... assuming 12345 is the issue id and 54321 is the version id.
I created a view of a query, to prep the data to go into the nodeassociation table. However, there are a number of tricks to note. First, take a look at the SQL:
Note:
This SQL returns:
Finally, to do the insert into nodeassociation, Jira (tomcat) should be shut down first, and then the prepfornodeassocinsert view can be used as the datasource for the insert.
You can confirm the fields in nodeassociation by running a "\d nodeassociation" in the psql interactive query program on the command line.
jira37=# \d nodeassociation Table "public.nodeassociation" Column | Type | Modifiers --------------------+-----------------------+----------- source_node_id | numeric(18,0) | not null source_node_entity | character varying(60) | not null sink_node_id | numeric(18,0) | not null sink_node_entity | character varying(60) | not null association_type | character varying(60) | not null sequence | numeric(9,0) | Indexes: "pk_nodeassociation" PRIMARY KEY, btree (source_node_id, source_node_entity, sink_node_id, sink_node_entity, association_type) "node_sink" btree (sink_node_id, sink_node_entity) "node_source" btree (source_node_id, source_node_entity)Then, because there is no constraint on the sequence field, a sql insert query can be run just on the fields in the prepfornodeassocinsert view, specifying which fields to add to.
Finally, take these steps to get started again:
#start tomcat
#reindex manually
The fix versions for all your issues should now be set up, allowing you to run various reports.
Regards,
Rick
Feb 02, 2007
Robin Stephenson says:
I recently had to purge a few projects of private issues & comments, and came up...I recently had to purge a few projects of private issues & comments, and came up with the following.
Find issues that are private to "company" (forget which table this lives in):
Find issues within a few projects which contain private comments:
Feb 14, 2007
Peter Brandström says:
Project Category to Project mapping: select tpr.pkey as 'Project Key', tpr.pnam...Project Category to Project mapping:
Source:
http://forums.atlassian.com/thread.jspa?messageID=257239191�
Feb 14, 2007
Peter Brandström says:
How do I get the text representation of the priority of an issue? I'm using Ente...How do I get the text representation of the priority of an issue?
I'm using Enterprise Jira and MySQL.
Apr 26, 2007
Ted Pietrzak says:
SELECT priority.pname FROM jiraissue INNER JOIN priority ON jiraissue.priorit...May 15, 2007
Denis Popov says:
Hello, How to get list of all Issue Type for project ? I'm using version: ...Hello,
How to get list of all Issue Type for project ?
I'm using version: 3.8.1 | edition: Enterprise. Thank you!
May 22, 2007
Peter Brandström says:
Members of a group: SELECT USERNAME FROM membershipbase WHERE GROUPNAME = <gr...Members of a group:
May 29, 2007
K Engstrom says:
What's the most efficient way of pulling a count of bugs moved into a resolved s...What's the most efficient way of pulling a count of bugs moved into a resolved state within a certain time frame?
JIRA Enterprise / PostgreSQL
Thanks.
Jun 18, 2007
Rosie Jameson says:
Could you please ask this question on the JIRA Developer Forum atCould you please ask this question on the JIRA Developer Forum at http://forums.atlassian.com ? -- many thanks
May 29, 2007
Bob Swift says:
Creates a customized view of a project that can be used when the jiraissues macr...Creates a customized view of a project that can be used when the jiraissues macro does not provide the right columns. You will need your project id to replace 12345 below. If you need to run multiple different queries, it helps to create a view in your database first to make the page sql easier. Here is an example that shows priority, votes, plan, summary, and description:
{sql:datasource=jiraDS|output=wiki|sortIcon=true} select * from project12345 {sql}This works for PostgreSQL. The concat function || may need to be changed for other databases.
Jun 13, 2007
Mel Belacel says:
Hi, How is it possible to track workflow changes (open to assign for example)? &...Hi,
How is it possible to track workflow changes (open to assign for example)?
Thanks
Dec 12, 2007
Peter Brandström says:
Textual representation of an issue´s issue type: select jiraissue.pkey, issuety...Textual representation of an issue´s issue type:
Feb 04
Peter Brandström says:
There's an error in the second paragraph "Find Fixed For versions for an issue"....There's an error in the second paragraph "Find Fixed For versions for an issue". Here is the corrected version:
Feb 15
Jamie says:
There's a couple of queries here:There's a couple of queries here: http://blogs.onresolve.com/?p=50
Average Issue Age by Priority over time, and Issue Count by Priority over time...
jamie
Apr 01
Peter Brandström says:
Enterprise Jira 3.12.2, MySQL 4.1 Each project's notification scheme: se...Enterprise Jira 3.12.2, MySQL 4.1
Each project's notification scheme:
select p.pname, ns.NAME from project p, nodeassociation n, notificationscheme ns where p.id=n.SOURCE_NODE_ID and n.SINK_NODE_ENTITY="NotificationScheme" and n.SINK_NODE_ID=ns.id;
Each project's issue type screen scheme:
select p.pname, i.NAME from project p, nodeassociation n, issuetypescreenscheme i where p.id=n.SOURCE_NODE_ID and n.SINK_NODE_ENTITY="IssueTypeScreenScheme" and n.SINK_NODE_ID=i.id;
Each project's workflow scheme, or NULL if default:
select p.pname, w.NAME from project p left join nodeassociation n on (p.id=n.SOURCE_NODE_ID and n.SINK_NODE_ENTITY="WorkflowScheme") left join workflowscheme w on (n.SINK_NODE_ID=w.id);
Each project's issue type scheme, or NULL if default:
select p.pname, f.configname from project p left join configurationcontext c on (p.id=c.project and c.customfield="issuetype") left join fieldconfigscheme f on (c.FIELDCONFIGSCHEME=f.id);