Reputation: 10476
I have the following query with many LEFT JOIN clauses that has 7 result columns, the last two of which are numbers. I'm expecting the count_non_zero column to always be equal to the count_total column (given the data I current have)
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;
I run this query to prove that no values in the 'salary' column are returning a value of 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
I'm thinking there is something that messes up the results that is causing the count_non_zero to get counts that do not exist. Anyone have anythoughts?
Upvotes: 1
Views: 444
Reputation: 130819
I am assuming your count_total is greater than your count_non_zero. That is to be expected because you are using outer join to join user_job_internship and salary_internship.
Your query is including companies that do not have any internships. A company will not be included in the count_non_zero if either the salary is 0 or if there is no internship at all.
Change those two joins to inner joins and you should get your expected result.
The other option is to change your count_total to ignore companies that haven't any internship
count(case when s.user_job_id is not null then attr.company_name_id end) as count_total
You have one other slight risk. Your count_non_zero is counting company_name whereas your count_total is counting company_name_id. You could have problems if the company_name column allows NULL values.
Upvotes: 2