Sql:如何使用公式计算表值

时间:2012-06-29 00:30:29

标签: sql mysqli

这是原始查询:它生成带有两列名称(标签,计数)的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'

1 个答案:

答案 0 :(得分:2)

这是使用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