2017-08-01 4 views
-1

最初のStackoverflowの質問 - だから私に簡単に行く:)。Summingカスタム列

こんにちは、私は書いた次のクエリを使用して "Attributed"としてアイテムにフラグを立てようとしています。本質的に、患者IDにPERSON_PROVIDER_RELATIONSHIPフラグがある場合、その患者IDには1が与えられます。彼らは別のタイプのフラグを持っている場合(あなたが受け取ることができる他の可能なフラグが2つあります)。すべてがうまく行きます(PERSON_PROVIDER_RELATIONSHIPのインスタンスに「1」を割​​り当てる)が、作成したカスタム列(「属性」)を合計しようとすると、このエラーが発生します(列「属性」は存在しません)。集計を行う別の列を作成しようとするか、最後に "having"節を追加するかどうかにかかわらず、このエラーが発生します。ここでは、合計が> 0のレコードのみを表示します。どんな助けもここにありがとう!私はこれを書くためにMySQLを使用しており、明確な情報を提供することを嬉しく思っています。

select distinct c.empi_id as "Patient", 
c.incurred_from_date as "Service Date", 
(case when c.billing_organization_source_id IN ('xxxx','yyyy') then 1 else 0 
end) as "In-Network Indicator", 
(case when t.ref_record_type = 'PERSON_PROVIDER_RELATIONSHIP' then 1 else 0 
end) as "Attribution", 
(sum("Attribution") over (partition by c.empi_id)) as "Attribution Flag", 
p.cleanprovidername as "Provider", t.ref_record_type 
from ph_f_annotated_claim c 
left outer join PH_F_Attribution_Component_Data_Point t 
on t.empi_id = c.empi_id and t.population_id = c.population_id 
inner join ph_d_personnel_alias a 
on a.prsnl_id = t.prsnl_id 
inner join xxxx_xxxx_xxxx_xxxx p 
on a.prsnl_alias_id = p.NPI 
where (c.bill_type_code like '33%' 
or c.bill_type_code like '32%' 
or c.bill_type_code like '033%' 
or c.bill_type_code like '032%') 
and c.source_description = 'MSSP Claims' 
and c.incurred_from_date >= '2015-12-01' 
and c.incurred_from_date <= '2017-01-31' 
and c.population_id = '2feb2cb1-be55-4827-a21f-4e2ef1a40340' 
and p.DegreeName IN ('MD','DO') 
and a.prsnl_alias_type = 'NPI' 
and p.PrimaryPHO = 'Yes' 
group by c.empi_id, c.incurred_from_date, c.billing_organization_source_id, 
p.cleanprovidername, t.ref_record_type 
+0

はhttps://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-meを参照してください。 -a-be-be-a-very-simple-sql-query – Strawberry

+0

そして、使用しているRDBMSを明確にしてください。 – Strawberry

答えて

1

同じSELECT句のエイリアス化された列名を式として使用することはできません。あなたはこのような何かをする必要があります:

sum(case when t.ref_record_type = 'PERSON_PROVIDER_RELATIONSHIP' then 1 else 0 end) over (partition by c.empi_id) as "Attribution Flag" 
+0

ありがとう!エイリアス化された列の合計に基づいて "having"句を使用してレコードを除外する方法はありますか?あなたが言ったことは、魅力のように機能しますが、 "having(sum(" Attribution Flag ")> 0"を返すと同じ "Column" Attribution "does not exist"というエラーを返します。 –

+0

HAVING '句:エイリアス名の代わりに式全体を使用してください。ある時点で、この式をサブクエリまたはCTEに入れる方が簡単になり、メインクエリの列名だけで*を参照できます。 –