我有以下查询,其中包含许多具有 7 个结果列的 LEFT JOIN 子句,其中最后两个是数字。我期望 count_non_zero 列始终等于 count_total 列(鉴于我当前拥有的数据)
WITH temp_table AS (
SELECT
attr.company_name_id AS option_id,
attr.company_name AS option_name,
uj.internship_company_name_id,
AVG(CASE WHEN s.salary > 0 THEN s.salary END) AS average,
COUNT(CASE WHEN s.salary > 0 THEN attr.company_name END) as count_non_zero,
COUNT(attr.company_name_id) as count_total
FROM company_name attr
LEFT JOIN user_job_internship uj ON uj.internship_company_name_id = attr.company_name_id
AND attr.approved_by_administrator = 1
LEFT JOIN salary_internship s ON uj.user_job_internship_id = s.user_job_id
AND uj.job_type_id = 4
LEFT JOIN [user] u ON u.user_id = uj.user_id AND u.include_in_student_site_results = 1
AND u.site_instance_id IN (1)
LEFT JOIN user_education_mba_school mba ON u.user_id = mba.user_id
AND mba.mba_graduation_year_id NOT IN (8)
GROUP BY attr.company_name_id, attr.company_name, uj.internship_company_name_id)
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY average DESC) AS row, *
FROM temp_table WHERE count_total >= 3) sub
WHERE row >= 1 AND row <= 25 ORDER BY average DESC;
我运行此查询以证明“salary”列中没有任何值返回值 0。
SELECT s.* FROM user_job_internship uj, salary_internship s
where internship_company_name_id = 440
AND uj.user_job_internship_id = s.user_job_id
我在想有什么东西会弄乱结果,导致 count_non_zero 得到不存在的计数。有人有什么想法吗?