Results 1 to 14 of 14
  1. #1
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44

    SQL Question

    select avg(datediff(d,coalesce(datevalue1,getdate()),coalesce(datevalue2,getdate())))as DaysDiffer from Tablename ; how do I change this statement for data for only this current year.

    Thanks folks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    With a WHERE clause, like:

    WHERE Year(datevalue1)=Year(GetDate())

    Is coalesce() a custom VBA function?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    from Google:
    The COALESCE function in SQL returns the first non-NULL expression among its arguments. The syntax for COALESCE is as follows: COALESCE ("expression 1", "expressions 2", ...) It is the same as the following CASE statement: SELECT CASE ("column_name")

    Neither Coalesce nor CASE are supported with Access SQL.


    What is your GetDate() function?
    What do you want to do in plain English?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I don't understand the question either, but the syntax is SQL Server. GetDate() is the equivalent of Now().
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44
    I need an Average Count of two date columns for 2018.

    Column 1 NAME = DATE_START
    Column 2 Name = Date_Received
    Table Name = REC_REC

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Presuming the rest works, and presuming you only care about the start date:

    select avg(datediff(d,coalesce(datevalue1,getdate()),coalesce(datevalue2,getdate())))as DaysDiffer
    from Tablename
    WHERE Date_Start Between '1/1/2018' AND '12/31/2018'
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44
    Hello,
    the answer you provided worked.

    I used the coalesce function in case there were NULL values for DateValue1, and DateValue2.

    Recap: I am being asked to provide an AVG of record count per Day between two date columns for 2018.

    Thanks.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Happy to help.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by 9944pdx View Post
    I need an Average Count of two date columns for 2018.

    Column 1 NAME = DATE_START
    Column 2 Name = Date_Received
    Table Name = REC_REC
    And how do you count days, when DATE_START is e.g. December 1st of 2017, and Date_Received is February 1st of 2018? I think you have to check for year of start date, and when it is less than your query year, you have to replace the start date with January 1st of query year. And in case the query year can be any year from past (not only current year), then you have to make the year check for date received too (Date_Recieved, or end of query year, or GetDate())

  10. #10
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44
    Can you provide what this would look like?

  11. #11
    Join Date
    Apr 2017
    Posts
    1,792
    On fly
    (I based the query on pbaldy's example)
    (and I haven't tested expressions to calculate first and last date of year, got them from https://dwaincsql.com/2014/04/04/man...imes-in-t-sql/)
    someting like:
    Code:
    SELECT
    avg(
          datediff(
               d,
               (CASE 
                    WHEN datevalue1 < CAST(1+DATEADD(month, 1-1,DATEADD(year,QueryYear-1900,0))-1 AS DATE) 
                                        THEN CAST(1+DATEADD(month, 1-1,DATEADD(year,QueryYear-1900,0))-1 AS DATE)
                                        ELSE datevalue1 
                              END),
               (CASE 
                   WHEN QueryYear = Year(GetDate()) 
                   THEN COALESCE(datevalue2, GetDate()) 
                   ELSE 
                        (CASE 
                             WHEN YEAR(datevalue2) = YEAR(QueryYear) 
                             THEN  datevalue2
                             ELSE CAST(1-1+DATEADD(month, 1-1,DATEADD(year,QueryYear+1-1900,0))-1 AS DATE) 
                                                 END) 
              END),
              )
         )
    )
    from Tablename
    WHERE datevalue1 <= CAST(1-1+DATEADD(month, 1-1,DATEADD(year,QueryYear+1-1900,0))-1 AS DATE) AND (datevalue2 Is Null OR datevalue2 >= CAST(1+DATEADD(month, 1-1,DATEADD(year,QueryYear-1900,0))-1 AS DATE))
    Last edited by ArviLaanemets; 07-02-2018 at 10:59 PM.

  12. #12
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44
    Thanks, I will work with that and come back to you if I have questions.

  13. #13
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    These are a the most common date functions I use - from here http://www.sqlservercentral.com/blog...date-routines/

    Code:
    declare @ThisDate datetime;
    set @ThisDate = getdate();
    
    
    select dateadd(dd, datediff(dd, 0, @ThisDate), 0)     -- Beginning of this day
    select dateadd(dd, datediff(dd, 0, @ThisDate) + 1, 0) -- Beginning of next day
    select dateadd(dd, datediff(dd, 0, @ThisDate) - 1, 0) -- Beginning of previous day
    select dateadd(wk, datediff(wk, 0, @ThisDate), 0)     -- Beginning of this week (Monday)
    select dateadd(wk, datediff(wk, 0, @ThisDate) + 1, 0) -- Beginning of next week (Monday)
    select dateadd(wk, datediff(wk, 0, @ThisDate) - 1, 0) -- Beginning of previous week (Monday)
    select dateadd(mm, datediff(mm, 0, @ThisDate), 0)     -- Beginning of this month
    select dateadd(mm, datediff(mm, 0, @ThisDate) + 1, 0) -- Beginning of next month
    select dateadd(mm, datediff(mm, 0, @ThisDate) - 1, 0) -- Beginning of previous month
    select dateadd(qq, datediff(qq, 0, @ThisDate), 0)     -- Beginning of this quarter (Calendar)
    select dateadd(qq, datediff(qq, 0, @ThisDate) + 1, 0) -- Beginning of next quarter (Calendar)
    select dateadd(qq, datediff(qq, 0, @ThisDate) - 1, 0) -- Beginning of previous quarter (Calendar)
    select dateadd(yy, datediff(yy, 0, @ThisDate), 0)     -- Beginning of this year
    select dateadd(yy, datediff(yy, 0, @ThisDate) + 1, 0) -- Beginning of next year
    select dateadd(yy, datediff(yy, 0, @ThisDate) - 1, 0) -- Beginning of previous year
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    9944pdx is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jan 2011
    Location
    PDX
    Posts
    44
    all is well, thanks for all your help folks.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 08-25-2012, 07:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums