我在SQL Server 2005上运行了一个T-SQL查询,我在其中定义了一个要返回给应用程序的数据表。
我使用几个case语句分配几个值,每个case语句都有相同的逻辑。
我想知道是否有办法让这个效率更高,而不是多次执行相同的逻辑?我在这里缩写了逻辑,但希望有足够的说明。有没有办法让我不必使用相同的“pt.PTYP_Category IN(90,92,94,96,98)”逻辑来定义三个case语句?
`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;
谢谢你, 吉姆`
答案 0 :(得分:1)
您可以使用公用表表达式,因此您只需编写一次“IN STATEMENT”:
INSERT INTO @MemberAccountingDetail
(
MACD_DPST_FEES_RowID,
MACD_HighOrderSort,
MACD_CategorySortDate,
MACD_MemberName
)
WITH CTE (PTYP_ID, IsInCollection)
AS
(
SELECT pt.PTYP_ID, CASE WHEN pt.PTYP_Category IN (90, 92, 94, 96, 98) THEN 0 ELSE 1 END AS IsInCollection
FROM PaymentType pt
)
SELECT
d.DPST_ID As MACD_DPST_FEES_RowID,
CTE.IsInCollection As MACD_HighOrderSort,
CASE WHEN CTE.IsInCollection = 1
THEN DATEADD(ss, 1, d.DPST_TransactionDate)
ELSE @DefaultDate
END As MACD_CategorySortDate,
CASE WHEN CTE.IsInCollection = 1
THEN pt.PTYP_CategoryDesc
ELSE SUBSTRING(ISNULL(d.DPST_OriginUserID, ''), 1, 11)
END As MACD_MemberName
FROM Deposit d
INNER JOIN CTE
ON CTE.PTYP_ID = d.DPST_PTYP_ID
INNER JOIN PaymentType pt
ON pt.PTYP_ID = d.DPST_PTYP_ID
WHERE d.DPST_MEMB_ID = @MEMB_ID;
答案 1 :(得分:0)
您可以尝试使用UNION进行两次单独选择,但我不确定它会更快:
INSERT INTO @MemberAccountingDetail
(
MACD_DPST_FEES_RowID,
MACD_HighOrderSort,
MACD_CategorySortDate,
MACD_MemberName
)
SELECT
d.DPST_ID As MACD_DPST_FEES_RowID,
0 As MACD_HighOrderSort,
DATEADD(ss, 1, d.DPST_TransactionDate) As MACD_CategorySortDate,
pt.PTYP_CategoryDesc 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 and pt.PTYP_Category IN (90, 92, 94, 96, 98)
UNION ALL
SELECT
d.DPST_ID As MACD_DPST_FEES_RowID,
1 As MACD_HighOrderSort,
@DefaultDate As MACD_CategorySortDate,
SUBSTRING(ISNULL(d.DPST_OriginUserID, ''), 1, 11) 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 and pt.PTYP_Category NOT IN (90, 92, 94, 96, 98)
答案 2 :(得分:0)
您可以在表格中创建一个计算列,将这5个类别汇总到一个代码中,然后使用它。
这意味着您不再需要硬编码魔术数字。但是你仍然需要四种不同的案例陈述。这是任何试图解决它的“聪明”的事情之一,只会使它变得更加复杂。
答案 3 :(得分:0)
CROSS APPLY的另一个选项
INSERT INTO @MemberAccountingDetail
(
MACD_DPST_FEES_RowID,
MACD_HighOrderSort,
MACD_CategorySortDate,
MACD_MemberName
)
SELECT
d.DPST_ID As MACD_DPST_FEES_RowID,
CASE WHEN o.IsMatch = 1 THEN 0
ELSE 1 END As MACD_HighOrderSort,
CASE WHEN o.IsMatch = 1 THEN DATEADD(ss, 1, d.DPST_TransactionDate)
ELSE @DefaultDate END As MACD_CategorySortDate,
CASE WHEN o.IsMatch = 1 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
CROSS APPLY (SELECT CASE WHEN pt.PTYP_Category IN (90, 92, 94, 96, 98) THEN 1 END AS IsMatch) o
WHERE d.DPST_MEMB_ID = @MEMB_ID;