Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NOT EXIST within same table 1

Status
Not open for further replies.

Belle19

Programmer
Jun 2, 2006
11
US
I need to pull check_numbers out of a table that have a fl_flag = '1' but to exclude them if there is a row in the same table with the same check_number but the fl_flag = '0'.

How would I go about doing this in an efficient way??? I am trying to use the NOT EXIST but not sure if this is the best way. Please advice.
 
Maybe this...

Code:
Select check_number
From   table
Group By check_number
Having Count(*) = Sum(fl_flag)

This assumes that the only acceptable values in the field are 1 and 0. Basically, the number of records (per check_number) must match the sum of the flag column.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Belle,
You don't need the NOT EXISTS.

This will work if I understand what you want.

SELECT checknumber
FROM table
WHERE fl_flag = 1

If the value in the fl_flag field is an int you don't need the quotes.
 
I think I might have not explained this completely. I need the check_number to set to a report only if there isn't ANY rows in the table where the check_number = '0'. I can't just do where check_number = '1' cause then there might be one out there where the check_number is the same but the fl_flag might be '0' on the entry. I am almost sure the only options are 1 and 0 but do not want to rely on this just in case. There might be other values the user wants to put in there.

Also the fl_flag field is a text field so the quotes are needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top