473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Values of two columns in two different tables--presentation using select

Values of two columns in two different tables--presentation using
select

Hi Everyone,

i have two tables in the database . One is called address table
and one is adressPhone Table. Below is the sample of those two tables

Address

col1 col2 col3
X 12 13
y 15 19
z 18 10

create table address(col1 varchar(20),col2 int, col3 int)
insert into address values ('x',12,13)
insert into address values ('y',15,19)
insert into address values ('z',18,10)
AddressPhone

Col4 Col5 Col6

13 213-455-9876 1
13 415-564-6546 2
13 543-987-5677 3
19 678-555-2222 1

create table addressphone(col4 int, col5 varchar(50),col6 int)

insert into addressphone values(13,'213-455-9876',1)
insert into addressphone values(13,'415-564-6546',2)
insert into addressphone values(13,'543-987-5677',3)
insert into addressphone values(19,'678-555-2222',1)

I have to display something like this

x 12 13 213-455-9876 415-564-6546 543-987-5677

y 15 19 678-555-2222 NULL NULL

So there is one to many relationship between address and
addressPhone table where address.col3 = addressphone.col4

I don't know how to write the query to get the phone numbers that
has he same id in the same row like I displyed above.

I did something like this, but this is not working

select col1,col2,col3, (select col5 from addressPhone where col6=1),
(select col5 from addressPhone where col6=2), (select col5 from
addressPhone where col6=3),
from address table inner join addressPhone
on address.col3=addressphone.col4

above is not working because it is complaining that a subquery
cannot return multiple results. Col6 in the addressphone table is
the phone type ike business phone,
mobile phone or home phone. It is possible that there are two phone
numbers for business phone.

Please let me know how can I write this query.

Any help will be greatly appreciated.

Thanks

Mar 14 '06 #1
4 2428
satish wrote:
Values of two columns in two different tables--presentation using
select

Hi Everyone,

i have two tables in the database . One is called address table
and one is adressPhone Table. Below is the sample of those two tables

Address

col1 col2 col3
X 12 13
y 15 19
z 18 10

create table address(col1 varchar(20),col2 int, col3 int)
insert into address values ('x',12,13)
insert into address values ('y',15,19)
insert into address values ('z',18,10)
AddressPhone

Col4 Col5 Col6

13 213-455-9876 1
13 415-564-6546 2
13 543-987-5677 3
19 678-555-2222 1

create table addressphone(col4 int, col5 varchar(50),col6 int)

insert into addressphone values(13,'213-455-9876',1)
insert into addressphone values(13,'415-564-6546',2)
insert into addressphone values(13,'543-987-5677',3)
insert into addressphone values(19,'678-555-2222',1)

I have to display something like this

x 12 13 213-455-9876 415-564-6546 543-987-5677

y 15 19 678-555-2222 NULL NULL

So there is one to many relationship between address and
addressPhone table where address.col3 = addressphone.col4

I don't know how to write the query to get the phone numbers that
has he same id in the same row like I displyed above.

I did something like this, but this is not working

select col1,col2,col3, (select col5 from addressPhone where col6=1),
(select col5 from addressPhone where col6=2), (select col5 from
addressPhone where col6=3),
from address table inner join addressPhone
on address.col3=addressphone.col4

above is not working because it is complaining that a subquery
cannot return multiple results. Col6 in the addressphone table is
the phone type ike business phone,
mobile phone or home phone. It is possible that there are two phone
numbers for business phone.

Please let me know how can I write this query.

Any help will be greatly appreciated.

Thanks


Thanks for including the DDL. It does also help if you include keys
with your CREATE TABLE statements.

Try:

SELECT A.col1, A.col2, A.col3,
P1.col5 AS phone1, P2.col5 AS phone2, P3.col5 AS phone3
FROM address AS A
LEFT JOIN addressphone AS P1
ON A.col3 = P1.col4
AND P1.col6 = 1
LEFT JOIN addressphone AS P2
ON A.col3 = P2.col4
AND P2.col6 = 2
LEFT JOIN addressphone AS P3
ON A.col3 = P3.col4
AND P3.col6 = 3
/* LEFT JOIN addressphone AS P4 ... etc */;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Mar 14 '06 #2
thanking you very much

i works a lot
satish

Mar 14 '06 #3
satish (sa**********************@gmail.com) writes:
I have to display something like this

x 12 13 213-455-9876 415-564-6546 543-987-5677

y 15 19 678-555-2222 NULL NULL

So there is one to many relationship between address and
addressPhone table where address.col3 = addressphone.col4


Here is a query that only joins addressphone once:
select a.col1, a.col2, a.col3,
MIN(CASE ap.col6 WHEN 1 THEN ap.col5 END),
MIN(CASE ap.col6 WHEN 2 THEN ap.col5 END),
MIN(CASE ap.col6 WHEN 3 THEN ap.col5 END)
FROM address a
LEFT JOIN addressphone ap ON a.col3 = ap.col4
GROUP BY a.col1, a.col2, a.col3
go

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: joe soap | last post by:
Hi all, Can this be done. I am wanting to have columns in my forms select element. I have data of varied length from three fields. Can this be done, or should I try and stuff spaces into the...
2
by: jaks | last post by:
How can I export a MySQL table with a different name using phpmyadmin? I want to export nuke_bbsearch_wordmatch and all it contents to phpbb_search_wordmatch This table in a different db. ...
3
by: I_was_here | last post by:
Hey if anyone is a query pro please showoff some knowledge thx. Ie: I have a table with : part price location qty 1 part repeats throughout the table and its price remains the same but it...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
1
by: new214 | last post by:
Hey all, got abit of a problem. Ive got a database where ive got 2 tables. I want to create a command which selects * from table 1 and if the same values are in table2 - it updates otherwise adds. ...
20
by: talktozee | last post by:
Hey, everyone! Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A. For...
8
by: crayfiss | last post by:
Hi, firstly I am a total freshie in all this. From what I have gathered on the web and this forum, I finally managed to get my form up. I have a set of radio buttons with values to it and a select...
3
by: Renilkumar | last post by:
Hi, I am using .net 2.0. I have a .aspx page with vb.net as codebehind. My form has one dropdown, 2 listboxes with add & remove button. During pageload I am loading all the values from the db to...
3
by: shalskedar | last post by:
I want to insert different values in each worksheet i.e every worksheet should 've different values.I tried the below code but i m not able to set the range for a particular worksheet For ex-For...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.