简体   繁体   中英

Get the MAX, MIN, AVG between two DATES

I'm trying to query min, max and avg between two dates, that I got from [DATA].

SELECT
    MIN([INDEX]) as MIN,
    AVG([INDEX]) as avg,
    MAX([INDEX]) as max
  FROM [data].[dbo].[db_DATA]

I tried with the following sql query, but didn't work too.

SELECT
    MIN([INDEX]) as MIN,
    AVG([INDEX]) as avg,
    MAX([INDEX]) as max
  FROM [data].[dbo].[db_DATA]
 having MIN([INDEX]) between '2017-02-20' AND '2017-02-25'

As you do not GROUP BY put it in the where clause instead of HAVING. Also remove MIN() because if min is between then max is also and avg is also.

SELECT
    MIN([INDEX]) as MIN,
    AVG([INDEX]) as avg,
    MAX([INDEX]) as max
  FROM [data].[dbo].[db_DATA]
 WHERE [INDEX] between '2017-02-20' AND '2017-02-25'

the dates is always with '#' if you are working with Ms-access

try like this

SELECT
    MIN([INDEX]) as MIN,
    AVG([INDEX]) as avg,
    MAX([INDEX]) as max
  FROM [data].[dbo].[db_DATA]
 having MIN([INDEX]) between #02/20/2017# AND #02/25/2017#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM