Including NULL results after join

允我心安 提交于 2019-12-25 04:07:11

问题


I am trying to do a report which shows all payments we have received and for the report I have to show names of patients who pay, but this table also contains checks from payers (insurance companies) and after I do a join all of the payers are excluded. I have tried every join version I know left, right, outer, inner, and combinations of the two. SQL Server 2005.

UPDATE

The line that is causing the left join not to work is

select p.*, max(episode_id) over (partition by patient_id) as maxei from patient p

which I am using in the join. Our system uses an episodic system, so if a patient leaves and comes back they get a new episode. The payment table does not have an episode field though, so I used that line of code in the join to only show the current episode. Any idea how I can keep it only showing the current episode while not losing the fields without patient_id's? Examples of the fields without patients id's are shown in the second image.

select 

pay.patient_id,
p.lname + ', ' + p.fname as 'Name',
pay.source_type,
pay.instrument,
pay.doc_reference,
pay.instrument_date,
pay.payment_amount,
pay.user_id,
pay.entry_chron,
pay.payor_id


from payment pay
join (select p.*, max(episode_id) over (partition by patient_id) as maxei from patient p) p
on p.patient_id = pay.patient_id

where episode_id = maxei and (pay.instrument_date between '2014-11-01' and '2014-11-30')
order by pay.payment_amount

This is what the results look like for patients with some fields commented out for confidentiality.

These are the fields that are being excluded

来源:https://stackoverflow.com/questions/27806899/including-null-results-after-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!