2016-08-18 3 views
-2

次は私のクエリです。クエリの各患者の医師の診察回数を取得する必要があります。カウントは正しくなく、患者ごとに2行ずつ表示されます。T-SQLのサブクエリを使用してカウントする

SELECT 
    pf.PatientId 
    , p.Visit 
    , pf.first 
    , pf.last 
    , df.first 
    , df.last 
    , doc.reconcile_status 
    , doc.orderid 
    , count(p.visit) 


    FROM [CentricityPS].[dbo].[PatientVisit] p 
    , [CentricityPS].[dbo].[document] doc 
    , [CentricityPS].[dbo].[Patientprofile] pf 
    , [CentricityPS].[dbo].[doctorfacility] df 

    where df.pvid in ('1507023132004420', '1725527248154950', '1406648461000690') 
    and p.doctorid = df.DoctorFacilityId 
    and p.patientprofileid = pf.patientprofileid 
    and pf.pid = doc.pid 
    and pf.patientstatusmid = '-900' 
    and pf.PatientProfileId = p.PatientProfileId 
-- and pf.PatientId = '8145' 
    -- and p.visit >= '2016-01-01' and p.visit <= '2016-07-01' 
    and not exists (select * from [CentricityPS].[dbo].[PatientVisit] p 
    where (p.visit > '2013-01-01' and p.visit < = '2016-01-01') 

    and p.patientprofileid = pf.patientprofileid and pf.patientstatusmid not in (-901) ) 
     and not exists (select * from [CentricityPS].[dbo].[PatientVisit] p 
    where p.visit <= '2013-01-01' 
    and p.patientprofileid = pf.patientprofileid and pf.patientstatusmid not in (-901)) 
--  and pf.patientid = '100293' 
group by df.DoctorFacilityId, pf.PatientId, p.visit, pf.first, pf.last, df.first, df.last, doc.RECONCILE_STATUS, doc.ORDERID, p.PatientProfileId 
order by df.doctorfacilityid, pf.patientid, p.visit desc 

私は間違っていますか?

ヘルプ!!!

+1

基本的な構文エラー以外は、サンプルデータとテーブル構造を伴わずに、問題を把握するためのクエリは、それ自体ではUSELESSです。 –

+0

これをチェックしてください[質問を改善する](https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) – TheGameiswar

答えて

0

あまりにも多くのフィールドをグループ化しています。各患者の診察回数が必要な場合は、SELECT PatientProfileフィールドとcount(p.visit)フィールドだけを使用し、同じPatientProfileフィールドをGROUP BYに含めます。

関連する問題