Question : query question

I have a query that pulls projects from a table.  It is joined to several other tables including a table that list the customers who are bidding on the project.  There is a Guid column in the project table that lists the customer who won the job.  I would like my query to show all the customers in the projectaccounts table unless the job is awarded and has a value in the guid field and then it would only show the account it sold to.
Here at the tables

Projects
ProjectAccounts
Customers
The project table looks like
|Pr#| PrDesc | CustID |
The projectaccounts looks like
|Pr#| CustID|
The customer table looks like
|CustID| Customer Name|

I assume it will take an if/then of some type to get this right, but I don’t know how to do it.  Is this done in the select statement or the join??  The customer name that is showing is from the projects accounts table and that is correct unless the cusID field has a value.
1:
2:
3:
4:
5:
6:
7:
8:
9:
SELECT  PRProject.ProjectNr AS ProjectNo, PRProject.ParentProject AS PRParent, PRProject.Description AS PRDesc, 
                      PRProject.IDCustomer AS CusID, PRProject.Status AS PRStatus, dbo.cicmpy.cmp_name



FROM         PRProject 
LEFT OUTER JOIN ProjectAccounts on PRProject.ProjectNr = ProjectAccounts.Project
LEFT OUTER JOIN cicmpy on ProjectAccounts.Account = cicmpy.cmp_wwn
ORDER BY ProjectNr

Answer : query question

A stripped-down sample - add the extra columns you need

The Or clause in the left outer join is the bit that does what you need

Select PRProject .ProjectNr, C.CustID
from
PRProject
left outer join ProjectAccounts PA
     on (ProjectAccounts on PRProject.ProjectNr = ProjectAccounts.Project
     and (PRProject.CustId is NULL or PRProject.CustId = ProjectAccounts.CustID )
inner join Customer
    on (Customer.CustID = ProjectAccounts.CustId)
Random Solutions  
 
programming4us programming4us