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 |
|
avarair
Starting Member
3 Posts |
Posted - 2007-01-09 : 21:06:32
|
| Hi all,Sorry if it looks a little cluttered!I have these two tables: CAMPAIGN(MEDIACODE varchar(10) UncheckedSPECIALOFFERCODE varchar(15) UncheckedLAYOUT varchar(10) UncheckedHEADERTEXT varchar(100) CheckedSORTORDER varchar(10) UncheckedSORTORDERCOLUMN varchar(50) UncheckedWIDTH varchar(50) Checked)andPROMORATEVIEW(MEDIACODE varchar(10) CheckedSPECIALOFFERCODE varchar(15) CheckedCAMPAIGNCODE varchar(15) CheckedNUMBEROFISSUES smallint CheckedRATE decimal(9, 0) CheckedDESPATCHMETHODCODE varchar(50) Checked)CAMPAIGN HAS ONLY ONE ROW:(CE DG8398 GRID ASC NUMBEROFISSUES NULL)ANDPROMORATEVIEWTOO MANY AND HERE A VERY SMALL RANGE OF RECORDSMEDIACODE SPECIALOFFERCODE CAMPAIGNCODE NUMBEROFISSUES RATE DESPATCHMETHODCODE(...CE CER1R02 CER1 12 429 WCE CER1R03 CER1 24 829 WCE CER1R03 CER1 12 429 WCE DG8398 DG8398 12 411 FCE DG8398 DG8398 12 405 1CE DG8399 DG8399 12 399 WCE DG8399 DG8399 12 735 1CE DG8399 DG8399 12 756 ACE DG8400 DG8400 12 756 ACE DG8400 DG8400 12 396 W...)Now the question:Why these two OUTER JOINS RETURN the same number of rows in My Sql2000 & 2005 express???1.SELECT DISTINCT P.MEDIACODE, P.SPECIALOFFERCODE,CD.MEDIACODEFROM CAMPAIGN AS CD RIGHT OUTER JOINPROMORATEVIEW AS P ON P.MEDIACODE = CD.MEDIACODE AND P.SPECIALOFFERCODE = CD.SPECIALOFFERCODE2.SELECT DISTINCT P.MEDIACODE, P.SPECIALOFFERCODE,CD.MEDIACODEFROM CAMPAIGN AS CD RIGHT OUTER JOINPROMORATEVIEW AS P ON P.MEDIACODE = CD.MEDIACODE AND P.SPECIALOFFERCODE = CD.SPECIALOFFERCODE AND CD.SORTORDER IS NULLIf you still with me here is what I am trying to do:to right outer join the campaign with promorateview and the if it's not overriden in campaign( there is no record for MEDIACODE, SPECIALOFFERCODE in campaign ) I will use that view to override the default values.I suspect that the last statement makes no "influence" on right outer join.Another work around for me now is that if I create a view that has these values and then filter it with VIEW.SORTORDER IS NULL it does the job but I am trying to get rid of this extra view.Thanks for your time, avarair |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 01:00:43
|
| Because you are using OUTER JOIN.Peter LarssonHelsingborg, Sweden |
 |
|
|
avarair
Starting Member
3 Posts |
Posted - 2007-01-10 : 01:19:13
|
| Hi Peso,thanks for the answer, could you, pleaze a little clearer define how outer join works and if my assumption in the post was correct(that it pulls all the records any way!?)Thanks,Artur |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-10 : 01:31:03
|
| Which OUTER JOIN? Left, right or full?They are all fully explained in Books Online.Also, since you are binding the JOIN with MediaCode, if record is matching, the value is the same!Post your expected output based on the sample data provided above.Peter LarssonHelsingborg, Sweden |
 |
|
|
avarair
Starting Member
3 Posts |
Posted - 2007-01-11 : 05:47:31
|
Hi Peso,thanks for your time!After spending some time on this and consulting with my coworker I figured out that my problem was in the precedence of how the statement is carried out.Right/Left Outer Join gives the NULL records attached and in order to get only the records that are the "NULLs" we simply need to restrict it by WHERE statement, not the condition IS NULL with the AND operator in the JOIN!Thanks again,I think I explained clearer what result I was looking for in the query after we figured where the mistake was!Avarairquote: Originally posted by Peso Which OUTER JOIN? Left, right or full?They are all fully explained in Books Online.Also, since you are binding the JOIN with MediaCode, if record is matching, the value is the same!Post your expected output based on the sample data provided above.Peter LarssonHelsingborg, Sweden
|
 |
|
|
|
|
|
|
|