SELECT
DAY(table_A.PaymentDate) as date1 ,
(CASE
WHEN MONTH(table_A.PaymentDate) = MONTH(CURRENT_TIMESTAMP)
THEN CAST(SUM(table_A.Total_Amount) As INT)
ELSE 0
END) AS This_month_CNT,
(CASE
WHEN MONTH(table_A.PaymentDate) = MONTH(CURRENT_TIMESTAMP) - 1
THEN CAST(SUM(table_A.Total_Amount) AS INT)
ELSE 0
END) AS last_month_CNT
FROM
Tbl_Pan_Paymentdetails table_A
FULL OUTER JOIN
Tbl_Pan_Paymentdetails table_B ON table_A.PaymentDate = table_B.PaymentDate
WHERE
YEAR(table_A.PaymentDate) = YEAR(CURRENT_TIMESTAMP)
AND table_A.PaymentDate >= DATEADD(MONTH, -2, GETDATE())
GROUP BY
DAY(table_A.PaymentDate),
MONTH(table_A.PaymentDate)
ORDER BY
DAY(table_A.PaymentDate) ;
答案 0 :(得分:1)
不确定我完全理解。
WHERE YEAR(table_A.PaymentDate) = YEAR(CURRENT_TIMESTAMP) AND
table_A.PaymentDate >= DATEADD(MONTH, -2, GETDATE())
在这里,您(1)将付款日期的年度元素与CURRENT_TIMESTAMP
进行比较,以及(2)根据GETDATE()
确保付款日期大于过去2个月?
不确定为什么同时使用CURRENT_TIMESTAMP
和GETDATE()
。无论哪种方式,我认为WHERE语句的第二部分可以满足您的需求。
如果当前日期是2015年1月31日,那么您当前的逻辑将不会返回2014年12月的任何记录。您的where语句的第一部分将过滤掉它们。如果您真的想要最近2个月,请从WHERE语句中删除以下内容
YEAR(table_A.PaymentDate) = YEAR(CURRENT_TIMESTAMP) AND