Transact SQL - Help with IF statement to return values

I need help with the below mentioned TSQL query.

If I run this code, I get back two records:

Snipaste_2019-09-19_10-20-00

When I run the below query, I only get back line 1 from the above-mentioned query and this is 100% accurate. What I need help with is in my IF block I have commented out below. My client has asked to return the other Procedures with a toggle control (at her discretion) that would return the other codes from that patient visit. I need help with the logic to bring back the other procedures from the PatientVisit. If I have not explained well enough, let me know and I can elaborate more. Thanks kindly in advance.

DECLARE @STARTDATE DATETIME = '08/26/2019'
, @ENDDATE DATETIME = '09/18/2019'
, @DATETYPE VARCHAR(20) = 'Batch Entry'

CREATE TABLE #Results
(
PatientVisitId INT
, PatientVisitProcsId INT
, CPTCode VARCHAR(10)
)

INSERT INTO #Results

SELECT
pv.PatientVisitId
, pvp.PatientVisitProcsId
, pvp.CPTCode

FROM cusFinancialAggregates cfa
INNER JOIN PatientVisit pv ON cfa.PatientVisitId = pv.PatientVisitId
LEFT JOIN PatientVisitProcs pvp ON cfa.PatientVisitProcsId = pvp.PatientVisitProcsId
INNER JOIN CusActionType_VR pmt ON cfa.ActionTypeMId = pmt.ActionTypeId AND pmt.ActionName NOT LIKE '%Transfer%'
WHERE
cfa.TransActionType = 'Pmts'
AND cfa.InsuranceAmount + cfa.PatientAmount <> 0
AND pmt.ActionName NOT LIKE '%Transfer%'
AND @DATETYPE = 'Batch Entry' AND cfa.EventDate >= ISNULL(@STARTDATE,'1/1/1900') AND cfa.EventDate < DATEADD ( D , 1 , ISNULL(@ENDDATE,'1/1/3000'))
AND cfa.PatientVisitId = 222728

/*
IF @CPT = 1
BEGIN

---- Grab the other Procedure that was NOT found in the Insert into #Results 

ELSE 

---- Do nothing and keep as it is

END

*/

SELECT * FROM #Results

DROP TABLE #Results

hi

first we need to see why it ( in select part ) filters out of two cptcodes into one
what is it !!!

second ... what can we change ( in select part ) that filters two cptcodes into one part to give the other cptcode

without access to your database and data and business logic is tough ...
doing the SQL is very easy !!!!

maybe something like this

-------------------------------------------------------------
DROP TABLE data 
go 

CREATE TABLE data 
  ( 
     patientvisitid      INT, 
     patientvisitprocsid INT, 
     cptcode             VARCHAR(10) 
  ) 
go 

INSERT INTO data 
SELECT 222728, 
       435243, 
       99214 

INSERT INTO data 
SELECT 222728, 
       435244, 
       99401 

go 

SELECT * 
FROM   data 

go 

patientvisitid	patientvisitprocsid	cptcode
222728			435243				99214
222728			435244				99401


------------------------------------------------------------------
-- @CPT = 1 

DECLARE @cpt INT 
DECLARE @sql NVARCHAR(max) = '' 

SET @sql = 'select * from Data where PatientVisitProcsId = ' 
SET @cpt = 1 

IF @cpt = 1 
  SET @sql = @sql + ' 435244 ' 
ELSE 
  SET @sql = @sql + ' 435243 ' 

EXEC Sp_executesql 
  @sql 

Results 
patientvisitid	patientvisitprocsid	cptcode
222728			435244				99401
---------------------------------------------------------------------
-- @CPT = 0 

SET @cpt = 0 
SET @sql = 'select * from Data where PatientVisitProcsId = ' 

IF @cpt = 1 
  SET @sql = @sql + ' 435244 ' 
ELSE 
  SET @sql = @sql + ' 435243 ' 

EXEC Sp_executesql 
  @sql 

Results 
patientvisitid	patientvisitprocsid	cptcode
222728			435243				99214

The general form of this type of query would be:

WHERE ...
AND (pvp.CPTCode = @inputCPTCode OR @inputCPTCode IS NULL)

If the user passes in a CPT Code - then it will only display that CPT code. If the user does not pass in a code - then it will show all. You can use a default value instead of NULL - depends on how you prompt the user. For example, use -1 for ALL...

WHERE ...
AND (pvp.CPTCode = @inputCPTCode OR @inputCPTCode = -1)