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 |
|
hanavan
Starting Member
25 Posts |
Posted - 2008-03-11 : 18:52:05
|
| I have a database, and I need data from 3 tables.From the first table I just need the primary key: issueID.I can make a left outer join with my second table so I can have this result.select A.issueID, B.projectIDfrom issuerequest as A left outer join projects as B on (A.issueID=B.referenceID)group by A.issueIDgives:100 | null101 | P003102 | P002103 | null...When the value in the second column is null, I need to check the valuein the 3th table. The join between the first and the thirth is:select C.issueID, min(D.project) as Projectfrom issuerequest as C left outer join ProductCustomer on (C.customer = D.customerID) and (C.product=D.produktID)where actuellproduct='1'group by C.issueIDthis gives a result as:100 | P002100 | P003101 | P004102 | P002103 | null...What I actually want is just 2 columns within the first IssueID and in the second Projectand no duplicate data. Everything (except null) is moreimportant then the same value in column 3.100 | P002101 | P003102 | P002103 | nullI've tried with except, but can't get all duplicate data out. |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2008-03-11 : 22:01:33
|
[code]Select a.issueID,Coalesce(b.projectID,c.project) as ProjIDFROM issuerequest a Left join projects b on a.issueID=b.referenceID left join (Select customerID,min(project) as Project FROM productcustomer Group by customerID) c on a.Customer = c.CustomerID and a.Product = c.ProductID and actuellproduct ='1'[/code]You can join to both Table 2 and Table 3 in one Select. in this case I subquery to do the min(project) and alias as 'c' to use the column.The coalesce function will pick the first non-null value in the expression, so if b.ProjectId is null, it will go to c.ProjectMaybe that will help? Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
hanavan
Starting Member
25 Posts |
Posted - 2008-03-12 : 04:34:46
|
| Thanks for your help,couldn't have solved it without. Never heard of thecoalesce function.I didn't have an issueID in the productcustomer table,so couldn't use that but with adding customer and product itwas solved.Select a.issueID,Coalesce(b.projectID,c.project) as ProjIDFROM issuerequest a Left join projects b on a.issueID=b.referenceID left join (Select min(project) as Project, customerID, productID FROM productcustomer Group by customerID, productID where actuellproduct ='1') c on a.Customer = c.CustomerID and a.Product = c.ProductID select a.anfrageID, Coalesce(min(b.projektnummer),min(c.projekt)) as ProjectIDFrom mphotlineanfrage as a left join mpprojektbuchungserfassung as b on (a.anfrageID= b.quellreferenz) left join (select min(projekt) as projekt, adresse, produktID from mphotlineproduktkunde where produktaktuell='-1' group by adresse, produktID)as c on (a.adresse = c.adresse) and (a.produktID = c.produktID)group by anfrageIDorder by anfrageID |
 |
|
|
hanavan
Starting Member
25 Posts |
Posted - 2008-03-12 : 05:13:29
|
| double post |
 |
|
|
|
|
|
|
|