Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:30:28+00:00 2026-06-06T18:30:28+00:00

I have the following query with many LEFT JOIN clauses that has 7 result

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:30:29+00:00Added an answer on June 6, 2026 at 6:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following query which take more than a minute to execute, how can
Following on from my last question Sql Server query performance , and discovering that
I have a query that looks like the following: select c.[Name], c.Description, c.ID, cd.StartDateTime
I have the following HQL query which works fine, however it returns a list
I have two tables in MySQL that I'm comparing with the following attributes: tbl_fac
I have the following query, which I designed to compile data from a number
I have two tables that contain the following information models id manufactor 1 Mercedes
I have the following query which checks goods receipts against purchase orders to see
I have the following query that returns test questions, possible answers to those questions
My database has following structure: Every Region ( CountryStates ) can have many cities

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.