| Author |
Topic |
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:02:03
|
| please i need a help with the following:a table Firms with fields ID and Name, contains:ID Name1 Sony2 Panasonic3 SamsungA table Phones with fields firmID and Phone contains:FirmID Phone1 883471 124542 89092what is the SQL query that displays the firm name with it's phone number, and if a firm has 2 phone numbers it should display only one, and if a firm has no number it should display it with the phone field emply or stroke out?The query result should be like this:Name PhoneSony 88347Panasonic 89092Samsung-What is the query to return only the firms who have no phone number?-What is the query to return the firms who have not less than 2 phone numbers-What is the query to return the firms who have less than 2 phone numbersPlease i appreciate your quick helpRegardsElias |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-16 : 07:08:37
|
| Homework? |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:13:10
|
No it's not a homework, it's kind of a personal test, i am trying to learn sqlcan you help me please?quote: Originally posted by Kristen Homework?
|
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2006-05-16 : 07:16:58
|
| What have you come up with so far? You won't learn anything if people just tell you what to do.-------Moo. :) |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-16 : 07:17:13
|
You're testing yourself by getting someone else to give you the answer? Show us what you've tried...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-05-16 : 07:21:30
|
| oks just a hints look out for left outer joins and inner joins in Book online i.e. SQL help fileIf Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:23:24
|
ok, i've tried this:SELECT DISTINCT name, phoneFROM firms LEFT JOIN phones ON Firms.ID=Phones.[Firm ID];i learned that the left join can return even the empty fields, i learned that Distinct eliminates duplicates, but i noticed that it only eliminate duplicates if the phone number is duplicated. i dont know exactly how can i let it work for the field "name"can we talk online?quote: Originally posted by RyanRandall You're testing yourself by getting someone else to give you the answer? Show us what you've tried...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part.
|
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:27:32
|
thank you, i tried the left join, and i got the difference between the inner and outer join, but the problem is how to eliminate the duplicates, i used distinct but it worked only if the phone number is the same, not if the company has 2 different phone numbersquote: Originally posted by chiragkhabaria oks just a hints look out for left outer joins and inner joins in Book online i.e. SQL help fileIf Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
|
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-16 : 07:33:22
|
| You need to use some aggregate function to pick one of the 'duplicate' phone numbers (note that they're not really duplicates). Try using max or min...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-16 : 07:34:34
|
Oh, and why is this URGENT if it's just a personal test?  Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:39:58
|
i meant by personal, that it's a challenge from a friend.thank you for your help.i think i came to know the other queries, but still to eliminate the "sony" duplicate, i can't get it.1)The query to return only the firms who have no phone numberSELECT * FROM firms,(select phones.firm_id,count(phones.firm_id) as cnt ,max(phones.Phone) max_numberfrom phonesgroup by phones.firm_id) phones_cnt_tblWHEREfirms.id = phones_cnt_tbl.firm_id (+)and cnt is null2)The query to return the firms who have not less than 2 phone numbersSELECT * FROM firms,(select phones.firm_id,count(phones.firm_id) as cnt,max(phones.Phone) max_numberfrom phonesgroup by phones.firm_id) phones_cnt_tblWHEREfirms.id = phones_cnt_tbl.firm_id (+) and cnt >= 23)The query to return the firms who have less than 2 phone numbersSELECT * FROM firms,(select phones.firm_id,count(phones.firm_id) as cnt,max(phones.Phone) max_numberfrom phonesgroup by phones.firm_id) phones_cnt_tblWHEREfirms.id = phones_cnt_tbl.firm_id (+)and cnt < 2so can you help me with the first one please?quote: Originally posted by RyanRandall Oh, and why is this URGENT if it's just a personal test?  Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part.
|
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-05-16 : 07:40:16
|
somthing like this .. Declare @TblFirm Table (ID Int,Name Varchar(20))Insert @TblFirm Select 1, 'Sony' Union All Select 2,'Panasonic' Union All Select 3, 'Samsung'Declare @TblPhone Table (FirmID int, phone varchar(10))Insert @TblPhone Select 1, '88347' Union All Select 1, '12454' Union All Select 2, '89092'Select A.Name,IsNull(b.Phone,'') From @TblFirm a Left Outer Join (Select FirmID,Max(phone) phone From @Tblphone b Group by FirmID ) as bon a.id = b.firmid--Group by b.phone --Or Select A.Name,Max(IsNull(b.Phone,'')) From @TblFirm a Left Outer Join @Tblphone bon a.id = b.firmidGroup by A.Name If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2006-05-16 : 07:50:21
|
| Declare @TblFirm Table (ID Int,Name Varchar(20))Insert @TblFirm Select 1, 'Sony' Union All Select 2,'Panasonic' Union All Select 3, 'Samsung'Declare @TblPhone Table (FirmID int, phone varchar(10))Insert @TblPhone Select 1, '88347' Union All Select 1, '12454' Union All Select 2, '89092'SELECT DISTINCT [Name] FROM @TblFirm tfLEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmIDWHERE tp.FirmID is nullSELECT [NAME] FROM @TblFirm tfLEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmIDGROUP BY [NAME]HAVING Count(*) >1SELECT [NAME] FROM @TblFirm tfLEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmIDGROUP BY [NAME]HAVING Count(*) <2-------Moo. :) |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:50:46
|
so any suggestion for the query to just displayName PhoneSony 88347Panasonic 89092Samsungplease?quote: Originally posted by mr_mist What have you come up with so far? You won't learn anything if people just tell you what to do.-------Moo. :)
|
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-16 : 07:51:20
|
Okay - good to see you've made an effort there. Well done! Here are my (various) 'answers' (using in, exists or left outer joins). Good luck with your friend  --datadeclare @Firms table (ID int, Name varchar(10))insert @Firms select 1, 'Sony'union all select 2, 'Panasonic'union all select 3, 'Samsung'declare @Phones table (FirmID int, Phone varchar(10))insert @Phones select 1, '88347'union all select 1, '12454'union all select 2, '89092'--calculation--what is the SQL query that displays the firm name with it's phone number, --and if a firm has 2 phone numbers it should display only one, --and if a firm has no number it should display it with the phonefield emply or stroke out? select f.Name, isnull(max(p.Phone), '') as Phone from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.Nameselect f.Name, isnull(min(p.Phone), '') as Phone from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.Name--What is the query to return only the firms who have no phone number?select * from @Firms where ID not in (select FirmID from @Phones)select f.* from @Firms f where not exists (select * from @Phones p where f.ID = p.FirmID)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID where FirmID is null--What is the query to return the firms who have not less than 2 phone numbersselect * from @Firms where ID in (select FirmID from @Phones group by FirmID having count(*) >= 2)select f.* from @Firms f where exists (select * from @Phones p where f.ID = p.FirmID group by FirmID having count(*) >= 2)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.ID, f.Name having count(*) >= 2--What is the query to return the firms who have less than 2 phone numbersselect * from @Firms where ID not in (select FirmID from @Phones group by FirmID having count(*) >= 2)select f.* from @Firms f where not exists (select * from @Phones p where f.ID = p.FirmID group by FirmID having count(*) >= 2)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.ID, f.Name having count(*) < 2 Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 07:58:51
|
Thank you ALL, you have been great helpI appreciate itquote: Originally posted by RyanRandall Okay - good to see you've made an effort there. Well done! Here are my (various) 'answers' (using in, exists or left outer joins). Good luck with your friend  --datadeclare @Firms table (ID int, Name varchar(10))insert @Firms select 1, 'Sony'union all select 2, 'Panasonic'union all select 3, 'Samsung'declare @Phones table (FirmID int, Phone varchar(10))insert @Phones select 1, '88347'union all select 1, '12454'union all select 2, '89092'--calculation--what is the SQL query that displays the firm name with it's phone number, --and if a firm has 2 phone numbers it should display only one, --and if a firm has no number it should display it with the phonefield emply or stroke out? select f.Name, isnull(max(p.Phone), '') as Phone from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.Nameselect f.Name, isnull(min(p.Phone), '') as Phone from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.Name--What is the query to return only the firms who have no phone number?select * from @Firms where ID not in (select FirmID from @Phones)select f.* from @Firms f where not exists (select * from @Phones p where f.ID = p.FirmID)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID where FirmID is null--What is the query to return the firms who have not less than 2 phone numbersselect * from @Firms where ID in (select FirmID from @Phones group by FirmID having count(*) >= 2)select f.* from @Firms f where exists (select * from @Phones p where f.ID = p.FirmID group by FirmID having count(*) >= 2)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.ID, f.Name having count(*) >= 2--What is the query to return the firms who have less than 2 phone numbersselect * from @Firms where ID not in (select FirmID from @Phones group by FirmID having count(*) >= 2)select f.* from @Firms f where not exists (select * from @Phones p where f.ID = p.FirmID group by FirmID having count(*) >= 2)select f.* from @Firms f left outer join @Phones p on f.ID = p.FirmID group by f.ID, f.Name having count(*) < 2 Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part.
|
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-16 : 08:10:12
|
| "and if a firm has 2 phone numbers it should display only one"Bit tricky this as the question doesn't say which, of the multiple entries, should be shown. e.g. Minimum number, random choice, etc.Tell your friend that its a lousy question and just the sort of useless specification that my clients give me - no chance of guessing what they actually want!Kristen |
 |
|
|
eliastannoury
Starting Member
11 Posts |
Posted - 2006-05-16 : 08:18:21
|
Random choicequote: Originally posted by Kristen "and if a firm has 2 phone numbers it should display only one"Bit tricky this as the question doesn't say which, of the multiple entries, should be shown. e.g. Minimum number, random choice, etc.Tell your friend that its a lousy question and just the sort of useless specification that my clients give me - no chance of guessing what they actually want!Kristen
|
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-16 : 08:33:32
|
| "Random choice"Hehehe - RyanRandall you'll have the change the MAX() and MIN() solutions to a crafty TOP 1 method instead - with ORDER BY NewID() for good measure!Actually, eliastannoury, you might like to tell your friend that Random Choice is not a good one. Users get very confused when they re-run the query and get a different result ... either of RyanRandall's MIN() / MAX() methods is probably preferable.Kristen |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-16 : 08:42:59
|
quote: Originally posted by Kristen "Random choice"Hehehe - RyanRandall you'll have the change the MAX() and MIN() solutions to a crafty TOP 1 method instead - with ORDER BY NewID() for good measure!
I don't have to do anything. He's not my friend Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-05-16 : 09:06:19
|
| Specification has changed - customer's prerogative! |
 |
|
|
Next Page
|