Reputation: 320
I have a simple table in SQL server 2008.
SELECT TOP lastupdated, remarks FROM table
I want to select the following criteria.
if remarks is 'Others'
and DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > 15
then select these rows
Additionally, I want to select following in the same query
if remarks is not 'Others'
then select these rows without waiting for 15 minutes
Upvotes: 2
Views: 1102
Reputation: 46
Just keep first to condition in braces and then add OR condition like below:
SELECT TOP lastupdated , remarks FROM table
WHERE (remarks = 'Others' AND DATEDIFF(MINUTE, LastUpdated, GETDATE()) > 15)
OR remarks <> 'Others'
Hope it helps.
Upvotes: 2
Reputation: 31
You should be able to just nest your WHERE clause like this:
SELECT lastupdated , remarks
FROM table
WHERE remarks != 'Others'
OR
(
remarks = 'Others'
AND
DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > 15
)
Upvotes: 3
Reputation: 311978
A case
expression should help you here:
SELECT TOP lastupdated, remarks
FROM mytable
WHERE DATEDIFF(MINUTE, LastUpdated, GETDATE() ) > CASE remarks WHEN 'Others' THEN 15 ELSE -1 END
Upvotes: 4
Reputation: 4039
You can do the following:
select LastUpdated, Remarks
from table
where (remarks = 'Others' and datediff(min, LastUpdated, GETDATE()) > 15)
or remarks != 'Others'
You can adjust the TOP x
, according to your needs, since you didn't put it in your question so I didn't know how many rows you needed to select.
Upvotes: 4