I have a bunch of WITH
statements:
;with OneAccession as (
select client_id,COUNT(patient_id) PatientCount from
(
select client_id,patient_id
from F_ACCESSION_DAILY
group by CLIENT_ID,PATIENT_ID
having COUNT(ACCESSION_ID)=1
) a
group by CLIENT_ID
)
,
TwoAccessions as (
select client_id,COUNT(patient_id) PatientCount from
(
select client_id,patient_id
from F_ACCESSION_DAILY
group by CLIENT_ID,PATIENT_ID
having COUNT(ACCESSION_ID)=2
) a
group by client_id
)
,
ThreeAccessions as (
select client_id,COUNT(patient_id) PatientCount from
(
select client_id,patient_id
from F_ACCESSION_DAILY
group by CLIENT_ID,PATIENT_ID
having COUNT(ACCESSION_ID)=3
) a
group by client_id
)
etc
And I join these statements on
select * from myTable
join OneAccession
on...
join TwoACcessions
on...
join ThreeAccessions
Instead of having all those with
statements, can i just create a stored proc? I would just pass the value of having count(accession_id)=**@myParam**
and do this:
select * from myTable
join storedproc(1)
on...
join storedproc(2)
on...
etc...
Is there an issue on joining on a stored Proc? Is my methodology OK?