使用带有GETDATE&的范围之间

时间:2015-04-22 17:15:41

标签: sql-server tsql

我对T-SQL相当新,我还在学习,所以请耐心等待我...我已经尝试了几种方法来做到这一点,没有运气。我有我现在的'还有90多天,但这是我奋斗的30天和60天。为什么我不能使用'之间的'条款?

   ,[CURRENT]=(SELECT sum (cle.[Amount]) 
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]>getdate()-30) 



   ,[30 DAYS]=(SELECT sum (cle.[Amount])
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]BETWEEN GETDATE()-31 AND getdate()-59) 

   ,[60 DAYS]=(SELECT sum (cle.[Amount]) 
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date] BETWEEN GETDATE()-60 AND getdate()-89) 

      ,[90 + DAYS]=((SELECT sum (cle.[Amount]) 
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]<=getdate()-90 ))

我知道在这两个存储桶中都有针对该特定客户的条目。

建议?

2 个答案:

答案 0 :(得分:1)

过滤一系列日期时,BETWEEN预计较早的日期早于日期。

您需要反转BETWEEN的两侧,以便先显示较早的日期(最高值):

   ,[30 DAYS]=(SELECT sum (cle.[Amount])
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date]BETWEEN GETDATE()-59 AND getdate()-31) 

   ,[60 DAYS]=(SELECT sum (cle.[Amount]) 
     FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3 left outer join 
     [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle on cl3.[entry no_]=cle.[cust_ ledger entry no_]
     where C.[NO_]=CL3.[CUSTOMER NO_] AND cl3.[open]='1' AND cl3.[Due Date] BETWEEN GETDATE()-89 AND getdate()-60) 

答案 1 :(得分:0)

您可以将一个查询与CASE语句

一起使用,而不是四个单独的子查询
SELECT
  [CURRENT] = SUM(CASE WHEN cl3.[Due Date] >= DATEADD(day,-30,GETDATE())
                       THEN cle.[Amount]
                  END)
 ,[30 DAYS] = SUM(CASE WHEN cl3.[Due Date] <  DATEADD(day,-30,GETDATE())
                        AND cl3.[Due Date] >= DATEADD(day,-60,GETDATE())
                       THEN cle.[Amount]
                  END)
 ,[60 DAYS] = SUM(CASE WHEN cl3.[Due Date] <  DATEADD(day,-60,GETDATE())
                        AND cl3.[Due Date] >= DATEADD(day,-90,GETDATE())
                       THEN cle.[Amount]
                  END)
 ,[90 DAYS] = SUM(CASE WHEN cl3.[Due Date] <  DATEADD(day,-90,GETDATE())
                       THEN cle.[Amount]
                  END)
FROM [NISNAV].[dbo].[NIS$Cust_ Ledger Entry] cl3
LEFT OUTER JOIN [NISNAV].[dbo].[NIS$Detailed Cust_ Ledg_ Entry] cle
  ON( cl3.[entry no_]=cle.[cust_ ledger entry no_])
WHERE C.[NO_]=CL3.[CUSTOMER NO_]
  AND cl3.[open]='1'