0

这是原始查询:,它生成带有两列名称(标签,计数)的o / p:

label      count 
Fails      1
Pass       3

这是我写的生成上述数据的查询

select r.pname as resolution
,lb.label
,count(r.pname) as occurences
from issuelink
inner join jiraissue p on issuelink.source = p.id
inner join jiraissue c on issuelink.destination = c.id
inner join issuelinktype t on issuelink.linktype = t.id
inner join resolution r on c.resolution = r.id
inner join issuestatus istat on p.issuestatus = istat.sequence
inner join label lb on c.id = lb.issue
where t.linkname = 'jira_subtask_link'
and p.pkey in (@pkey)
and c.issuetype in ('13')
and r.pname not in ('Fails Smoke Test - General Failure', 'Fails Smoke Test - New Firmware Available')
and label in ('SmokeTest', 'fullcert', 'bfv', 'papercert')
group by r.pname;

根据您的建议,我尝试使用交叉函数,但是它引发了“请检查语法”的错误:

select  100.0 * fail.cnt / (fail.cnt + pass.cnt) as c
from    (
        select  r.pname as label
        ,       count(r.pname) as cnt
        from issuelink
        inner join jiraissue p on issuelink.source = p.id
        inner join jiraissue c on issuelink.destination = c.id
        inner join issuelinktype t on issuelink.linktype = t.id
        inner join resolution r on c.resolution = r.id
        inner join issuestatus istat on p.issuestatus = istat.sequence
        inner join label lb on c.id = lb.issue
        where t.linkname = 'jira_subtask_link'
        and p.pkey in ('SRPDTVTPV-3')
        and c.issuetype in ('13')
        and r.pname not in ('Fails Smoke Test - General Failure', 'Fails Smoke Test - New Firmware Available')
        and label in ('SmokeTest', 'fullcert', 'bfv', 'papercert')
        group by r.pname
        ) as fail
cross join    
        (
        select  r.pname as label
        ,       count(r.pname) as cnt
        from issuelink
        inner join jiraissue p on issuelink.source = p.id
        inner join jiraissue c on issuelink.destination = c.id
        inner join issuelinktype t on issuelink.linktype = t.id
        inner join resolution r on c.resolution = r.id
        inner join issuestatus istat on p.issuestatus = istat.sequence
        inner join label lb on c.id = lb.issue
        where t.linkname = 'jira_subtask_link'
        and p.pkey in ('SRPDTVTPV-3')
        and c.issuetype in ('13')
        and r.pname not in ('Fails Smoke Test - General Failure', 'Fails Smoke Test - New Firmware Available')
        and label in ('SmokeTest', 'fullcert', 'bfv', 'papercert')

        group by r.pname
        ) as pass
where   fail.label = 'Fails Certification'
        and pass.label = 'Passes Certification'
4

1 回答 1

2

这是使用 a 的一种方法cross join

select  100.0 * a.count / (a.count + b.count) as c
from    YourQuery a
cross join    
        YourQuery b
where   a.label = 'a'
        and b.label = 'b'

这是另一种使用聚合的方法,有点棘手,但只使用一次 YourQuery:

select  100.0 * cnt_a / (cnt_a + cnt_b) as c
from    (
        select  max(case when label = 'a' then count end) as cnt_a
        ,       max(case when label = 'b' then count end) as cnt_b
        from    YourQuery
        ) as SubQueryAlias
于 2012-06-29T00:32:50.667 回答