I am trying to find a way to calculate the date difference between different rows when these rows have the same Med_rec_no. I would be subtracting the sch_stp_ts from the "row" below that is labeled adm_ts
DROP TABLE #TEMP1
SELECT * INTO #TEMP1 FROM (
select DISTINCT
TPM300_PAT_VISIT.med_rec_no,
TPM300_PAT_VISIT.vst_ext_id as 'Visit Ext',
--TPM300_PAT_VISIT.vst_int_id AS 'Visit Int',
TPM300_PAT_VISIT.adm_ts,
--TYPE.cod_dtl_ds AS 'PAT_TYP',
DISCH_SERV.cod_dtl_ds,
TOR101_CASE_PROCEDURE.pro_stp_dt,
TOR101_CASE_PROCEDURE.sch_stp_ts,
TSM910_ICD9_REF.icd9_code,
TSM910_ICD9_REF.icd9_abrv_ds
from
TPM300_PAT_VISIT
INNER JOIN TSM180_MST_COD_DTL TYPE ON TPM300_PAT_VISIT.pat_ty = TYPE.cod_dtl_int_id
INNER JOIN TSM180_MST_COD_DTL DISCH_SERV ON TPM300_PAT_VISIT.dschg_srv_cd = DISCH_SERV.cod_dtl_int_id
left JOIN TPM318_VISIT_DIAGNOSIS ON TPM300_PAT_VISIT.vst_int_id = TPM318_VISIT_DIAGNOSIS.vst_int_id
LEFT JOIN TSM910_ICD9_REF ON TPM318_VISIT_DIAGNOSIS.icd9_int_id = TSM910_ICD9_REF.icd9_int_id
LEFT JOIN TOR100_CASE_HEADER ON TPM300_PAT_VISIT.vst_int_id = TOR100_CASE_HEADER.vst_int_id
LEFT JOIN TOR101_CASE_PROCEDURE ON TOR100_CASE_HEADER.cas_int_id = TOR101_CASE_PROCEDURE.cas_int_id
LEFT JOIN TOR105_CASE_PARTICIPANT ON TOR101_CASE_PROCEDURE.cas_pro_int_id = TOR105_CASE_PARTICIPANT.cas_pro_int_id
where
--TOR105_CASE_PARTICIPANT.ptc_stp_dt BETWEEN '01/01/2014' AND getdate()
--and
--AND
TSM910_ICD9_REF.icd9_code IN ('I468', 'I469', 'I97121', 'I97120', 'I213','I214','J960','J9589','J698','R092')
AND
ICD9_diag_ty = 'P'
--AND
--TSM910_ICD9_REF.icd9_code = 'R092'
--AND
--TSM910_ICD9_REF.icd9_code IN ('J960','J9589','J698')
UNION ALL
SELECT DISTINCT
TPM300_PAT_VISIT.med_rec_no,
TPM300_PAT_VISIT.vst_ext_id as 'Visit Ext',
--TPM300_PAT_VISIT.vst_int_id AS 'Visit Int',
TPM300_PAT_VISIT.adm_ts,
DISCH_SERV.cod_dtl_ds,
--TYPE.cod_dtl_ds AS 'PAT_TYP',
TOR101_CASE_PROCEDURE.pro_stp_dt,
TOR101_CASE_PROCEDURE.sch_stp_ts,
TSM910_ICD9_REF.icd9_code,
TSM910_ICD9_REF.icd9_abrv_ds
FROM
TPM300_PAT_VISIT
INNER JOIN TSM180_MST_COD_DTL ON TPM300_PAT_VISIT.pat_ty = TSM180_MST_COD_DTL.cod_dtl_int_id
INNER JOIN TPB105_CHARGE_DETAIL ON TPM300_PAT_VISIT.vst_int_id = TPB105_CHARGE_DETAIL.vst_int_id
INNER JOIN TSM180_MST_COD_DTL DISCH_SERV ON TPM300_PAT_VISIT.dschg_srv_cd = DISCH_SERV.cod_dtl_int_id
INNER JOIN TSM180_MST_COD_DTL TYPE ON TPM300_PAT_VISIT.pat_ty = TYPE.cod_dtl_int_id
left JOIN TPM318_VISIT_DIAGNOSIS ON TPM300_PAT_VISIT.vst_int_id = TPM318_VISIT_DIAGNOSIS.vst_int_id
LEFT JOIN TSM910_ICD9_REF ON TPM318_VISIT_DIAGNOSIS.icd9_int_id = TSM910_ICD9_REF.icd9_int_id
LEFT JOIN TOR100_CASE_HEADER ON TPM300_PAT_VISIT.vst_int_id = TOR100_CASE_HEADER.vst_int_id
LEFT JOIN TOR101_CASE_PROCEDURE ON TOR100_CASE_HEADER.cas_int_id = TOR101_CASE_PROCEDURE.cas_int_id
LEFT JOIN TOR105_CASE_PARTICIPANT ON TOR101_CASE_PROCEDURE.cas_pro_int_id = TOR105_CASE_PARTICIPANT.cas_pro_int_id
WHERE
TPM300_PAT_VISIT.adm_ts BETWEEN '01/01/2015' AND '01/01/2016'
AND
sch_stp_ts IS NOT NULL
AND
pro_stp_dt IS NOT NULL
AND
ICD9_diag_ty = 'P'
AND
DISCH_SERV.cod_dtl_ds LIKE ('%SURGERY%')
) AS TEMP1