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 |
davidais
Starting Member
17 Posts |
Posted - 2012-10-03 : 07:08:48
|
Hi all,I am trying to join two columns [AccountNo] and [SubAddress] from two tables [tbOrderHistory] and [dbo.uvTemp_PP_Projects].The columns need to be specified as left joins from [tbOrderHistory] as both [AccountNo] and [SubAddress] are unique to the [ProjectName] in [dbo.uvTemp_PP_Projects].When joining I had to use correlation names to distinguish between them [PP1] and [PP2].The result produces values that are either unique to [PP1] or [PP2] depending on which one I select - however I need the unique [ProjectName] based on the combination of both joins.SELECTtbGRNHistory.SupplierAccNo, tbGRNHistory.GRNInDate, tbGRNHistory.PONumber, tbOrderHistory.SONumber, tbOrderHistory.AccountNo as CustAccNo, tbCustomers.CustomerName, tbOrderHistory.SubAddressCode, PP2.ProjectName, tbGRNHistory.ValueFROMtbGRNHistoryleft join tbPOrderHistory on tbGRNHistory.PONumber=tbPOrderHistory.PONumberleft join tbOrderHistory on tbPOrderHistory.SONumber=tbOrderHistory.SONumberleft join tbCustomers on tbOrderHistory.AccountNo=tbCustomers.AccountNumberLeft join dbo.uvTemp_PP_Projects as PP1 on tbOrderHistory.AccountNo=PP1.AccountNoLeft join dbo.uvTemp_PP_Projects as PP2 on tbOrderHistory.SubAddressCode=PP2.SubAddressGROUP BYtbGRNHistory.SupplierAccNo, tbGRNHistory.GRNInDate, tbGRNHistory.PONumber, tbOrderHistory.SONumber, tbOrderHistory.AccountNo, tbCustomers.CustomerName, tbOrderHistory.SubAddressCode, tbGRNHistory.Value, PP2.ProjectName Many thanks,D |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-03 : 07:32:14
|
Add another column to your select list using coalesce:SELECTCOALESCE(pp1.ProjectName,pp2.ProjectName) AS TheProjectName,... your other columnsFROMtbGRNHistoryleft join tbPOrderHistory on tbGRNHistory.PONumber=tbPOrderHistory.PONumberleft join.... |
|
|
davidais
Starting Member
17 Posts |
Posted - 2012-10-03 : 08:32:08
|
thanks but I'm afraid this does not work, [ProjectName] can have both multiple instances of [AccountNo] and [SubAddress], I need to the unique combination of these two fields in the resultsthanks |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-10-03 : 08:39:59
|
Let us see if second time is the charm :) If the following does not work, can you post some representative sample data? In the code below, you have to specify some ordering criterion (see "SomeColumn" in the query) and it will pick the first row based on that ordering criterion.;WITH cte AS(SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(pp1.ProjectName,pp2.ProjectName) ORDER BY SomeColumn) AS RN, tbGRNHistory.SupplierAccNo, tbGRNHistory.GRNInDate, tbGRNHistory.PONumber, tbOrderHistory.SONumber, tbOrderHistory.AccountNo AS CustAccNo, tbCustomers.CustomerName, tbOrderHistory.SubAddressCode, PP2.ProjectName, tbGRNHistory.ValueFROM tbGRNHistory LEFT JOIN tbPOrderHistory ON tbGRNHistory.PONumber = tbPOrderHistory.PONumber LEFT JOIN tbOrderHistory ON tbPOrderHistory.SONumber = tbOrderHistory.SONumber LEFT JOIN tbCustomers ON tbOrderHistory.AccountNo = tbCustomers.AccountNumber LEFT JOIN dbo.uvTemp_PP_Projects AS PP1 ON tbOrderHistory.AccountNo = PP1.AccountNo LEFT JOIN dbo.uvTemp_PP_Projects AS PP2 ON tbOrderHistory.SubAddressCode = PP2.SubAddressGROUP BY tbGRNHistory.SupplierAccNo, tbGRNHistory.GRNInDate, tbGRNHistory.PONumber, tbOrderHistory.SONumber, tbOrderHistory.AccountNo, tbCustomers.CustomerName, tbOrderHistory.SubAddressCode, tbGRNHistory.Value, PP2.ProjectName)SELECT * FROM cte WHERE RN=1; |
|
|
davidais
Starting Member
17 Posts |
Posted - 2012-10-03 : 10:52:33
|
perhaps this will help explain a little differently - when it is done in Access the code looks like this: LEFT JOIN dbo_uvTemp_PP_Projects ON (dbo_uvOrderHistoryLatest.AccountNo = dbo_uvTemp_PP_Projects.AccountNo) AND (dbo_uvOrderHistoryLatest.SubAddressCode = dbo_uvTemp_PP_Projects.SubAddress) |
|
|
|
|
|
|
|