| Author |
Topic  |
|
|
nhaas
Yak Posting Veteran
USA
90 Posts |
Posted - 01/16/2013 : 17:57:28
|
I have a simple Query:
select voipnumber, type from VOIPNumbersMonthly ORDER BY Type, ORG
it returns 8700 rows, which are correct. Now I want to add a cost per type. I add the join:
select voipnumber, type,VoipPhones.CC710 from VOIPNumbersMonthly inner join VoipPhones ON VOIPNumbersMonthly.type = VoipPhones.PModel ORDER BY Type, ORG
but I only get 2428 rows.
What could be the cause of the issue?
Thank you |
|
|
jimf
Flowing Fount of Yak Knowledge
USA
2868 Posts |
Posted - 01/16/2013 : 18:45:29
|
I'm not even sure how your first query works, since there is no ORG to order by, same as the 2nd query. In your 2nd query, that just means that there 2428 rows in VOIPNumbersMonthly that have a record in VoipPhones based on type = Pmodel. Try it with a LEFT JOIN and see what you get.
Jim
Everyday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47960 Posts |
Posted - 01/16/2013 : 23:58:06
|
quote: Originally posted by nhaas
I have a simple Query:
select voipnumber, type from VOIPNumbersMonthly ORDER BY Type, ORG
it returns 8700 rows, which are correct. Now I want to add a cost per type. I add the join:
select voipnumber, type,VoipPhones.CC710 from VOIPNumbersMonthly inner join VoipPhones ON VOIPNumbersMonthly.type = VoipPhones.PModel ORDER BY Type, ORG
but I only get 2428 rows.
What could be the cause of the issue?
Thank you
the reason is simple. you dont have matching entries for the missing 6000+ records. inner join will return result only if it finds a match based on condition. If you want result regardless of match use left join instead ie
select voipnumber, type,VoipPhones.CC710
from VOIPNumbersMonthly
innerleft join VoipPhones
ON VOIPNumbersMonthly.type = VoipPhones.PModel
ORDER BY Type, ORG
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47960 Posts |
Posted - 01/16/2013 : 23:59:26
|
quote: Originally posted by jimf
I'm not even sure how your first query works, since there is no ORG to order by, same as the 2nd query. In your 2nd query, that just means that there 2428 rows in VOIPNumbersMonthly that have a record in VoipPhones based on type = Pmodel. Try it with a LEFT JOIN and see what you get.
Jim
Everyday I learn something that somebody else already knew
sorry i didnt get that explanation I feel its correct so far as OP has field ORG in VOIPNumbersMonthly table
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
|
nhaas
Yak Posting Veteran
USA
90 Posts |
Posted - 01/17/2013 : 10:37:45
|
Thanks for the help, I have found in the data that I have spaces in some of my entries. so in one table I have 7965 and the next table I have ' 7965'.
Is there some way that I can remove spaces in front or behind the data?
Thanks |
 |
|
|
sodeep
Flowing Fount of Yak Knowledge
USA
7173 Posts |
Posted - 01/17/2013 : 20:30:54
|
| You can do ltrim(columnname) or you want to remove from both then ltrim(rtrim(columnname). But when you use this function in join, index won't be used. It is good to correct the data. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47960 Posts |
Posted - 01/17/2013 : 23:00:02
|
why not do clean up in table using UPDATE with logic Sodeep suggested and then do the join?
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|