Author |
Topic |
s_k_sameer
Starting Member
9 Posts |
Posted - 2008-05-14 : 04:39:10
|
I have a ACTION column. i.e Its only disply SELL/BUY.ACTIONBUYBUYBUYSELLSELLSELLSELLBUYBUYSELLSELLSELLBUYHow to create another column display like thisACTIONBUY 1BUY 2BUY 3SELL 1SELL 2SELL 3SELL 4BUY 1BUY 2SELL 1SELL 2SELL 3BUY 1Help me anyone... |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-14 : 04:46:49
|
DO you have any other columns in your table? Its probably better if you can give some data from the other columns too. |
 |
|
s_k_sameer
Starting Member
9 Posts |
Posted - 2008-05-14 : 04:55:48
|
I have TIME Column also..Assume my table like this..TIME ACTION12:40:01 BUY 12:40:31 BUY 12:40:51 BUY 12:41:01 SELL 12:41:21 SELL12:41:31 SELL 12:41:41 SELL 12:41:51 BUY 12:42:01 BUY 12:42:29 SELL 12:42:31 SELL 12:42:41 SELL 12:42:51 BUY |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-14 : 05:03:38
|
Anything else?How come the three last Actions are earlier than the previous action? E 12°55'05.25"N 56°04'39.16" |
 |
|
s_k_sameer
Starting Member
9 Posts |
Posted - 2008-05-14 : 05:09:33
|
quote: Originally posted by Peso Anything else?How come the three last Actions are earlier than the previous action? E 12°55'05.25"N 56°04'39.16"
SOrry...I changed now.. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-14 : 05:15:56
|
quote: Originally posted by s_k_sameer I have TIME Column also..Assume my table like this..TIME ACTION12:40:01 BUY 12:40:31 BUY 12:40:51 BUY 12:41:01 SELL 12:41:21 SELL12:41:31 SELL 12:41:41 SELL 12:41:51 BUY 12:42:01 BUY 12:42:29 SELL 12:42:31 SELL 12:42:41 SELL 12:42:51 BUY
SELECT ROW_NUMBER() OVER (PARTITION BY LEFT(TIME,5),ACTION ORDER BY TIME) AS Seg,TIME,ACTIONFROM YOURTABLE |
 |
|
s_k_sameer
Starting Member
9 Posts |
Posted - 2008-05-14 : 06:39:42
|
Hi visakh,Thnks for ur response. Its not giving proper output.It wrong in my requirement. |
 |
|
eralper
Yak Posting Veteran
66 Posts |
Posted - 2008-05-14 : 06:43:27
|
Hey,That is same topic with the one at MSDN forums :) [url]http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3339807&SiteID=1[/url]I had also posted the following solution to that post :Add an Id identity column to the Actions table then run the following code blackwith cte (id, current_code, prev_code, rn) as(select id, current_code, prev_code, rn--, 0from (select a.id, a.code current_code, b.code prev_code,case when a.code <> isnull(b.code,'') then 1else nullend as rnfrom EY_Action a left join EY_Action b on (a.id ) = (b.id + 1)) awhere id = 1union allselect a.id, a.current_code, a.prev_code, isnull(a.rn, cte.rn + 1)--, cte.rnfrom (select a.id, a.code current_code, b.code prev_code,case when a.code <> isnull(b.code,'') then 1else nullend as rnfrom EY_Action a inner join EY_Action b on (a.id ) = (b.id + 1)) a inner join cte on (a.id) = (cte.id + 1))select c.id, c.current_code, c.rn from cte cEralperhttp://www.kodyaz.com/content/SQLServerArticles.aspx-------------Eralperhttp://www.kodyaz.com |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-05-14 : 06:50:14
|
Which certainly is so much more efficient than Visakh16's suggestion? E 12°55'05.25"N 56°04'39.16" |
 |
|
s_k_sameer
Starting Member
9 Posts |
Posted - 2008-05-14 : 07:34:23
|
hi eralper,what is current_code, prev_code |
 |
|
|