Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql script help

Author  Topic 

privatecoder
Starting Member

3 Posts

Posted - 2010-07-31 : 14:37:08
[code]
1. table kayitlar 2. Table Kayitlar Result Table

id userno odemetipi id userno id userno odemetipi
1 1 K 1 1 1 1 K
1 2 K 1 2 1 2 K
1 5 K 1 3 1 3 K
1 6 O 1 4 1 4 K
1 9 T 1 5 1 5 K
2 1 K 1 6 1 6 O
2 3 O 1 7 1 7 O
1 8 1 8 O
1 9 1 9 T
1 10 1 10 T
2 1 2 1 K
2 2 2 2 K
2 3 2 3 O
2 4 2 4 O
2 5 2 5 O
[/code]

i have 2 table.(1. and 2.) i want to write a sql script to occur 3. table without using function or cursor. Can anyone help me?

edit: moved to proper forum

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-07-31 : 18:12:04
what is the logic here ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

privatecoder
Starting Member

3 Posts

Posted - 2010-08-01 : 08:52:57
quote:
Originally posted by khtan

what is the logic here ?


KH
[spoiler]Time is always against us[/spoiler]





In the 1. table there is no userno(3,4, 7,8..) But 2. table has all userno. And Result table has all userno and 'odemetipi' column. In the Result table id = 1 and userno = 3 and 'odemetipi' = 'K'. Why K? Because the maximun userno smaller then 3 is 2. so user no 2 'odemeipi'= K. So userno = 3 and 'odemetipi'=K
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-01 : 21:20:07
[code]
select *
from tbl2 t2
cross apply
(
select top 1 odemetipi
from tbl1 t1
where t1.id = t2.id
and t1.userno <= t2.userno
order by t1.userno desc
) t1
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

privatecoder
Starting Member

3 Posts

Posted - 2010-08-03 : 12:44:34
quote:
Originally posted by khtan


select *
from tbl2 t2
cross apply
(
select top 1 odemetipi
from tbl1 t1
where t1.id = t2.id
and t1.userno <= t2.userno
order by t1.userno desc
) t1



KH
[spoiler]Time is always against us[/spoiler]






Yes man you are right! Thank you very much... You are wonderful!!
Go to Top of Page
   

- Advertisement -