Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi..
I have a problem. please help me in solving it.

I HAVE TWO TABLE 1ST STRUCTURE IS
ID HEAD1 HEAD2 HEAD3
10 3400 500 600
11 6400 800 600
12 8400 500 600
13 1400 900 600

-----------------------------

2ND TABLE IS

HEADNAME DESCRIPTION
HEAD1 AAAAA
HEAD2 BBBBB
HEAD3 CCCCC
HEAD4 DDDDD
-------------------------------------------------

I HAVE TO GET VALUE WITH THEIR DESCRIPTION. AS :---
ID DESCRIPTION AMT
10 AAAAA 3400
10 BBBBB 500
10 CCCCC 600
11 AAAAA 6400
11 BBBBB 800
11 CCCCC 600
12 AAAAA 6400
12 BBBBB 800
12 CCCCC 600
13 AAAAAA 1400
13 BBBBBB 900
13 CCCCCC 600
Posted
Updated 1-Oct-12 0:55am
v2
Comments
[no name] 1-Oct-12 6:54am    
have u tried once..googled or not..???
Abhijit Parab 1-Oct-12 7:06am    
This is not possible with simple join. you have to write some logic for that
ridoy 1-Oct-12 7:30am    
i think it is not possible until you create a new field for ID in your 2nd table and then join those 2 tables on its ID

For joining at least one column should be common in both table but in your table there is no common column and tell me how can u get amount because AMT column does not exist in any table .
 
Share this answer
 
you have not designed database very well
but as per input output example,
you can use below query,
SQL
select * from
(
select Id,(select Description from table2 where headname='head1') as Description,head1 as amt
from table1
union all
select Id,(select Description from table2 where headname='head2') as Description,head2 as amt
from table1
union all
select Id,(select Description from table2 where headname='head3') as Description,head3 as amt
from table1
) as a
order by id,Description

Happy coding!
:)
 
Share this answer
 
Comments
Member 8083455 1-Oct-12 8:30am    
THANKS AARTI
BUT IF WE INCREASE NO OF HEAD AS HEAD4, HEAD5, .... THEN WE CANT GET IT.
I WANT TO GET IT DYNAMICALLY.
AND PLEASE COULD YOU HINT ME TO MODIFY DATABASE STRUCTURE...
Aarti Meswania 1-Oct-12 8:33am    
but you have make it static
see structure of table-1
there are static columns Head1,head2... you have to add columns while adding new head same way you have to change query

Aarti Meswania 1-Oct-12 8:51am    
table1's structure should be like below
declare columns as below
id
head
Amt

id head amt
10 head1 3400
10 head2 500
10 head3 600
11 head1 6400
...

then query will be easy
select Id,Description,amt
from table1
left join table2 on headname=head
Member 8083455 4-Oct-12 6:00am    
CAN WE GET RIGHT STRUCTURE BY USING DYNAMIC PIVOT.
IF YES PLEASE TELL ME QUERY.
Aarti Meswania 4-Oct-12 6:11am    
no your question is not related to pivot view, you are in wrong way,
pivot is what it is something like display data columnwise instead of row wise
e.g in calender i want name of days(sun,mon..) horizontally not vertically in that case i will use pivot query to arrange my data from sql.

if i say in 2nd table enter new row having name head9, then in first table you will going to insert a column having name head9!!!
it is not a practical way to do this.

now guess i have entered head1...head999 now, what?
you will going to create 999 columns in first table?! :)
that's why I have suggested to change database structure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900