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 |
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2013-01-16 : 17:57:28
|
I have a simple Query:select voipnumber, typefrom VOIPNumbersMonthlyORDER BY Type, ORGit returns 8700 rows, which are correct. Now I want to add a cost per type. I add the join:select voipnumber, type,VoipPhones.CC710from VOIPNumbersMonthlyinner join VoipPhonesON VOIPNumbersMonthly.type = VoipPhones.PModel ORDER BY Type, ORGbut I only get 2428 rows.What could be the cause of the issue?Thank you |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2013-01-16 : 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 arecord in VoipPhones based on type = Pmodel. Try it with a LEFT JOIN and see what you get.JimEveryday I learn something that somebody else already knew |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-16 : 23:58:06
|
quote: Originally posted by nhaas I have a simple Query:select voipnumber, typefrom VOIPNumbersMonthlyORDER BY Type, ORGit returns 8700 rows, which are correct. Now I want to add a cost per type. I add the join:select voipnumber, type,VoipPhones.CC710from VOIPNumbersMonthlyinner join VoipPhonesON VOIPNumbersMonthly.type = VoipPhones.PModel ORDER BY Type, ORGbut 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 insteadieselect voipnumber, type,VoipPhones.CC710from VOIPNumbersMonthlyinnerleft join VoipPhonesON VOIPNumbersMonthly.type = VoipPhones.PModel ORDER BY Type, ORG ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-16 : 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 arecord in VoipPhones based on type = Pmodel. Try it with a LEFT JOIN and see what you get.JimEveryday I learn something that somebody else already knew
sorry i didnt get that explanationI feel its correct so far as OP has field ORG in VOIPNumbersMonthly table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nhaas
Yak Posting Veteran
90 Posts |
Posted - 2013-01-17 : 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
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2013-01-17 : 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
52326 Posts |
Posted - 2013-01-17 : 23:00:02
|
why not do clean up in table using UPDATE with logic Sodeep suggested and then do the join?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|