lyonsja
lyonsja

Reputation: 11

SQL Query conditions

I have a table with around 90 columns but only three of which I an interested in. Let's call them code, date1 and date2; code has integer values 1 to 7 and date1 and date2 are standard format dates (YYYY-MM-DD).

I need to take all rows with code 3, 4 and 5 and rows in which code is 1 or 2 and date1 is not the same as date2.

How can I run this query?

I'm using RStudio's RODBC package. I know I can just use R's rbind function on two separate queries but I would prefer to avoid this.

Any help will be much appreciated.

Upvotes: 0

Views: 71

Answers (2)

venkatesh
venkatesh

Reputation: 151

select code,date1,date2
from your_table
where code in (3,4,5)
or (code in (1,2) and date1 != date2)

Upvotes: 1

JohnHC
JohnHC

Reputation: 11195

Us an in for the codes, then use an or for the special condition

select *
from MyTable
where code in (3,4,5)
or (code in (1,2) and date1 <> date2)

Upvotes: 3

Related Questions