|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
NOT EXISTS? Finding records without a correlating record in another tableI'm working with (essentially) two tables here.
Magazine.(MagazineID, PK) and Ratedetail.(ratedetailID, PK)(magazineID, FK) I need a statement that returns all the magazineIDs that do not have a ratedetailid. I'm assuming not exists is what i'm looking to use, but am not having any luck with it. I don't think I need to supply any dummy data or go into more details with the schema... but I will if requested.... Thanks in advance, Krb > I'm working with (essentially) two tables here. SELECT m.* FROM> Magazine.(MagazineID, PK) and Ratedetail.(ratedetailID, PK)(magazineID, > FK) > > I need a statement that returns all the magazineIDs that do not have a > ratedetailid. I'm assuming not exists is what i'm looking to use, but > am not having any luck with it. Magazine m LEFT OUTER JOIN RateDetail r ON m.MagazineID = r.MagazineID WHERE r.MagazineID IS NULL; or SELECT * FROM Magazine m WHERE NOT EXISTS (SELECT MagazineID FROM RateDetail r WHERE r.MagazineID = m.MagazineID); > I don't think I need to supply any dummy data or go into more details This should be automatic, imho, but I realize that's a lofty goal. I don't > with the schema... but I will if requested... think most people understand how much easier it makes it to run your existing schema, test our queries against it, and make sure we provide you with the right answer, instead of making all kinds of guesses about what you want returned, etc. We don't ask for DDL and sample data just to mak you work harder to get an answer, we actually do it to save everybody time and allow us to help more people in a day. Aaron Bertrand [SQL Server MVP] wrote:
> This should be automatic, imho, but I realize that's a lofty goal. I don't I appreciate your time and effort, and do realize that sample> think most people understand how much easier it makes it to run your > existing schema, test our queries against it, and make sure we provide you > with the right answer, instead of making all kinds of guesses about what you > want returned, etc. We don't ask for DDL and sample data just to mak you > work harder to get an answer, we actually do it to save everybody time and > allow us to help more people in a day. data/schema info is quite important, but I thought my question was basic enough to get query I was looking for. I believe I will be able to take your (2nd) suggestion and compile my own query to achieve my desired output. I also don't want people to do "all" of my work for me, but rather point me in the right direction so that I still have to put some effort into learning how things actually work. Thanks again, Krb. > I appreciate your time and effort, and do realize that sample Yes, it was, I was speaking more in the general sense. That doesn't mean > data/schema info is quite important, but I thought my question was > basic enough to get query I was looking for. including it would make our lives more difficult. It just means that if you don't include it, you may get untested and/or undesired solutions. A |
|||||||||||||||||||||||