Author |
Topic |
pkindigo
Starting Member
2 Posts |
Posted - 2010-08-12 : 02:17:23
|
hi, I have two tables likecreate table a(id int, expense int)insert into aselect 1,2000union all select 1,2000union all select 2,3000union all select 2,3000create table b(id int, amount int)insert into bselect 1,5000union all select 2,10000i write the query ---select a.id , a.expense ,b.id , b.amount from ainner join b on b.id = a.id it return the result -----1 2000 1 50001 2000 1 50002 3000 2 100002 3000 2 10000but i want -------------1 2000 1 50001 2000 1 02 3000 2 100002 3000 2 0please help meThanks |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-08-12 : 02:34:13
|
That makes no sense - sorry. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
urzsuresh
Starting Member
30 Posts |
Posted - 2010-08-12 : 07:07:18
|
Hello friend, Kindly try this below sample one. You will get some idea. Let me know that, do you need any further clarifficationdeclare @a table (id int, expense int)insert into @aselect 1,2000union allselect 1,2000union allselect 2,3000union allselect 2,3000declare @b table (id int, amount int)insert into @bselect 1,5000union allselect 2,10000;with cteas(selecta.id , a.expense,b.id ids , b.amount,row_number() over( partition by b.amount order by newid())as Resultfrom @a ainner join @b b on b.id = a.id),cte1 as( Select a.id,a.expense,a.ids,0 amount,a.result from cte a Join cte b on a.id=b.id and b.result=2 and a.result=2 union all Select id,expense,ids,amount,result from cte where result=1)Select id,expense,ids,amount from cte1 order by ids,resultquote: Originally posted by pkindigo hi, I have two tables likecreate table a(id int, expense int)insert into aselect 1,2000union all select 1,2000union all select 2,3000union all select 2,3000create table b(id int, amount int)insert into bselect 1,5000union all select 2,10000i write the query ---select a.id , a.expense ,b.id , b.amount from ainner join b on b.id = a.id it return the result -----1 2000 1 50001 2000 1 50002 3000 2 100002 3000 2 10000but i want -------------1 2000 1 50001 2000 1 02 3000 2 100002 3000 2 0please help meThanks
Suri |
 |
|
urzsuresh
Starting Member
30 Posts |
Posted - 2010-08-12 : 07:14:48
|
Hello Friend, This another methoddeclare @a table (id int, expense int)insert into @aselect 1,2000union allselect 1,2000union allselect 2,3000union allselect 2,3000declare @b table (id int, amount int)insert into @bselect 1,5000union allselect 2,10000;with cteas(selectdistinct a.id , a.expense,b.id ids , b.amount,row_number() over( partition by b.amount order by newid())as Resultfrom @a ainner join @b b on b.id = a.id),cte1 as( Select distinct a.id,a.expense,a.ids,Case a.Result when 2 then 0 else a.amount end amount, a.result from cte a )Select id,expense,ids,amount from cte1 order by ids,result |
 |
|
|
|
|