I have a T-SQL Query running on SQL Server 2005 where I have defined a table of data to be returned to the application.
I am assigning several values using several case statements which each has the identical logic.
I am wondering if there is a way to make this more efficient instead of performing the same logic multiple times? I have abbreviated the logic here, but hopefully there is enough to make the point. Is there a way that I wouldn't have to define three case statements using identical "pt.PTYP_Category IN (90, 92, 94, 96, 98)" logic?
`INSERT INTO @MemberAccountingDetail
(
MACD_DPST_FEES_RowID,
MACD_HighOrderSort,
MACD_CategorySortDate,
MACD_MemberName
)
SELECT
d.DPST_ID As MACD_DPST_FEES_RowID,
CASE
WHEN pt.PTYP_Category IN (90, 92, 94, 96, 98)
THEN 0
ELSE 1
END As MACD_HighOrderSort,
CASE
WHEN pt.PTYP_Category IN (90, 92, 94, 96, 98)
THEN DATEADD(ss, 1, d.DPST_TransactionDate)
ELSE @DefaultDate
END As MACD_CategorySortDate,
CASE
WHEN pt.PTYP_Category IN (90, 92, 94, 96, 98)
THEN pt.PTYP_CategoryDesc
ELSE SUBSTRING(ISNULL(d.DPST_OriginUserID, ''), 1, 11)
END As MACD_MemberName
FROM
Deposit d
INNER JOIN PaymentType pt
ON pt.PTYP_ID = d.DPST_PTYP_ID
WHERE
d.DPST_MEMB_ID = @MEMB_ID;
Thank you, Jim `