Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,


I have a table table1.

table1 has one column:ID
data are:
ID
1
2
3
4
5
6
7
8
9
10

Another table is present as Table2

has 2 column: ID,Amt

Data are:

ID Amt
1 10
5 20
7 100

Now i need a result as

ID Amt

1 10
2 10
3 10
4 10
5 20
6 20
7 100


PLease tell me query to achive this
Posted

Very strange data and requirement...
With the data shown above, following query works:
SQL
SELECT Table1.ID,  Max(Table2.Amt)
FROM Table1, Table2
where table1.id>=table2.id and table1.id<=7
group by table1.id

[Edit]
The maximum value of table2 need not be hard coded but can be retrieved via a subquery:
SQL
SELECT Table1.ID,  Max(Table2.Amt)
FROM Table1, Table2
where table1.id>=table2.id and table1.id<= (select max(table2.id) from table2)
group by table1.id

[/Edit]
 
Share this answer
 
v2
Comments
Maciej Los 20-Feb-14 15:31pm    
It looks perfect!
+5!
King Fisher 20-Feb-14 23:33pm    
Good one
SQL
select a.id,case when a.id between 1 and 4 then 10 when a.id between  5 and 6 then 20 when a.id between 7 and 10 then 100 end  From table1 as a left join table2 as b on a.id=b.id
 
Share this answer
 
v3
Comments
praveen.victor 20-Feb-14 8:20am    
In this Query values are hard coded. need a dynamic query
Join Query :
SQL
SELECT t2.ID, t2.Amt
FROM Table1 t1, Tabele2 t2
WHERE t1.ID = t2.ID


-KR
 
Share this answer
 
Comments
praveen.victor 20-Feb-14 8:21am    
its not satisfying the requirment
Krunal Rohit 20-Feb-14 8:25am    
I have seen such cases where "no repeatation" of data is needed. The result you need is simply depends upon the data but query.
Any way, can you tell me why are you doing this ?
-KR

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