0

I have the following SQL Query

SELECT gsu.*, gsuo.*, gsob.*, gsued.*
FROM gs_users gsu
INNER JOIN gs_user_objects gsuo
ON gsu.id = gsuo.user_id
INNER JOIN gs_objects gsob
ON gsuo.imei = gsob.imei
INNER JOIN gs_user_events_data gsued
ON gsuo.imei = gsued.imei 
WHERE NOT gsued.status = 'Actioned' AND gsuo.user_id = '62' 
    OR gsuo.user_id = '14' 
GROUP BY gsued.dt_tracker 
ORDER BY gsued.dt_tracker 

When I use the above query the first section of "WHERE NOT" and "AND" is executed and filtered, but after the "OR" section with the 14 is executed but the "WHERE NOT" clause is not executed.

I have tried brackets as well but no result on the filter

Serg
  • 22,285
  • 5
  • 21
  • 48
eTrack
  • 7
  • 3
  • Please explain the logic you want to implement. It is not obvious from the question. – Gordon Linoff Mar 22 '21 at 15:47
  • It really depends on what you want to achieve. Sample data and expected result would clear up the ambiguity. Just going by hunch I'd assume you want WHERE NOT status = 'Actioned' AND (gsuo.user_id IN ('62','14') – Error_2646 Mar 22 '21 at 15:48
  • I have one table with two columns, One Colum is Status where it is either Actioned or Active. The other column has user_id there is two user ID's 14 and 62 that i want to select but only display them if the status is equal to Actioned. What my code is doing now is selecting all 62 id's and Actioned but not selecting any 14 id's it is basically showing all lines that 14 has regardless if Actioned or Active – eTrack Mar 22 '21 at 15:50

1 Answers1

1

I might guess that you want:

WHERE gsued.status <> 'Actioned' AND
      gsuo.user_id IN (62, 14)

Note: I removed the single quotes, because the values look like numbers. If user_id is really a string, then use single quotes. But in general, try not to mix data types.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786