Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL join
Message
 
 
To
09/02/2009 07:17:34
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Application:
Desktop
Miscellaneous
Thread ID:
01380251
Message ID:
01380337
Views:
46
Try
SELECT t2.*, 
		t1.SaleType,t2.TargetSales,t2.ActualSales,t2.IsAchieved
	FROM table2 t2 
		LEFT JOIN table1 t1 
   			ON t2.RegionCode = t1.RegionCode AND t2.DepotCode = t1.DepotCode 
   				AND t2.DistrictCode = t1.DistrictCode
>
>I have here 2 table structure and sample data in it and I would like to produce the output in the third table.
>Is there any idea how do I do it? I tried INNER JOIN and OUTER JOIN but unable to generate the output I wanted to.
>
>
>Table 1
>
>RegionCode	DepotCode	DistrictCode	SaleType	TargetSales	ActualSales	IsAchieved  
>REG001		DEP001		DIST001		Type 1 		5678 		2345 		False 
>REG001 		DEP001 		DIST001 	Type 2 		1234 		2345 		True 
>REG002 		DEP002 		DIST002 	Type 3 		444 		444 		True 
>REG002 		DEP002 		DIST002 	Type 2 		11 		12 		True 
>REG003 		DEP004 		DIST004 	Type 9 		789 		234 		False 
>
>
>Table 2
>
>RegionCode	DepotCode	DistrictCode	ValueType	TargetPercentage	ActualPercentage	ValueIsAchieved  
>REG001 		DEP001 		DIST001 	VAL001 		4 			2 			False 
>REG001 		DEP001 		DIST001 	VAL234 		4 			4 			True 
>REG001 		DEP001 		DIST001 	VAL455 		2 			1 			False 
>REG002 		DEP002 		DIST002 	VAL111 		5 			7 			True 
>
>
>Expected result:
>
>RegionCode	DepotCode	DistrictCode	SaleType	TargetSales	ActualSales	IsAchieved	ValueType	TargetPercentage	ActualPercentage	ValueIsAchieved
>REG001 		DEP001 		DIST001 	Type 1 		5678 		2345 		False 		VAL001 		4 			2 			False 
>REG001 		DEP001 		DIST001 	Type 2 		1234 		2345 		True 		VAL234 		4 			4 			True 
>REG001 		DEP001 		DIST001 	null 		null 		null 		null 		VAL455 		2 			1 			False 
>REG002 		DEP002 		DIST002 	Type 3 		444 		444 		True 		VAL111 		5 			7 			True 
>REG002 		DEP002 		DIST002 	Type 2 		11 		12 		True 		null 		null 			null 			null 
>REG003 		DEP004 		DIST004 	Type 9 		789 		234 		False 		null 		null 			null 			null 
>
>
> Any advise on what technique to use would be greatly apprecited.
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform