在where子句中使用Select Min和MAx

时间:2017-08-08 07:14:48

标签: sql max where-clause min

我使用了一些select语句来驱动结果,但是我有2个select语句,它们返回约会的最短时间和约会的最长时间。但是,如果min和max相同,我想从where子句中删除。

{{1}}

我收到错误Mintime无效。

我确信我需要在新的子选择中保留它但不确定..

干杯

3 个答案:

答案 0 :(得分:3)

HAVING clause用于聚合函数,例如

HAVING NOT MIN(APPOINTMENTTIME) <> MAX(APPOINTMENTTIME)

我不知道为什么你在谓词中使用双重否定,根据你的描述,你只需要<>

HAVING MIN(APPOINTMENTTIME) <> MAX(APPOINTMENTTIME)

答案 1 :(得分:1)

按照Sql Server

进行尝试
        SELECT * FROM (
        select   
        APPOINTMENTS.userid, 
        users.LOCATIONID,
        min(APPOINTMENTTIME) as mintime, 
        max(APPOINTMENTTIME) as maxtime
        from appointments
        where 
        APPOINTMENTDATE between '2017-01-07' and '2017-01-08'

        group by 
        appointments.USERID,users.LOCATIONID,
        appointments.APPOINTMENTDATE
        )as t WHERE  mintime <> maxtime

答案 2 :(得分:0)

请尝试

select   
APPOINTMENTS.userid, 
users.LOCATIONID,
min(APPOINTMENTTIME) as mintime, 
max(APPOINTMENTTIME) as maxtime

from appointments
where 
APPOINTMENTDATE between '2017-01-07' and '2017-01-07'

group by 
appointments.USERID,users.LOCATIONID,
appointments.APPOINTMENTDATE
having min(APPOINTMENTTIME)  <> max(APPOINTMENTTIME)