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 |
|
borg_jos
Starting Member
1 Post |
Posted - 2010-10-03 : 10:53:04
|
| I have 2 tables:Table_A has 3 records:REG_NUMBER CAR_MAKEABC123 CAR1DEF456 CAR2XYZ999 CAR3Table_B has 2 records:REB_NUMBER DISTANCE_TRAVELLEDDEF456 5XYZ999 20I need a query to display for each record in Table A how much distance has travelled. However if a record has travelled no distance (ie no record is created in Table_B) I require that the query does not ommit such record however it displays a 0.Example:ABC123 CAR1 0DEF456 CAR2 5XYZ999 CAR3 20 With the below query as you can see it omits ABC123:SELECT A1.*,A2.DISTANCE_TRAVELLED FROM TABLE_A A1, TABLE_B A2WHERE A1.REG_NUMBER = A2.REG_NUMBERCan someone please help? |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2010-10-03 : 12:26:58
|
| SELECT A1.*, IsNull(A2.DISTANCE_TRAVELLED,0)FROM TABLE_A A1LEFT JOIN TABLE_B A2 ON A1.REG_NUMBER = A2.REG_NUMBER |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-10-04 : 09:35:30
|
| can there be multiple records for same REG_NUMBER in table_B? ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|