[英]SQL select issue : get max value for each day
I have a problem with my SQL select, I need to get the max value for the column ( currentcount
) per each day for the period between DateDebut
and DateFin
, but the returned result show me multiple values. 我的SQL选择有问题,我需要获取
DateDebut
和DateFin
之间的每一天的列( currentcount
)的DateFin
,但是返回的结果显示了多个值。
My SQL statement: 我的SQL语句:
DECLARE @dateDebut DATE,
@dateFin DATE,
@SITA_METIER VARCHAR(20),
@DEFSITE VARCHAR(20);
SET @dateDebut = '01/01/2017';
SET @dateFin = '31/12/2018';
SELECT DISTINCT
CONVERT(DATE, attemptdate) AS Date,
MAX(currentcount) AS MAXUSERS
FROM
logintracking
WHERE
attemptdate BETWEEN CONVERT(DATE, @dateDebut) AND CONVERT(DATE, @dateFin)
AND logintracking.clientaddr IN ('10.118.254.21', '10.118.254.156')
GROUP BY
attemptdate, currentcount
Result: 结果:
enter image description here 在此处输入图片说明
Desired result: only the max value for the column ( currentcount
) for each day 所需结果:每天仅列的最大值 (
currentcount
)
PS: attemptdate
type is Timestamp
, that's why I need to cast it into a simple date. PS:
attemptdate
类型为Timestamp
,这就是为什么我需要将其attemptdate
转换为简单日期。
I also tried to use 'having' function but still get multiple values 我也尝试使用“具有”功能,但仍然获得多个值
You already use GROUP BY
the distinct
is no make sense, so the distinct
can be removed. 您已经使用
GROUP BY
了, distinct
是没有意义的,因此可以删除distinct
。
then you just modify CONVERT(Date,attemptdate)
instead of attemptdate
in Group by
, and only need to group by CONVERT(Date,attemptdate)
那么您只需修改
CONVERT(Date,attemptdate)
而不是Group by
的attemptdate
,并且只需group by CONVERT(Date,attemptdate)
进行group by CONVERT(Date,attemptdate)
select CONVERT(Date,attemptdate) as Date, max(currentcount) as MAXUSERS
from logintracking
where attemptdate between @dateDebut and @dateFin
and logintracking.clientaddr in ('10.118.254.21', '10.118.254.156')
group by CONVERT(Date,attemptdate)
Note: 注意:
Your @dateDebut
and @dateFin
already are Date
type. 您的
@dateDebut
和@dateFin
已经是Date
类型。 No need to use CONVERT
function. 无需使用
CONVERT
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.