Home All Groups Group Topic Archive Search About

selecting records based on details in related tables

Author
6 Apr 2006 6:45 PM
KBuser
The important schema:
TBL: ISSUE
PK:  IssueID

One to many relation to:
TBL: PAGE
PK:  PageID
FK:  IssueID

One to many relation to the table with values I'm checking:
TBL: PAGEDETAILS
PK:  PageDetailsID
FK:  PageID
BrandID
RateDetailID

So each issue can have many pages, and each page can have many page
details.
I want to select the Issues which have no related pagedetails with null
values in brandid or ratedetailid.

I've tried many variations of the following query, but to no avail. I
can't seem to wrap my head around the logic.

SELECT ISSUE.IssueID from ISSUE where ISSUE.IssueID not in(SELECT
ISSUE.IssueID
FROM         ISSUE INNER JOIN
                      PAGE ON ISSUE.IssueID = PAGE.IssueID INNER JOIN
                      PAGEDETAILS ON PAGE.PageID = PAGEDETAILS.PageID
WHERE     (PAGEDETAILS.BrandID IS NULL) OR (PAGEDETAILS.RateDetailID IS
NULL))


I've tried with outer joins, using not exists, and a couple other
things, but can't seem to get it to work properly. Does anyone see the
flaw in my query?

Author
6 Apr 2006 9:47 PM
ML
Please post DDL, sample data might also help, so we don't have to do
*everything*. :)


ML

---
http://milambda.blogspot.com/
Author
6 Apr 2006 9:50 PM
MGFoster
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tried this?

SELECT I.IssueID
FROM (Issue As I INNER JOIN Page As P ON I.IssueID = P.IssueID)
LEFT JOIN PageDetails As PD ON P.PageID = PD.PageID
WHERE PD.PageID IS NULL

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBRDWNGoechKqOuFEgEQIWqgCgy35hV+XqH/DVwugEt1xqQVIpxX0AoKVe
d/AGWoz7YwJRaQEbRIrv95EA
=IVIQ
-----END PGP SIGNATURE-----


KBuser wrote:
Show quote
> The important schema:
> TBL: ISSUE
> PK:  IssueID
>
> One to many relation to:
> TBL: PAGE
> PK:  PageID
> FK:  IssueID
>
> One to many relation to the table with values I'm checking:
> TBL: PAGEDETAILS
> PK:  PageDetailsID
> FK:  PageID
> BrandID
> RateDetailID
>
> So each issue can have many pages, and each page can have many page
> details.
> I want to select the Issues which have no related pagedetails with null
> values in brandid or ratedetailid.
>
> I've tried many variations of the following query, but to no avail. I
> can't seem to wrap my head around the logic.
>
> SELECT ISSUE.IssueID from ISSUE where ISSUE.IssueID not in(SELECT
> ISSUE.IssueID
> FROM         ISSUE INNER JOIN
>                       PAGE ON ISSUE.IssueID = PAGE.IssueID INNER JOIN
>                       PAGEDETAILS ON PAGE.PageID = PAGEDETAILS.PageID
> WHERE     (PAGEDETAILS.BrandID IS NULL) OR (PAGEDETAILS.RateDetailID IS
> NULL))
>
>
> I've tried with outer joins, using not exists, and a couple other
> things, but can't seem to get it to work properly. Does anyone see the
> flaw in my query?
>

AddThis Social Bookmark Button