SKARVA Bodavula
SKARVA Bodavula

Reputation: 957

SQL query where clause filter based on another column value

Below query I am trying to filter status id =7 records for team_id>1 But want to include records with status_id=7 for team_id=1.How can we write the query.

 SELECT MAX(created_date) AS maxtime,
        TEAM_ID,
        ticket_id 
 FROM ri_ticket_status_history  
 WHERE status_id<>7    
 GROUP BY TEAM_ID,ticket_id

Upvotes: 2

Views: 4441

Answers (3)

Mureinik
Mureinik

Reputation: 311163

A combination of and and or logical operators should do it:

SELECT   MAX(created_date) AS maxtime ,team_id, ticket_id 
FROM     ri_ticket_status_history 
WHERE    (status_id <> 7 AND team_id > 1) OR team_id = 1
GROUP BY team_id, ticket_id

Upvotes: 4

A_Sk
A_Sk

Reputation: 4630

brace location can change the result set.

select max(created_date) as maxtime ,TEAM_ID,ticket_id 
from  ri_ticket_status_history 
where ( status_id<>7  or  (status_id=7 and team_id=1))
group by TEAM_ID,ticket_id

Upvotes: 1

Ewan
Ewan

Reputation: 1087

Try

select max(created_date) as maxtime ,TEAM_ID,ticket_id 
from  ri_ticket_status_history 
where status_id<>7  or  (status_id=7 and team_id=1)
group by TEAM_ID,ticket_id

Upvotes: 1

Related Questions