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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Join issue

Author  Topic 

nhaas
Yak Posting Veteran

90 Posts

Posted - 2013-01-16 : 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
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 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
Go to Top of Page

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, 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/

Go to Top of Page

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 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/

Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -