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 7502327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:49:43+00:00 2026-05-29T20:49:43+00:00

Here’s a nasty one. I’m trying to reference a table in an outer query

  • 0

Here’s a nasty one. I’m trying to reference a table in an outer query from an inner select nested in a join. I need to filter these inner queries on values from the outer sales_contract table, but I receive the ‘multi-part identifier could not be bound’ error when I try to reference any of these columns in the inner queries:

sc.commission_range_start
sc.commission_range_end
sc.commission_duration
sc.signup_range_start
sc.signup_range_end

How can I restructure this query?

with account_hierarchy(account_major, account_minor) as
(
    select
        gp.account_major as account_major,
        gp.account_major as account_minor
    from
        group_accounts gp with(nolock)
        left join group_accounts gc with(nolock) on gp.account_major = gc.account_minor
    where
        gc.account_major is null
    group by
        gp.account_major

    union all

    select 
        up.account_major, down.account_minor
    from 
        group_accounts as down with(nolock)
        join account_hierarchy as up on up.account_minor = down.account_major
)
select
    sa.first_name as 'AgentFirstName',
    sa.last_name as 'AgentLastName',
    sc.threshold as 'CommissionUsageThreshold',
    sc.commission_amount as 'CommissionAmount',
    sc.commission_percentage as 'CommissionPercentage',
    parent.primary_phone as 'AccountNumber',
    child.primary_phone as 'ChildAccountNumber',
    coalesce(total_credit.amount, 0) as 'CreditTotal',
    coalesce(total_billing.amount, 0) as 'BillingTotal'
from
    sales_contract sc
    inner join sales_agent sa on sc.sales_agent_id = sa.id
    inner join sales_distributor sd on sa.distributor_id = sd.id
    inner join demographic parent on sc.primary_phone = parent.primary_phone
    inner join account_hierarchy ah on parent.primary_phone = ah.account_major
    inner join demographic child on ah.account_minor = child.primary_phone
    inner join
    (
        select
            d.primary_phone
        from
            demographic d
        where
            (d.active_date >= sc.signup_range_start or sc.signup_range_start is null) and
            (d.active_date < sc.signup_range_end or sc.signup_range_end is null)
    ) commission on
        child.primary_phone = commission.primary_phone
    left outer join
    (
        select
            j.primary_phone, sum(j.billed_amount) as amount
        from
            jobs_complete j
        where
            j.time_proofed >= sc.commission_range_start
            and (j.time_proofed < dateadd(month, sc.commission_duration, sc.commission_range_start))
            and (j.time_proofed < sc.commission_range_end or sc.commission_range_end is null)
        group by
            j.primary_phone
    ) total_billing on
        commission.primary_phone = total_billing.primary_phone
    left outer join
    (
        select
            c.primary_phone, sum(c.amount) as amount
        from
            credit c
        group by
            c.primary_phone
    ) total_credit on
        commission.primary_phone = total_credit.primary_phone
where
    sd.email = @DistributorEmail
  • 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-05-29T20:49:44+00:00Added an answer on May 29, 2026 at 8:49 pm

    I think that you need to change the JOINs where you are referencing the outer tables for CROSS APPLY and OUTER APPLY. Try this:

    with account_hierarchy(account_major, account_minor) as
    (
        select
            gp.account_major as account_major,
            gp.account_major as account_minor
        from
            group_accounts gp with(nolock)
            left join group_accounts gc with(nolock) on gp.account_major = gc.account_minor
        where
            gc.account_major is null
        group by
            gp.account_major
    
        union all
    
        select 
            up.account_major, down.account_minor
        from 
            group_accounts as down with(nolock)
            join account_hierarchy as up on up.account_minor = down.account_major
    )
    select
        sa.first_name as 'AgentFirstName',
        sa.last_name as 'AgentLastName',
        sc.threshold as 'CommissionUsageThreshold',
        sc.commission_amount as 'CommissionAmount',
        sc.commission_percentage as 'CommissionPercentage',
        parent.primary_phone as 'AccountNumber',
        child.primary_phone as 'ChildAccountNumber',
        coalesce(total_credit.amount, 0) as 'CreditTotal',
        coalesce(total_billing.amount, 0) as 'BillingTotal'
    from
        sales_contract sc
        inner join sales_agent sa on sc.sales_agent_id = sa.id
        inner join sales_distributor sd on sa.distributor_id = sd.id
        inner join demographic parent on sc.primary_phone = parent.primary_phone
        inner join account_hierarchy ah on parent.primary_phone = ah.account_major
        inner join demographic child on ah.account_minor = child.primary_phone
        cross apply
        (
            select
                d.primary_phone
            from
                demographic d
            where
                (d.active_date >= sc.signup_range_start or sc.signup_range_start is null) and
                (d.active_date < sc.signup_range_end or sc.signup_range_end is null) and
                d.primary_phone = child.primary_phone
        ) commission 
        outer apply
        (
            select
                j.primary_phone, sum(j.billed_amount) as amount
            from
                jobs_complete j
            where
                j.time_proofed >= sc.commission_range_start
                and (j.time_proofed < dateadd(month, sc.commission_duration, sc.commission_range_start))
                and (j.time_proofed < sc.commission_range_end or sc.commission_range_end is null)
                and j.primary_phone = child.primary_phone
            group by
                j.primary_phone
        ) total_billing
        left outer join
        (
            select
                c.primary_phone, sum(c.amount) as amount
            from
                credit c
            group by
                c.primary_phone
        ) total_credit on
            commission.primary_phone = total_credit.primary_phone
    where
        sd.email = @DistributorEmail
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here's a dump of the stats provided my mod_pagespeed from one of my sites.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is the issue I am having: I have a large query that needs
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is a table CarID| Attribute | Value 1 | Color | Red 2
Here's my scenario. I've got SharePoint 2010 running on one server, with an SQL
Here is the problem I'm trying to solve for my game. I have this

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.