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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query help

Status
Not open for further replies.

Cloonalt

Programmer
Jan 4, 2003
354
US
I have a table with multiple instances of Field 1. Field 2 may be 0 or 1, for instance:

Field 1 Field2
ABC Company 1
ABC Company 1
ABC Company 0
DEF Company 0
DEF Company 0
DEF Company 0

Can I write a query to get only Companies where there are all 0s?

Any help appreciated.

Thanks.
 
Thanks.

But in your query, I'll get companies who may have one instance of 0 and one instance of 1. I want to retrieve only companies where ALL records have 0 in the field 2 column.
 
Try this... The @Temp stuff is a table variable so I could dummy up some data.

Code:
Declare @Temp Table(CompanyName VarChar(50), Field2 Integer)

Insert Into @Temp Values('ABC Company',1)
Insert Into @Temp Values('ABC Company',1)
Insert Into @Temp Values('ABC Company',0)
Insert Into @Temp Values('DEF Company',0)
Insert Into @Temp Values('DEF Company',0)
Insert Into @Temp Values('DEF Company',0)

Select 	CompanyName, 
		Count(1) As RecordCount, 
		Sum(Case When Field2 = 0 Then 1 Else 0 End) As RecordsWith0
From 	@Temp 
Group By CompanyName
Having  Count(1) = Sum(Case When Field2 = 0 Then 1 Else 0 End)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Okay, you can also use not exists
Code:
Declare @Temp Table(CompanyName VarChar(50), Field2 Integer)

Insert Into @Temp Values('ABC Company',1)
Insert Into @Temp Values('ABC Company',1)
Insert Into @Temp Values('ABC Company',0)
Insert Into @Temp Values('DEF Company',0)
Insert Into @Temp Values('DEF Company',0)
Insert Into @Temp Values('DEF Company',0)

Select     * from @Temp t
where  Field2 =0 and not exists (select * from @Temp where Field2 =1 and t.CompanyName =CompanyName)

Denis The SQL Menace
SQL blog:
Personal Blog:
 
how about???
Code:
select companyname
from @temp
group by companyname
having sum(field2) = 0

Jim
 
Jim, since the OP didn't specify whether there could be negative numbers, I didn't go in that direction. However...

Code:
select companyname
from @temp
group by companyname
having sum([!]Abs([/!]field2[!])[/!]) = 0

You're solution probably makes the most sense.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Sorry, Field2 is text so sum won't work.

It's actually 'I' and 'O'
 
In that case... Back to Case...When

Code:
Declare @Temp Table(CompanyName VarChar(50), Field2 VarChar(1))

Insert Into @Temp Values('ABC Company','I')
Insert Into @Temp Values('ABC Company','I')
Insert Into @Temp Values('ABC Company','O')
Insert Into @Temp Values('DEF Company','O')
Insert Into @Temp Values('DEF Company','O')
Insert Into @Temp Values('DEF Company','O')

Select 	CompanyName, 
From 	@Temp 
Group By CompanyName
Having  Count(1) = Sum(Case When Field2 = 'O' Then 1 Else 0 End)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
why not ;-)
Code:
Declare @Temp Table(CompanyName VarChar(50), Field2 VarChar(1))

Insert Into @Temp Values('ABC Company','I')
Insert Into @Temp Values('ABC Company','I')
Insert Into @Temp Values('ABC Company','O')
Insert Into @Temp Values('DEF Company','O')
Insert Into @Temp Values('DEF Company','O')
Insert Into @Temp Values('DEF Company','O')

select companyname
from @temp
group by companyname
having sum(convert(int,replace(replace(field2,'I','1'),'O','0'))) = 0

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Or....

Having Count(1) = Sum(Ascii(Field2)-78)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Or...

Having Count(1) = Count(NullIf(Field2, 'I'))

[bigsmile]

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
george, that's very nice

and i must say, this is a most interesting thread

is there any difference here between count(*) and count(1)? ;-)

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top