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
 SQL Server Development (2000)
 URGENT HELP IN QUERIES

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 Name
1 Sony
2 Panasonic
3 Samsung

A table Phones with fields firmID and Phone contains:

FirmID Phone
1 88347
1 12454
2 89092

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 phone field emply or stroke out?

The query result should be like this:

Name Phone
Sony 88347
Panasonic 89092
Samsung

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

Please i appreciate your quick help

Regards
Elias

Kristen
Test

22859 Posts

Posted - 2006-05-16 : 07:08:37
Homework?
Go to Top of Page

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 sql
can you help me please?

quote:
Originally posted by Kristen

Homework?

Go to Top of Page

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

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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

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 file


If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

eliastannoury
Starting Member

11 Posts

Posted - 2006-05-16 : 07:23:24
ok, i've tried this:

SELECT DISTINCT name, phone
FROM 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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.

Go to Top of Page

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 numbers

quote:
Originally posted by chiragkhabaria

oks just a hints
look out for left outer joins and inner joins in Book online i.e. SQL help file


If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.

Go to Top of Page

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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

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 number

SELECT * FROM firms,
(select phones.firm_id,count(phones.firm_id) as cnt ,max(phones.Phone) max_number
from phones
group by phones.firm_id) phones_cnt_tbl
WHERE
firms.id = phones_cnt_tbl.firm_id (+)
and cnt is null

2)The query to return the firms who have not less than 2 phone numbers

SELECT * FROM firms,(select phones.firm_id,count(phones.firm_id) as cnt,max(phones.Phone) max_number
from phones
group by phones.firm_id) phones_cnt_tbl
WHERE
firms.id = phones_cnt_tbl.firm_id (+) and cnt >= 2

3)The query to return the firms who have less than 2 phone numbers

SELECT * FROM firms,(select phones.firm_id,count(phones.firm_id) as cnt,max(phones.Phone) max_number
from phones
group by phones.firm_id) phones_cnt_tbl
WHERE
firms.id = phones_cnt_tbl.firm_id (+)and cnt < 2

so 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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.

Go to Top of Page

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 b
on a.id = b.firmid
--Group by b.phone

--Or

Select A.Name,Max(IsNull(b.Phone,'')) From
@TblFirm a Left Outer Join @Tblphone b
on a.id = b.firmid
Group by A.Name




If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

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 tf
LEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmID
WHERE tp.FirmID is null

SELECT [NAME] FROM @TblFirm tf
LEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmID
GROUP BY [NAME]
HAVING Count(*) >1

SELECT [NAME] FROM @TblFirm tf
LEFT OUTER JOIN @TblPhone tp ON tf.ID = tp.FirmID
GROUP BY [NAME]
HAVING Count(*) <2

-------
Moo. :)
Go to Top of Page

eliastannoury
Starting Member

11 Posts

Posted - 2006-05-16 : 07:50:46
so any suggestion for the query to just display

Name Phone
Sony 88347
Panasonic 89092
Samsung


please?

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. :)

Go to Top of Page

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

--data
declare @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.Name
select 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 numbers
select * 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 numbers
select * 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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

eliastannoury
Starting Member

11 Posts

Posted - 2006-05-16 : 07:58:51
Thank you ALL, you have been great help
I appreciate it




quote:
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

--data
declare @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.Name
select 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 numbers
select * 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 numbers
select * 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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.

Go to Top of Page

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

eliastannoury
Starting Member

11 Posts

Posted - 2006-05-16 : 08:18:21
Random choice

quote:
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

Go to Top of Page

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

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 Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-05-16 : 09:06:19
Specification has changed - customer's prerogative!
Go to Top of Page
    Next Page

- Advertisement -