Jump to content

Join problem


TapeGun007
Go to solution Solved by Barand,

Recommended Posts

Prospects

---------

ProspectCode

ProspectBusinessName

ProspectLastContacted

ProspectCounty

ProspectZip

ProspectState

 

Territories

-----------

TerritoryID

TerritoryCode (Should always match ProspectCode above)

Territory (should always be a county name)

TerritoryState

TerritoryZip

 

I want to join these two tables under these circumstances:

 

(

ProspectCounty = Territory County AND ProspectState = TerritoryState

OR

ProspectZip = TerritoryZip

)

 

AND

ProspectLastContacted was last contacted 2 weeks ago.

 

Link to comment
Share on other sites

Here at least one of my attempts at this:

SELECT * 
                        FROM Prospects p
                        LEFT JOIN Territories t
                        ON p.ProspectCounty = t.Territory
                        WHERE (ProspectCounty = Territory
                            AND ProspectState = TerritoryState)
                        AND DATE(ProspectLastContacted) < DATE(NOW() - INTERVAL 14 DAY)
                        ORDER BY p.BusinessName

I won't use * once I get the right code, I realize that's not good.  I just realized I forgot to put the OR in there.

Edited by TapeGun007
Link to comment
Share on other sites

I attempted this just to see if I could narrow it down.  I realize I probably need to join based on State names and not counties.

 

 

"SELECT * 
                        FROM Prospects p
                        LEFT JOIN Territories t
                        ON 
                            p.ProspectState = t.TerritoryState
                        OR
                            t.ProspectZip = t.TerritoryZip
                        ";

 

This yields nothing.

Link to comment
Share on other sites

Hi Barand,

 

Just to clarify... in the table "Territories" there is either a Territory (County) AND State set, OR just a Zip code with no State or County set.

 

I checked my "Prospects" table to ensure they all have a "ProspectLastContacted" date that is at least 2 - 4 weeks ago.

 

I tried your code and it give me nothing in return.

Edited by TapeGun007
Link to comment
Share on other sites

It's a confusing data model that you have there.

 

 

TerritoryCode (Should always match ProspectCode above)

 

That implies that each prospect can have many territories, but it seem more likely that a territory would have many prospects and the model would be more like this

+----------------------+             +--------------+
| Prospects            |             |  Territory   |
+----------------------+             +--------------+
| ProspectCode         |     +-------|  TerritoryID |
| ProspectBusinessName |     |       |  County      |
| ProspectLastContacted|     |       |  State       |
| TerritoryID          |>----+       |  Zip         |
+----------------------+             +--------------+
Link to comment
Share on other sites

Yeah, I realized when I first wrote that .... that it wasn't the ID that needed to match.  I get confused sometimes because normally I would match an ID.  But the Sales People are assigned a "ReferralCode" that is unique to them already.

 

It really goes like this:

 

 
Sales                 |  Prospects  |       |  Territories  |
--------------              ------------------      -----------------
SalesCode
 
 
Link to comment
Share on other sites

Somehow I clicked submit on accident.

 

I use a "code" because the code is unique to each sales person and is assigned by another database that I have no control over.  So instead of using a SalesID, I use a SalesCode, ProspectCode, and TerritoryCode.

 

So the SalesCode must match in the TerritoryCode.  In some rare cases I may let a sales person keep a lead that is not in their territory.

 

Here is how the table actually works:

tables.gif

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.