Вопрос

Update 20 March 2014

Following Steve's advice below I came up with the following:

use JiraDB;
GO
With OrderedBugList AS
(
    SELECT  cg.issueid as "cgIssueID"
        ,cg.AUTHOR as "cgWho"
        ,cg.CREATED as "cgCreated"
        ,cg.ID as "cgID"
        ,ROW_NUMBER() OVER (PARTITION BY cg.issueid ORDER BY cg.created DESC) as RowNumber
FROM changegroup cg
where cg.CREATED < '2014-01-10'
)

select  obl.RowNumber, 
    obl.cgCreated, 
    obl.cgID, 
    obl.cgIssueID, 
    obl.cgWho,
    ji.pkey,
    ji.summary,
    ci.NEWSTRING,
    ci.OLDSTRING 
from OrderedBugList obl
LEFT OUTER JOIN jiraissue ji on ji.ID = obl.cgIssueID
LEFT OUTER JOIN changeitem ci on ci.groupid = obl.cgID
where   obl.RowNumber = 1
and     ji.issuetype = 1
and     ci.FIELD = 'Status'

It didn't give me all I want, but it did solve the problem as I described.

End Update

I'm trying to create a historical view of a project, to demonstrate a trend in bug metrics.

I am using Atlassian JIRA (v4.4.4#664-r167664) with MS SQL Server 2008 R2

I have used the standard (from Jira sample SQL Queries):

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>;

However I am finding that this is giving me the wrong result set.

I have taken to the changeitem table to try and dig for my gold. :)

SELECT cg.issueid 
  ,ji.pkey
  ,ji.SUMMARY 
  ,ci.FIELD
  ,CAST(ci.NEWSTRING AS varchar) AS NEWST
  ,CAST(ci.OLDSTRING AS varchar) AS OLDST
  ,cg.CREATED
  FROM changegroup cg
  LEFT OUTER JOIN changeitem ci on cg.ID = ci.groupid
  LEFT OUTER JOIN jiraissue ji on cg.issueid = ji.ID 
  where ci.FIELD = 'status'
  and ji.issuetype = 1
  and ji.PRIORITY = 1
  and cg.CREATED < '2012-09-15'
  order by ji.pkey, cg.created DESC, cg.issueid

My last stumbling block is to try and get only one record returned, i.e. the one record that has status in the timeframe required. The query above returns a result set that gives me something like:

  • issueid / pkey / SUMMARY / FIELD / NEWST / OLDST / CREATED
  • **32298 / QST-9693 / Bug Summary 1 / status / Closed / Resolved / 2012-09-12 17:11:46.560
  • 32298 / QST-9693 / Bug Summary 1 / status / Resolved / In Progress / 2012-09-07 17:33:24.397
  • 32298 / QST-9693 / Bug Summary 1 / status / In Progress / Open / 2012-09-07 17:32:40.623
  • **32404 / QST-9789 / Bug Summary 2 / status / Resolved / In Progress / 2012-09-13 14:41:24.080
  • 32404 / QST-9789 / Bug Summary 2 / status / In Progress / Open / 2012-09-10 17:43:07.663

I want to return only the records marked with double asterisks (**)

Pray tell how I may achieve this :)

Thanks in advance.

Это было полезно?

Решение

Do you think you can create a SQL Fiddle to make it easier to play around with this: http://sqlfiddle.com/

You should be able to achieve what you want, i.e. first of each group, by using the:

ROW_NUMBER() OVER (PARTITION BY...)

clause. A full breakdown of how to use this is here:

http://technet.microsoft.com/en-us/library/ms186734.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top