Solved

Select Statement, MS Sql Server

Posted on 2007-11-27
4
139 Views
Last Modified: 2010-03-19
Good afternoon,

I've got a table in my database that has 3 relationships to the same table (you can enter 3 states, so it has a relationship from like addressstateID to StateID in the state table, and one for billingstateaddress, etc).

In the table itself, I have 2 test columns entered.  However, when I run my select statement it's pulling back 5,000 results.  Everything is identical, except for Billing State and Shipping State.  It'll pull back one record for Alabama and Alabama.  Then the next record will be identical except it'll pull back Alabama and Alaska, and it keeps going through this for each and every possible combination of states, even though there are only 2 actual records in the database.

Doing my own troubleshooting, it seems to be narrowed down to the CROSS JOIN - if I remove the CROSS JOIN, then it only pulls back 2 records - but it only pulls back the StateID not the state names like I need.

I did something very similiar on another page, and it works flawless, and after 3 hours of looking through everything I can't figure out what would cause this.  Has anybody else run into this before and would know what is causing it?  I double checked the relationships and everything else, and I can't for the life of me figure it out.
SELECT     a.VendorLocationId, a.VendorId, a.Address, a.City, a.StateId, a.ZipCode, a.ShippingAddress, a.ShippingCity, a.ShippingStateId, a.ShippingZipCode, 
                      a.BillingAddress, a.BillingCity, a.BillingStateId, a.BillingZipCode, a.PrimaryPhone, a.SecondaryPhone, a.Fax, a.Email, b.State, c.State AS BillingState, 
                      d.State AS ShippingState
FROM         VendorLocation AS a CROSS JOIN
                      State AS b CROSS JOIN
                      State AS c CROSS JOIN
                      State AS d
WHERE     (a.VendorId = @VendorID) AND (a.StateId = b.StateId OR
                      a.StateId IS NULL) AND (a.BillingStateId = c.StateId OR
                      a.BillingStateId IS NULL) AND (a.ShippingStateId = d.StateId OR
                      a.ShippingStateId IS NULL)

Open in new window

0
Comment
Question by:arhame
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20360976
I highly doubt you want a CROSS JOIN ... but a LEFT JOIN will do perfectly
SELECT     a.VendorLocationId, a.VendorId, a.Address, a.City, a.StateId, a.ZipCode, a.ShippingAddress, a.ShippingCity, a.ShippingStateId, a.ShippingZipCode, 
                      a.BillingAddress, a.BillingCity, a.BillingStateId, a.BillingZipCode, a.PrimaryPhone, a.SecondaryPhone, a.Fax, a.Email, b.State, c.State AS BillingState, 
                      d.State AS ShippingState
FROM      VendorLocation AS a 
LEFT JOIN State AS b 
   ON (a.StateId = b.StateId)
LEFT JOIN State AS c 
   ON (a.BillingStateId = c.StateId)
LEFT JOIN State AS d
   ON (a.ShippingStateId = d.StateId)
WHERE     (a.VendorId = @VendorID) 
                      
       

Open in new window

0
 
LVL 8

Author Comment

by:arhame
ID: 20361051
Ah, that did the trick.  I actually just put a , in the select statement like:
FROM      VendorLocation                      a
      ,State                  b
      ,State                  c
      ,State                  d

But visual studio 2005 kept putting in the CROSS JOIN.

Visual Studio forced yours to a left outter join, but it's pulling the proper results.  That shouldn't matter should it?  Left Outer join instead of just left join?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20361064
LEFT JOIN and LEFT OUTER JOIN are indeed the same thing, OUTER being optional keyword
0
 
LVL 8

Author Comment

by:arhame
ID: 20361071
Thanks for your quick help :)  Points awarded.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 82
Selection from table2 where criteria for table1 10 69
Sql query 107 102
SQL Server 2005 - Comparing Fields' Contents 6 58
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question