0

我有以下表格问题,问题状态和自定义表(CT)。以下是相同的数据

Issue:                       Issuestatus:                    CT

Issue|pkey|issuestatus       id | pname                     Issue|Referred
----------------------------------------------------------------------------
100     T-1  1                  1   Open                       100     NULL
200     T-2  2                  2   Closed                     200     NULL 
300     T-3  3                  3   Acknowledged               700     Dev
400     T-4  4                  4   In Progress                800     QA
500     T-5  1                  5   Referred                   800     NULL 
600     T-6  2                                                 400     NULL
700     T-7  5                                                 500     NULL   
800     T-8  5                                                 700     NULL

我需要如下输出

pname      |  Count
Open           2
Closed         2
Acknowledged   1
In Progress    1
Dev            1 
QA             1

因此,如果您看到,需要分组issuestatus并获取 的计数pkey,但是当issuestatus“推荐”时,您需要查看 CT 表并获取分组依据中相应的推荐列文本。CT 中的问题链接到问题表中的问题。

这是我尝试过的,但不是正确的输出

select pname = case 
    when pname='Referred' then CT.Referred 
    else pname end,
    COUNT(pkey) 
from  CT,issue a,issuestatus
where a.issuestatus=issuestatus.id and a.issue=CT.ISSUE 
group by pname,CT.Referred
4

3 回答 3

2

我测试了以下查询,给出了你想要的

试试看

WITH T1 AS (
  SELECT (CASE WHEN pname='Referred' THEN Referred ELSE pname END)AS [pn]
  FROM (SELECT i.Issue,c.Referred,iss.pname
        FROM Issue i
        INNER JOIN IssueStatus iss ON i.issuestatus=iss.id
        LEFT OUTER JOIN CT c ON c.Issue=i.Issue
       ) t
)


SELECT pn,count(*) AS cnt
FROM T1
WHERE pn IS NOT NULL
GROUP BY pn
ORDER BY cnt DESC

这是SQLFiddle

于 2013-09-04T07:06:06.217 回答
1

尝试这个

 SELECT pname, COUNT(pname)
    FROM (SELECT (CASE WHEN Pname='Referred'  THEN CT.referred else st.pname END)AS pname
            FROM Issue a JOIN IssueStatus st
            ON a.issuestatus = st.id LEFT OUTER JOIN CT
            ON a.Issue = CT.Issue) tbl
    GROUP BY pname
    HAVING pname IS NOT NULL;

这是SQLFiddle

于 2013-09-04T06:41:09.560 回答
0

尝试这个。没有测试它,因为没有测试数据的脚本。

   select case when pname='Referred' then CT.Referred else pname end pname, COUNT(*) cnt
    from  CT,issue a,issuestatus
    where a.issuestatus=issuestatus.id and a.issue=CT.ISSUE 
    group by case when pname='Referred' then CT.Referred else pname end
于 2013-09-04T06:23:36.817 回答