SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Join issue
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nhaas
Yak Posting Veteran

USA
90 Posts

Posted - 01/16/2013 :  17:57:28  Show Profile  Visit nhaas's Homepage  Reply with Quote
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
2869 Posts

Posted - 01/16/2013 :  18:45:29  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/16/2013 :  23:58:06  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/16/2013 :  23:59:26  Show Profile  Reply with Quote
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

USA
90 Posts

Posted - 01/17/2013 :  10:37:45  Show Profile  Visit nhaas's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 01/17/2013 :  20:30:54  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/17/2013 :  23:00:02  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000