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 |
|
sinky
Starting Member
1 Post |
Posted - 2008-08-04 : 12:30:18
|
| Hi,I have three tables:useruserpetpetpetcategoryhow can i tell if a user has a whole category of pets.I can use:Select * form pet where petid in (select * from userpet where userid = 8)now i have all the records i think i needs with example results below: petid:1petdesc:catpetcat:1petid:2petdesc:dogpetcat:1now say category 1 only had two pets - how can i show this user has all the pets in the category.Any help would be much appreciated.Sinky |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-04 : 12:57:17
|
no need of cursor. just use like thisSELECT u.userIDFROM user uJOIN userpet upON up.UserID=u.UserIDJOIN pet pON p.petID=up.petIDJOIN petcategory pcON pc.categoryID=p.categoryIDJOIN (SELECT categoryID,COUNT(DISTINCT petID) AS petcount FROM petcategory GROUP BY categoryID)tON t.categoryID=pc.categoryIDGROUP BY userIDHAVING COUNT(DISTINCT up.PetID) =MAX(petcount) |
 |
|
|
|
|
|