2012-02-14 12 views
4

ここでは厄介なことです。私は、結合でネストされた内部選択から外部クエリのテーブルを参照しようとしています。私は、外sales_contractテーブルから値上でこれらの内側のクエリをフィルタリングする必要があるが、私は内側のクエリでこれらの列のいずれかを参照しようとすると、エラー「マルチパート識別子はバインドできませんでした」受信:ジョインでのネストされた選択:マルチパートの識別子をバインドできませんでした

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

このクエリはどのように再構成できますか?

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 
+0

これらの列は見つからないのですか? – Lamak

+0

申し訳ありませんが、それを含める必要があります。私の投稿を編集して追加しました。 – Donald

答えて

3

私はあなたがCROSS APPLYOUTER APPLYのための外側のテーブルを参照しているJOIN Sを変更する必要があると思います。これを試してみてください:

+1

これは興味深いです。私は今までにも適用されることは聞いたことがありません。構文はややこしいですが、うまくいきました。 apply文のwhere句の中で "on"句を移動しなければならなかったが、正しい結果が得られているようだ。新しいことを教えてくれてありがとう! – Donald

+1

@Donald - あなたは絶対に正しいです、私は 'ON'節を削除するのを忘れました。私は今クエリを変更したので、正常に動作するはずです – Lamak

1

サブクエリをビューに移動してビューに参加させてみることもできます。

select ... 
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 vwCommission on child.primary_phone = vwCommission.primary_phone 
     and ... 
    left outer join vwTotal_Billing on commission.primary_phone = vwTotal_Billing.primary_phone 
     and ... 
    left outer join vwTotal_credit on commission.primary_phone = vwTotal_credit.primary_phone 
     and ... 
where ... 
+0

通常、これは良い解決策になると思いますが、これを試してみますが、このクエリはSRSSレポート内に存在するため、完全に自己完結型であることを望みます。 – Donald

+0

これは分かります。同じ考え方はサブクエリにも適用できます。フィルタリングする列も選択し、フィルタをサブクエリから結合に移動します。 –

関連する問題