1
votes

I am using a number of select statements to drive results, however I have 2 select statements which returns the minimum time for appointment and max time for appointment. However where the min and max are the same, I want to remove from within my where clause.

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'
AND NOT mintime <> maxtime
GROUP BY appointments.USERID,
         users.LOCATIONID,
         appointments.APPOINTMENTDATE

I get the error Mintime is not valid.

I am sure I need to hold this in a new sub select but not sure..

Cheers

3
Left justified SQL is too hard to read... - jarlh

3 Answers

3
votes

The HAVING clause is used for aggregate functions, e.g.

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

I don't know why you are using a double negative in your predicate though, based on your description you just need <>

HAVING MIN(APPOINTMENTTIME) <> MAX(APPOINTMENTTIME)
1
votes

Try this as per 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
0
votes

Please try

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)