0

我正在尝试将礼物总数除以总天数。Count 语句单独工作,但是当我尝试将这两个计数相除时,报告不会运行。有任何想法吗?

((SELECT COUNT(*)
FROM ATTENDANCE
    LEFT OUTER JOIN ATTENDANCE_CODE
        ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
    LEFT OUTER JOIN PERIOD
        ON PERIOD.ID = ATTENDANCE.PERIODID
WHERE ATTENDANCE.STUDENTID = STUDENTS.ID
AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID
AND ATTENDANCE_CODE.ATT_CODE = 'LZP'
)/
(SELECT COUNT(*)
FROM ATTENDANCE
    LEFT OUTER JOIN ATTENDANCE_CODE
        ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
    LEFT OUTER JOIN PERIOD
        ON PERIOD.ID = ATTENDANCE.PERIODID
WHERE ATTENDANCE.STUDENTID = STUDENTS.ID
AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID
AND (ATTENDANCE_CODE.ATT_CODE = 'A' or ATTENDANCE_CODE.ATT_CODE = 'LZA' or ATTENDANCE_CODE.ATT_CODE = 
'LZP') 
))
4

2 回答 2

0

不要使用两个SELECT查询,而是使用一个查询,将不同的条件相加,然后除以它们。

SELECT SUM(ATTENDANCE_CODE.ATT_CODE = 'LZP') / SUM(ATTENDANCE_CODE.ATT_CODE IN ('A', 'LZA', 'LZP') AS attendance_ratio
FROM ATTENDANCE
INNER JOIN ATTENDANCE_CODE
    ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
INNER JOIN PERIOD
    ON PERIOD.ID = ATTENDANCE.PERIODID
WHERE ATTENDANCE.STUDENTID = STUDENTS.ID
AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID

此外,看起来您不应该在LEFT OUTER JOIN这里使用。ATTENDANCE_CODEID并且PERIODID似乎是外键,它们应该始终在父表中找到匹配项。

于 2020-09-18T16:50:26.803 回答
0

你需要select那个除法的结果。NULLIF()在分母中使用以避免潜在的零误差除法也是一个好主意。

SELECT
    (SELECT COUNT(*)
    FROM ATTENDANCE
        LEFT OUTER JOIN ATTENDANCE_CODE
            ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
        LEFT OUTER JOIN PERIOD
            ON PERIOD.ID = ATTENDANCE.PERIODID
    WHERE ATTENDANCE.STUDENTID = STUDENTS.ID
    AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
    AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
    AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
    AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID
    AND ATTENDANCE_CODE.ATT_CODE = 'LZP'
    )/
    NULLIF((SELECT COUNT(*)
    FROM ATTENDANCE
        LEFT OUTER JOIN ATTENDANCE_CODE
            ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
        LEFT OUTER JOIN PERIOD
            ON PERIOD.ID = ATTENDANCE.PERIODID
    WHERE ATTENDANCE.STUDENTID = STUDENTS.ID
    AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
    AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
    AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
    AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID
    AND (ATTENDANCE_CODE.ATT_CODE = 'A' or ATTENDANCE_CODE.ATT_CODE = 'LZA' or ATTENDANCE_CODE.ATT_CODE = 
    'LZP') 
    ), 0)

查看两个非常相似的子查询,我想知道整个事情是否可以简化为:

SELECT AVG(ATTENDANCE_CODE.ATT_CODE = 'LZP')
FROM ATTENDANCE
LEFT OUTER JOIN ATTENDANCE_CODE
    ON ATTENDANCE_CODE.ID = ATTENDANCE.ATTENDANCE_CODEID
LEFT OUTER JOIN PERIOD
    ON PERIOD.ID = ATTENDANCE.PERIODID
WHERE 
    ATTENDANCE.STUDENTID = STUDENTS.ID
    AND ATTENDANCE.ATT_DATE BETWEEN '%param1%' AND '%param2%'
    AND PERIOD.PERIOD_NUMBER BETWEEN '1' AND '8'
    AND ATTENDANCE.ATT_MODE_CODE = 'ATT_ModeMeeting'
    AND ATTENDANCE.SCHOOLID = STUDENTS.SCHOOLID
    AND ATTENDANCE_CODE.ATT_CODE IN ('LZP', 'LZA', 'A')
于 2020-09-18T16:47:44.507 回答