-1

我在这一行遇到了这个 case 表达式的问题

DATEPART(yyyy, DATEADD(mm, -5, getdate()))

如果我删除这一行,它可以正常工作,但如果我把它排除在外,它会从每个 12 月选择记录,而不仅仅是像我想要的那样从 2012 年 12 月开始。

我到处搜索,我无法弄清楚。

Select C.CustId ,
Sum(Case DATEPART(mm, I.InvoiceDate)
When DATEPART(mm, DATEADD(mm, -5, getdate())) and
     DATEPART(yyyy, DATEADD(mm, -5, getdate()))
  Then Ia.Amount 
  Else 0 End) As 'Total0'
from Invoice I 
inner join InvoiceAmtSummary Ia  on I.GUIDInvoice=Ia.GUIDInvoice
inner join Customer C on  C.GUIDCustomer=I.GUIDCustomer
group by C.CustId
4

4 回答 4

1

我想你可能想要

SELECT
            C.CustId,
            SUM
            (
                CASE 
                    WHEN
      DATEPART(mm, I.InvoiceDate) = DATEPART(mm, DATEADD(mm, -5, getdate()))
  AND
      DATEPART(yyyy, I.InvoiceDate) = DATEPART(yyyy, DATEADD(mm, -5, getdate()))
                        THEN Ia.Amount
                    ELSE
                        0
                END
            ) [Total0]
    FROM 
            Invoice I 
        JOIN
            InvoiceAmtSummary Ia  
                ON I.GUIDInvoice = Ia.GUIDInvoice
        JOIN
            Customer C 
                ON  C.GUIDCustomer = I.GUIDCustomer
    GROUP BY
            C.CustId

或者,更明智的

DECLARE @TargetDate DateTime;
DECLARE @TargetYear Int;
DECLARE @TargetMonth Month;

SET @TagetDate = DATEADD(mm, -5, getdate()));
SET @TargetYear = YEAR(@TargetDate);
SET @TargetMonth = MONTH(@TargetDate);

SELECT
            C.CustId,
            SUM(COALESCE(Ia.Amount, 0))
     FROM
            Customer C
         LEFT JOIN
            ( 
            SELECT
                         MONTH(I.InvoiceDate) Month,
                         YEAR(I.InvoiceYear) Year,
                         I.GUIDInvoice,
                         I.GUIDCustomer
                FROM
                         Invoice I
            ) S
                ON S.GUIDCustomer = C.GUIDCustomer
         LEFT JOIN
            InvoiceAmtSummary Ia
                ON Ia.GUIDInvoice = S.GUIDInvoice
     WHERE
             S.Year = @TargetYear
         AND
             S.Month = @TargetMonth
     GROUP BY
             C.CustID;
于 2013-04-05T13:03:07.797 回答
1

呃,为什么不:

WHERE InvoiceDate >= DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-4, 0)
  AND InvoiceDate <  DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-3, 0)

最终评估(今天,2013 年 4 月 5 日)为:

WHERE InvoiceDate >= '2012-12-01'
  AND InvoiceDate <  '2013-01-01'

放入您的查询:

SELECT C.CustId,
  [Total0] = SUM(CASE 
    WHEN InvoiceDate >= DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-4, 0)
     AND InvoiceDate <  DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-3, 0)
    THEN Ia.Amount ELSE 0 END
  )
FROM dbo.Invoice AS I 
INNER JOIN dbo.InvoiceAmtSummary AS Ia 
   ON I.GUIDInvoice = Ia.GUIDInvoice
INNER JOIN dbo.Customer AS C 
   ON C.GUIDCustomer = I.GUIDCustomer
GROUP BY C.CustId;

尽管您可能希望将其放在WHERE子句中,除非您想包括当月没有销售的客户(似乎将他们排除在外是合乎逻辑的,因为您已经排除了从未有过销售的客户发票)。例如

SELECT C.CustId, Total0 = SUM(Ia.Amount)
FROM dbo.Invoice AS I
INNER JOIN dbo.InvoiceAmtSummary AS Ia 
   ON I.GUIDInvoice = Ia.GUIDInvoice
INNER JOIN dbo.Customer AS C 
   ON C.GUIDCustomer = I.GUIDCustomer
   WHERE InvoiceDate >= DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-4, 0)
     AND InvoiceDate <  DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE())-3, 0)
GROUP BY C.CustId;

无论如何,现在您实际上可以使用 index onInvoiceDate而不是这种DATEPART(MONTH不可分割的 hack。

还有一些其他的提示:

  1. 说出你的意思。yyyy当您可以输入YEAR并且更明确时,为什么要输入?要了解为什么这很重要,请尝试猜测这将返回什么,然后尝试找出为什么它没有按照您的想法执行:

    SELECT DATEPART(y, GETDATE());
    
  2. 你的语法不正确。你不能说CASE WHEN (some boolean expression) THEN- 你必须将它与某物进行比较。CASE不是布尔构造,它是返回单个结果的表达式。所以你必须评估它,比如CASE WHEN (expression) = 1 THEN......

  3. 请不要使用AS 'alias'带单引号的语法。如果需要分隔符(在这种情况下不需要),请使用[square brackets]. 某些形式的别名单引号定界符已被弃用,它们使代码更难阅读,因为它们看起来像字符串文字。

  4. 请阅读以下有关日期范围查询的文章:

于 2013-04-05T13:18:32.753 回答
0

要仅选择 2012 年 12 月的记录,请尝试

select ... FROM ... where YEAR(your_timestamp) = 2012 AND MONTH(your_timestamp) = 12
于 2013-04-05T13:01:56.203 回答
0

试试这个。它现在为您提供本月的动态所有记录 - 5

select ... FROM ... where YEAR(your_timestamp) = YEAR(DATE_SUB(NOW(), INTERVAL 5 MONTH)) AND MONTH(your_timestamp) = Month(DATE_SUB(NOW(), INTERVAL 5 MONTH))
于 2013-04-05T13:38:09.393 回答