sql server - Using a range with GETDATE & BETWEEN -


i'm new t-sql , still learning please bear me on one...i've tried several ways no luck. have 'current' , 90+ days it's 30 , 60 days i'm struggling with. why can't use 'between' clause?

   ,[current]=(select sum (cle.[amount])       [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date]>getdate()-30)        ,[30 days]=(select sum (cle.[amount])      [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date]between getdate()-31 , getdate()-59)      ,[60 days]=(select sum (cle.[amount])       [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date] between getdate()-60 , getdate()-89)         ,[90 + days]=((select sum (cle.[amount])       [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date]<=getdate()-90 )) 

i know there entries particular customer in both of buckets.

suggestions?

when filtering range of dates, between expects earlier date come before later date.

you need reverse sides of between older date (with highest -value) comes first:

   ,[30 days]=(select sum (cle.[amount])      [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date]between getdate()-59 , getdate()-31)      ,[60 days]=(select sum (cle.[amount])       [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_]      c.[no_]=cl3.[customer no_] , cl3.[open]='1' , cl3.[due date] between getdate()-89 , getdate()-60)  

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -