使用GROUP BY的3个SELECT语句的AVG

时间:2015-01-29 23:27:32

标签: sql oracle

我试图做以下事情

我有一个表格QA,其中包含以下内容:

TICKET_ID   SKILL_ID  SCORE         USER
###############################################
1           10          15          USER1
1           20           5          USER1
1           30          95          USER1
2           40          20          USER1
2           50          40          USER1
3           60          70          USER1
3           70          15          USER1

表格SKILLS由:

组成
SKILL_ID   SKILL_NAME   AREA_ID
   10       SKILL1        1
   20       SKILL2        1
   30       SKILL3        2
   40       SKILL4        2
   50       SKILL5        2
   60       SKILL6        3
   70       SKILL7        3

和表格TICKETS由:

组成
  TICKET_ID   TICKET_NUMBER   
    1           AAA
    2           BBB
    3           CCC

QA使用TICKET_ID向TICKETS使用FK,使用SKILL_ID使用FK到SKILLS

我需要做的是:

表格中的每张票QA检查每个区域的得分总和是否超过100,如果超过0,则将3个结果的AVG和另一个表中的ticket_number分组 因此,对于每种情况,我都需要计算每个区域的得分,如果> 100然后0否则保留原始值,然后使3个值的平均值和票号

分组

不确定这是否可行

所以也许计算area_1的分数我做了以下几点:

SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 1) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1

计算area_2相同的得分但改变WHERE条件(此时WHERE AREA_ID = 2):

SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 2) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1

并计算area_3得分相同但改变WHERE条件(WHERE AREA_ID = 3):

SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 3) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1

这个块中的每一个都给出1个值作为输出

我想要实现的是通过ticket_number输出3块AVG

我试着总结3个街区,但它不允许我:

SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 1) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1
+
SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 2) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1
+
SELECT DECODE(100 - SUM(SCORE),
100,100,
95,95,
90,90,
85,85,
80,80,
75,75,
70,70,
65,65,
60,60,
55,55,
50,50,
45,45,
40,40,
35,35,
30,30,
25,25,
20,20,
15,15,
10,10,
5,5,
0) SCORE_A1 FROM QA WHERE SKILL_ID IN(SELECT SKILL_ID FROM SKILLS WHERE AREA_ID = 3) AND TICKET_NUMBER = :P2_TICKET_NUMBER AND QA.USER = :P2_USER GROUP BY 1

谢谢

2 个答案:

答案 0 :(得分:4)

请尝试以下查询:

SELECT
    AREAS_SCORES.USER,
    AREAS_SCORES.TICKET_ID,
    AREAS_SCORES.TICKET_NUMBER,
    AVG(CASE 
            WHEN AREAS_SCORES.AREA_SUM_SCORE > 100 THEN 0
            ELSE AREAS_SCORES.AREA_SUM_SCORE
        END) AVG_SCORE
FROM
(
    SELECT
        QA.USER,
        QA.TICKET_ID,
        QA.TICKET_NUMBER,
        SKILLS.AREA_ID,
        SUM(QA.SCORE) AREA_SUM_SCORE
    FROM 
        QA 
        INNER JOIN SKILLS ON SKILLS.SKILL_ID = QA.SKILL_ID
        INNER JOIN TICKETS ON TICKETS.TICKET_ID = QA.TICKET_ID
    GROUP BY
        QA.USER,
        QA.TICKET_ID,
        QA.TICKET_NUMBER,
        SKILLS.AREA_ID
) AREAS_SCORES

子查询为每个用户的每个票证计算每个区域内的得分总和。然后再次汇总信息以计算平均值,但限制条件是当特定区域的得分总和超过100时,则应将其计为0

我希望它有所帮助(假设我理解你的问题)。

答案 1 :(得分:2)

您可以通过一次选择完成所有操作。您必须将SUMCASE合并,如下所示:

SELECT T.TICKET_NUMBER,
SUM(CASE WHEN AREA_ID = 1 THEN QA.SCORE ELSE 0 END) SCORE_A1,
SUM(CASE WHEN AREA_ID = 2 THEN QA.SCORE ELSE 0 END) SCORE_A2,
SUM(CASE WHEN AREA_ID = 3 THEN QA.SCORE ELSE 0 END) SCORE_A3
FROM QA INNER JOIN SKILLS S ON QA.SKILL_ID = S.SKILL_ID
INNER JOIN TICKETS T ON QA.TICKET_ID = T.TICKET_ID
WHERE QA.USER = :P2_USER GROUP BY T.TICKET_NUMBER

现在,要应用其他条件以确保SUM不超过100,请使用外部查询:

SELECT TICKET_NUMBER,
CASE WHEN SCORE_A1 > 100 THEN 0 ELSE SCORE_A1 END SCORE_A1,
CASE WHEN SCORE_A2 > 100 THEN 0 ELSE SCORE_A2 END SCORE_A2,
CASE WHEN SCORE_A3 > 100 THEN 0 ELSE SCORE_A3 END SCORE_A3
FROM 
   (
    SELECT T.TICKET_NUMBER,
    SUM(CASE WHEN AREA_ID = 1 THEN QA.SCORE ELSE 0 END) SCORE_A1,
    SUM(CASE WHEN AREA_ID = 2 THEN QA.SCORE ELSE 0 END) SCORE_A2,
    SUM(CASE WHEN AREA_ID = 3 THEN QA.SCORE ELSE 0 END) SCORE_A3
    FROM QA INNER JOIN SKILLS S ON QA.SKILL_ID = S.SKILL_ID
    INNER JOIN TICKETS T ON QA.TICKET_ID = T.TICKET_ID
    WHERE QA.USER = :P2_USER GROUP BY T.TICKET_NUMBER
   ) TIX

最后,如果您需要这三个分数的平均值,请使用另一个级别的外部查询:

SELECT TICKET_NUMBER,
SCORE_A1,
SCORE_A2,
SCORE_A3,
(SCORE_A1+SCORE_A2+SCORE_A3)/3 AVG_SCORE
FROM
   (
    SELECT TICKET_NUMBER,
    CASE WHEN SCORE_A1 > 100 THEN 0 ELSE SCORE_A1 END SCORE_A1,
    CASE WHEN SCORE_A2 > 100 THEN 0 ELSE SCORE_A2 END SCORE_A2,
    CASE WHEN SCORE_A3 > 100 THEN 0 ELSE SCORE_A3 END SCORE_A3
    FROM 
       (
        SELECT T.TICKET_NUMBER,
        SUM(CASE WHEN AREA_ID = 1 THEN QA.SCORE ELSE 0 END) SCORE_A1,
        SUM(CASE WHEN AREA_ID = 2 THEN QA.SCORE ELSE 0 END) SCORE_A2,
        SUM(CASE WHEN AREA_ID = 3 THEN QA.SCORE ELSE 0 END) SCORE_A3
        FROM QA INNER JOIN SKILLS S ON QA.SKILL_ID = S.SKILL_ID
        INNER JOIN TICKETS T ON QA.TICKET_ID = T.TICKET_ID
        WHERE QA.USER = :P2_USER GROUP BY T.TICKET_NUMBER
       ) TIX
   ) MORE_TIX