Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
LemonDrops
Starting Member
2 Posts |
Posted - 2011-03-15 : 23:51:41
|
Hi Sorry a lousy subject description.I have a table with three columnsColumn one is unique values (Animal_Id)Column two is one of two values (Animal_Type) pig or cowColumn three is a non-unique value, numeric (Barn_ID) 100, 200, 300, etcColumn three's value is the key in another table that I don't think is relevant here (Barn_Location)There are no nulls.Some barns have only pigs, some only cows, and some both pigs and cows.I want to find out which barns have only pigs.The table looks like this:Animal_id Animal_Type Barn_ID1 cow 3002 cow 2003 pig 3004 cow 2005 cow 1006 pig 4007 pig 3008 cow 2009 pig 40010 pig 20011 cow 30012 cow 20013 pig 30014 cow 20015 cow 10016 pig 40017 cow 30018 cow 20019 pig 40020 pig 20021 cow 30022 cow 20023 pig 30024 cow 20025 cow 10026 pig 40027 cow 30028 cow 20029 pig 400IF I doselect count(animal_type), barn_id, animal_typefrom animals group by barn_id, animal_type;That obviously isn't the answer since it doesn't sort out only pigs. And if I try adding "where animal-type='pig'" THAT isn't right either because the results include barns that also have cows.And what would have been a more accurate subject line description? And - it reads like a homework assignment, but it isn't. I'm self learning on the job.Thanks  |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-03-16 : 01:44:35
|
| One way:Create table #Test(Animal_id int, Animal_Type varchar(50), Barn_ID int)truncate table #TestInsert into #Testselect 1 ,'cow', 300 union allselect 2 ,'cow', 200 union allselect 3 ,'pig', 300 union allselect 4 ,'cow', 200 union allselect 5 ,'cow', 100 union allselect 6 ,'pig', 400 union allselect 7 ,'pig', 300 union allselect 8 ,'cow', 200 union allselect 9 ,'pig', 400 union allselect 10 ,'pig', 200 union allselect 11 ,'cow', 300 union allselect 12 ,'cow', 200 union allselect 13 ,'pig', 500 union allselect 14 ,'zebra', 600 union allselect 14 ,'pig', 600 Select Barn_id from #Test group by Barn_idhaving Count(distinct Animal_type) = 1 and max(Animal_type)='pig' |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-03-16 : 17:24:12
|
| SELECT barn_id FROM Test GROUP BY barn_id HAVING MAX(animal_type) = MIN(animal_type) AND MAX(animal_type) = 'pig';--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-03-16 : 18:40:50
|
| [code]select distinct barn_id --You want barn_ids, no duplicatesfrom animals pigswhere animalType = 'pig' --these are pigs, you display their barns and not exists(select * from animals cows where cows.barn_id = pigs.barn_id )--you do not want to include barns that are linked to cows too[/code]MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
LemonDrops
Starting Member
2 Posts |
Posted - 2011-03-16 : 20:41:38
|
| Interesting variations in solutions. Thanks for solving my problem! Got me to look up "not exists" too. |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-03-18 : 02:31:50
|
quote: Originally posted by mmarovic
select distinct barn_id --You want barn_ids, no duplicatesfrom animals pigswhere animalType = 'pig' --these are pigs, you display their barns and not exists(select * from animals cows where cows.barn_id = pigs.barn_id )--you do not want to include barns that are linked to cows too MirkoMy blog: http://mirko-marovic-eng.blogspot.com/
Sorry, there was an error in code, the correct one is:select distinct barn_id --You want barn_ids, no duplicatesfrom animals pigswhere animalType = 'pig' --these are pigs, you display their barns and not exists(select * from animals cows where cows.barn_id = pigs.barn_id and animalType = 'cow' )--you do not want to include barns that are linked to cows too MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
|
|
|
|
|