具有日期范围和max函数的SQL Server查询

时间:2019-07-10 12:25:07

标签: sql-server date

我有两个表TBL_TRANSACTION(NUM_ID,TRANS_DATE,TRANS_TYPE,TOTAL_SUM)TBL_PERSONS(NUM_ID,NAME)

我想获取此查询的结果:从两个表中以条件(Names that don't have a transaction between the current date and DATEADD(YYYY,-1,GETDATE()))) and TRANS_TYPE=1选择NAME,NUM_ID和TRANS_DATE列

SELECT  dbo.TBL_TRANSACTION.NUM_ID, dbo.TRANSACTION.TRANS_DATE, dbo.TBL_PERSON.NAME, dbo.TBL_PERSON.TOTAL_SUM
FROM    dbo.TBL_TRANSACTION INNER JOIN
        dbo.TBL_PERSON ON dbo.TBL_TRANSACTION.NUM_ID = dbo.TBL_PERSON.NUM_ID
WHERE  (dbo.TBL_TRANSACTION.TRANS_DATE NOT BETWEEN DATEADD(yyyy, - 1, GETDATE()) AND GETDATE()) AND TRANS_TYPE =1

但是,我没有收到以上任何结果。

1 个答案:

答案 0 :(得分:1)

在此查询中对日期过滤进行了一些调整。你能检查一下吗?

SELECT T1.NUM_ID, 
       T1.TRANS_DATE, 
       T2.NAME, 
       T2.TOTAL_SUM
FROM dbo.TBL_TRANSACTION T1
     INNER JOIN dbo.TBL_PERSON T2 ON T1.NUM_ID = T2.NUM_ID
WHERE T1.TRANS_DATE < DATEADD(yyyy, -1, GETDATE())
     AND TRANS_TYPE = 1;