| Author |
Topic |
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-11-03 : 05:23:27
|
Hello AllIs there any option in SELECT query to select a particular row of interest ?for ex: In my application the result set i get is something likeStep Data-------------1 ITEM111 ITEM121 ITEM132 ITEM212 ITEM222 ITEM23 If i have to select ITEM21 based on the index [2].How can i use the SELECT statement in such a case to retrieve the correct data |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 05:30:47
|
| where data like 'ITEM2%'MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-03 : 05:44:36
|
[code]select top 1 Datafrom yourtablewhere Step = 2order by Data[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-11-03 : 06:16:10
|
Hello I cannot use this query.TOP(1) always returns the first item i.e ITEM21what if i needed ITEM22 or ITEM23 ??quote: Originally posted by khtan
select top 1 Datafrom yourtablewhere Step = 2order by Data KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-11-03 : 06:23:59
|
Hello MadhivananActually i think i didnt explain the problem properly.The result i get is during the runtime, and i have no idea what is returned, hence i cannot use such a filter 'ITEM2%'I have explained my scenario here pictorially.I have an index say 2, depending on this index whenever i SELECT from this table with filter set on Column : Stepi should be able to get the result ITEM22 Step Data ------------- 1 ITEM11 1 ITEM12 1 ITEM13 2 ITEM21 --> 2 ITEM22 2 ITEM23 quote: Originally posted by madhivanan where data like 'ITEM2%'MadhivananFailing to plan is Planning to fail
|
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-03 : 06:38:57
|
what do you mean by index ? and what is index = 2 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-03 : 06:39:35
|
quote: Originally posted by srivatsahg Hello I cannot use this query.TOP(1) always returns the first item i.e ITEM21what if i needed ITEM22 or ITEM23 ??quote: Originally posted by khtan
select top 1 Datafrom yourtablewhere Step = 2order by Data KH[spoiler]Time is always against us[/spoiler]
but you never says you need it in your 1st post KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-11-03 : 07:07:53
|
HelloSorry for not explaining my problem correctlyResult i need, if index = 2 and Step = 2 Step Data ------------- 1 ITEM11 1 ITEM12 1 ITEM13 2 ITEM21 --> 2 ITEM22 2 ITEM23 Result i need, if index = 3 and Step = 1 Step Data ------------- 1 ITEM11 1 ITEM12 -->1 ITEM13 2 ITEM21 2 ITEM22 2 ITEM23 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-03 : 07:13:08
|
| where step=@step and data like 'ITEM'+@step+'%'MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-03 : 07:25:40
|
[code]select *from( select *, idx = row_number() over (partition by Step order by Data) from yourtable) awhere Step = 1and idx = 3[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
srivatsahg
Yak Posting Veteran
71 Posts |
Posted - 2009-11-03 : 08:19:29
|
Thanks a ton ..!!I needed a similar query I modified your query for my requirement.quote: Originally posted by khtan
select *from( select *, idx = row_number() over (partition by Step order by Data) from yourtable) awhere Step = 1and idx = 3 KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
|