【问题标题】:find data from previous month if current month is January in SQL如果当前月份是 SQL 中的一月,则查找上个月的数据
【发布时间】:2020-12-01 12:22:58
【问题描述】:

我需要写一份报告来显示前几个月的交易数量。

我有以下工作正常,直到当月是一月

SELECT 'Previous1' as Period, TrnYear,TrnMonth, PartCategory, StockCode, Description, Qty, DrawOfficeNum, WarehouseToUse, JobClassification
FROM     vwUsagePurchPress_1YearHistory
WHERE  (TrnYear = DATEPART(Year, GETDATE())) AND (TrnMonth = DATEPART(Month, GETDATE())-1)

如果当前月份 = 1 月,我如何让它读取上一年的 12 月

【问题讨论】:

标签: sql sql-server


【解决方案1】:

您的代码看起来像 SQL Server。我建议:

 where datediff(month, datefromparts(TrnYear, TrnMonth, 1), getdate) = 1

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 2018-06-18
相关资源
最近更新 更多