Sorry for the long description, but here goes:
I have four tables:
Opportunity
---------------------
OppID ProjectID
---------------------
1 44
2 45
Project
---------------------
ProjID Title
---------------------
44 Project1
45 Project2
ProjectDealers
---------------------
ProjectID DealerID
---------------------
44 100
44 101
45 101
Dealers
--------------------
DealerID DealerName
--------------------
100 Walmart
101 K-mart
My current select statment is:
SELECT b.OppID, a.Title, d.DealerName
FROM Project a
LEFT JOIN Opportunity b ON a.ProjectID = b.ProjectID
LEFT JOIN ProjectDealers c ON a.ProjectID = c.ProjectID
LEFT JOIN Dealers d ON c.DealerID = d.DealerID
My current result set is:
OppID Title DealerName
-------------------------------------------
1 Project1 Walmart
1 Project1 K-mart
2 Project2 K-mart
What I need the select statement to do is restrict the results to one dealer per project, so my results would be:
OppID Title DealerName
-------------------------------------------
1 Project1 Walmart
2 Project2 K-mart
I am unsure how to accomplish this, as I am fairly new to SQL. Any ideas would be most appreciated. Thanks.
I have four tables:
Opportunity
---------------------
OppID ProjectID
---------------------
1 44
2 45
Project
---------------------
ProjID Title
---------------------
44 Project1
45 Project2
ProjectDealers
---------------------
ProjectID DealerID
---------------------
44 100
44 101
45 101
Dealers
--------------------
DealerID DealerName
--------------------
100 Walmart
101 K-mart
My current select statment is:
SELECT b.OppID, a.Title, d.DealerName
FROM Project a
LEFT JOIN Opportunity b ON a.ProjectID = b.ProjectID
LEFT JOIN ProjectDealers c ON a.ProjectID = c.ProjectID
LEFT JOIN Dealers d ON c.DealerID = d.DealerID
My current result set is:
OppID Title DealerName
-------------------------------------------
1 Project1 Walmart
1 Project1 K-mart
2 Project2 K-mart
What I need the select statement to do is restrict the results to one dealer per project, so my results would be:
OppID Title DealerName
-------------------------------------------
1 Project1 Walmart
2 Project2 K-mart
I am unsure how to accomplish this, as I am fairly new to SQL. Any ideas would be most appreciated. Thanks.