2011-08-18 80 views
1

我有以下情形使用SQL語句, 實現我分配到組分流,GX,GY一個問題:問題SQL語句的結果

組分流 - >組GX

組GX - >組分流

組分流 - >組GY

我想只提取第一次,當我的問題是分配到分診組而忽略其他,我想這樣做但總是不好的結果(回顧所有行當分配到分診組問題)

SQL語句:

select g.created ,ji.pkey as issueName 
    from changegroup g 
    join changeitem ci on (ci.groupid = g.id) 
    join jiraissue ji on (ji.id = g.issueid) 
    join project p on (p.id = ji.project) 
    join priority pr on (pr.id = ji.priority) 
    where ci.field = 'Group' 
    and ci.oldString = 'Triage' 
    and p.pname = 'Test' 
    and pr.pname='P3' 
    and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00' 

語句的結果(正如你看到的,因此請求檢索2行中的問題200被分配到組分流2次):

enter image description here

表changegroup具有這種結構:

enter image description here

表changeitem具有這樣的結構: enter image description here

回答

0

試試這一個。但它可以很慢查詢

select g.created, ji.pkey as issueName 
from changegroup g 
join jiraissue ji on (ji.id = g.issueid) 
where (g.created, issueName) in (
select min(g.created) ,ji.pkey as issueName 
from changegroup g 
join changeitem ci on (ci.groupid = g.id) 
join jiraissue ji on (ji.id = g.issueid) 
join project p on (p.id = ji.project) 
join priority pr on (pr.id = ji.priority) 
where ci.field = 'Group' 
and ci.oldString = 'Triage' 
and p.pname = 'Test' 
and pr.pname='P3' 
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00' 
group by issueName 
)