0

In the table there are several columns, one is a varchar column called "Country", and another is a boolean column called "SomeFlag", I want to filter all the countries those have "SomeFlag" both 0 and 1 from records.
For example,

Record1: country is US, SomeFlag is 0 and some other values.  
Record2: country is US, SomeFlag is 1 and some other values.  
Record3: country is CA, SomeFlag is 1 and some other values.  

So US is what I want to filter out, how should I construct this SQL query?
Thanks in advance.

3
  • I think you're saying to eliminate countries which have both row(s) with SomeFlag equal to 0 and other(s) with SomeFlag = 1. Right? Commented Feb 12, 2015 at 7:54
  • Yes buddy, that's it. I guess something went wrong with the expression, making it unclear to understand. Commented Feb 12, 2015 at 8:02
  • Well you didn't say the name of the table. Substitute that for T in any of my queries and I'll think you'll have it working. Commented Feb 12, 2015 at 8:04

3 Answers 3

1
select Country from T
group by Country
having min(cast(SomeFlag as int)) = max(cast(SomeFlag as int))

or

select Country from T
group by Country
having count(distinct SomeFlag) = 1

or

select Country from T
where not exists (
    select 1 from T
    where SomeFlag = 0
) or not exists (
    select 1 from T
    where SomeFlag = 1
)

or

select Country from T
where Country not in (
    select Country from T
    group by Country
    having
            min(cast(SomeFlag as int)) = 0
        and max(cast(SomeFlag as int)) = 1
)

Do nulls in SomeFlag come into play?

Sign up to request clarification or add additional context in comments.

Comments

0
"select * from table where (some_flag == '1' or some_flag == '0') and (country == 'US');"

1 Comment

I think it should be a loop to get all the countries.
0

It will be look like:

  SELECT *
    FROM Table
    WHERE (Table.SomeFlag == 0 OR Table.SomeFlag == 1) AND Table.Country == "US";

But if you don't want all information from the table use tags you want, except *.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.