February 13, 2014 at 12:33 am
Hi Friends
I have two tables like
table A
ID Name
1 ragu
2 sudhir
3 siv
table B
DEPT Location
CS chennai
EE mumbai
EC calcutta
i want the result like
ID Name Dept
1 ragu CS
2 sudhir EE
3 siv EC
Two tables have no relationship how can i get this?
February 13, 2014 at 1:07 am
Essentially - you can't.
The tables need to have a common element for you make the association between them - the tables shown don't.
If on the other hand Table A had the location ID from Table B in it, the required results would be possible.
-------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
February 13, 2014 at 1:39 am
Suppose B table have ID but no relationship means how can i write query?
February 13, 2014 at 1:55 am
You need to have the relationship in the data so that you can write a query and return the desired result.
Have a look at the following code and see what I mean
DECLARE @TableA TABLE
(
ID INT ,
Name VARCHAR(10) ,
Department CHAR(2)
)
INSERT INTO @TableA
VALUES ( 1, 'Ragu', 'CS' ),
( 2, ' sudhir', 'EE' ),
( 3, ' siv', 'EC' )
DECLARE @TableB TABLE
(
Dept CHAR(2) ,
Location VARCHAR(50)
)
INSERT INTO @TableB
VALUES ( 'CS', 'chennai' ),
( 'EE', 'mumbai' ),
( 'EC', 'calcutta' )
SELECT ID ,
Name ,
Location
FROM @TableA AS ta
INNER JOIN @TableB AS tb ON ta.Department = tb.Dept
-------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
February 13, 2014 at 1:56 am
In that case you would be able to join but it won't guarantee the output you are looking for ... Instead output will completely depend on how ID values are placed within your table B..
February 13, 2014 at 2:18 am
If the data is stored as in my example, it will return the desired results
-------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
February 13, 2014 at 3:25 am
I think when you say "stored as in my example" you are probably referring to the ORDER of the rows in the example tables, as nothing else in there would provide that meaning to the data.
But, (and it is a very big but) you should never rely on the order of rows unless you explicitly have something in the query that defines that order - (so order by, ranking functions or similar).
Was there any other row to determine how the join should be done?
Mike John
February 13, 2014 at 3:32 am
Agreed - to clarify; I was only looking at the data values returned, not necessarily the row order.
I was attempting to resolve the join issue - not the order which would be rectified by adding
order by ta.id asc
at the end of the query
-------------------------------Posting Data Etiquette - Jeff Moden [/url]Smart way to ask a question
There are naive questions, tedious questions, ill-phrased questions, questions put after inadequate self-criticism. But every question is a cry to understand (the world). There is no such thing as a dumb question. ― Carl Sagan
I would never join a club that would allow me as a member - Groucho Marx
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply