1

我在 SQL Server 2005 上运行了一个 T-SQL 查询,我在其中定义了一个要返回给应用程序的数据表。

我使用几个case语句分配了几个值,每个case语句都有相同的逻辑。

我想知道是否有一种方法可以提高效率而不是多次执行相同的逻辑?我已经简化了这里的逻辑,但希望足以说明这一点。有没有一种方法我不必使用相同的“pt.PTYP_Category IN (90, 92, 94, 96, 98)”逻辑来定义三个案例语句?

`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; 

谢谢你,吉姆`

4

4 回答 4

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;
于 2013-02-08T10:26:53.050 回答
0

您可以在表中创建一个计算列,将这 5 个类别汇总为一个代码,然后使用它。

这意味着您不必再对幻数进行硬编码。但是你仍然需要四个不同的 case 语句。这是其中任何试图“聪明”地解决它只会让它变得更加复杂的事情之一。

于 2013-02-08T05:45:57.410 回答
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)
于 2013-02-08T05:35:47.017 回答
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; 
于 2013-02-08T12:50:36.603 回答