when joining tables, how to get the max value from same column in the 2 joined tables

  • Companies in my db have an ad quota.

    Some companies (not all) have a parent company

    The companies that have a parent company have no ad quaota themselves, but use the ad quota of their parents.

    The companies w/o a parent company have an ad quaota themselves

    Now, with the following statement I'm able to get the adquaota if a user's company has a parent.

    But as soon as the user's company DOESNT have a parent I get NULL for remainingads...why and how to fix this?

    select c.title,

    case when c.remainingads>pc.remainingads then c.remainingads else pc.remainingads end

    as remainingads

    FROM companies c

    inner join aspnet_Users au on au.companyid=c.id

    left join companies pc on pc.id=c.parentcompanyid

    where au.UserName='johndoe'

  • This was removed by the editor as SPAM

  • Ah, I see, perfect, it now works! Thanks! 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply