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

Inner/outer join

Status
Not open for further replies.

kreeben

Programmer
Nov 3, 2004
4
SE
I have a table with (amongst others) the columns "pid" and "countrycode" (char and varchar). Pid stands for "PageId". I want a recordset that includes the pid for the countrycode "se" and also includes the pid for the cuntrycodes "de" and "eu", if there are any pid:s for these countries. If a pid is missing from either "eu" or "de" I still want to include the row.

My table columns:

|countrycode|pid|

I want my recordset to look like this:

|pid|se.countrycode|de.countrycode|eu.countrycode|
_________________________________________________
|startpage|se|de|eu| (if "startpage" is defined for all countrycodes)

|startpage|se|de|null| (if "startpage" is defined for the countrycodes "se" and "de")

Am I making any sense?

It doesn´t matter if I use "INNER", "OUTER", "LEFT", "RIGHT" or "FULL" joins, i always get the same no good result.

Here´is my statement, where I have tried with only two different countrycodes:

SELECT se.pid AS PAGE, se.CountryCode AS SWEDEN, de.CountryCode AS GERMANY
FROM wa_PageContent se LEFT OUTER JOIN
wa_PageContent de ON se.pid = de.pid
WHERE (se.KeyPage = 1) AND (se.CountryCode = 'se') AND (de.KeyPage = 1) AND (de.CountryCode = 'de')
ORDER BY se.Script, se.pid, se.SortOrder
 
Well, to clarify, the statement in my last thread gives me a result where only those cases where pid exists for both "se" and "de" are included.

What I want is a complete list for countrycode = "se" and "de". If a pid is missing from "de" the the result should be NULL, or something.

Can anyone help me, or are y´all to busy following the election ;-)
 
Nevermind... I solved it by counting the number of instances of PageId form each CountryCode. If a PageId does not exist for a specific CountryCode then the result is ZERO.

SELECT pid AS PAGE,
(SELECT COUNT(*)
FROM wa_pagecontent
WHERE pid = base.pid AND countrycode = 'se') AS SWEDISH,

(SELECT COUNT(*)
FROM wa_pagecontent
WHERE pid = base.pid AND countrycode = 'eu') AS ENGLISH,

(SELECT COUNT(*)
FROM wa_pagecontent
WHERE pid = base.pid AND countrycode = 'de') AS GERMAN
FROM wa_PageContent base
WHERE (KeyPage = 1)
ORDER BY pid
 
Have you tried something like this ?
SELECT se.pid AS PAGE, se.CountryCode AS SWEDEN, de.CountryCode AS GERMANY
FROM wa_PageContent se LEFT OUTER JOIN (
SELECT pid, CountryCode FROM wa_PageContent
WHERE (KeyPage = 1) AND (CountryCode = 'de')
) de ON se.pid = de.pid
WHERE (se.KeyPage = 1) AND (se.CountryCode = 'se')
ORDER BY se.Script, se.pid, se.SortOrder


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top