| Author |
Topic |
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-09 : 07:30:14
|
| Hi, I am having a table Products. Now I have to write a query to get the above and below rows along with row for which prod-id given.The Prod table has Prod-id,Prodname, category |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-08-09 : 08:04:06
|
quote: Originally posted by vmurali Hi, I am having a table Products. Now I have to write a query to get the above and below rows along with row for which prod-id given.The Prod table has Prod-id,Prodname, category
What do u mean by above & below? how much above and below Post sample data, expected output.Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-09 : 08:06:40
|
quote: how much above and below 
Playing with 2 meaning words..  Chirag |
 |
|
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-09 : 08:21:04
|
The row prior and row next for a given rowquote: Originally posted by harsh_athalye
quote: Originally posted by vmurali Hi, I am having a table Products. Now I have to write a query to get the above and below rows along with row for which prod-id given.The Prod table has Prod-id,Prodname, category
What do u mean by above & below? how much above and below Post sample data, expected output.Harsh AthalyeIndia."Nothing is Impossible"
|
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-08-09 : 08:44:54
|
| vmurali,>> The row prior and row next for a given rowFor u it may be some rows and u may c those rows. but v cannot c the rows.The SQL server doesn't have a (or precisely speaking, it cannot be guaranteed of) particular order by which u get the record set.The Prior or Next is different in different contexts. If it not is ordered (sorted) , then u will get the records in one way. If sorted by a column, a different way, If by another column some other way.Anyway, this is possible in Front End.Also tell what is ur requirement.Is it a sorted records set ? Is there a query to get ur records ?Srinika |
 |
|
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-09 : 08:53:32
|
Ok Keeping that the rows are in sorted order. How to get the rows prior and next to a particular row.quote: Originally posted by Srinika vmurali,>> The row prior and row next for a given rowFor u it may be some rows and u may c those rows. but v cannot c the rows.The SQL server doesn't have a (or precisely speaking, it cannot be guaranteed of) particular order by which u get the record set.The Prior or Next is different in different contexts. If it not is ordered (sorted) , then u will get the records in one way. If sorted by a column, a different way, If by another column some other way.Anyway, this is possible in Front End.Also tell what is ur requirement.Is it a sorted records set ? Is there a query to get ur records ?Srinika
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-09 : 10:01:35
|
| Why do you want to get the output like this?MadhivananFailing to plan is Planning to fail |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-08-09 : 10:21:14
|
| What is ur front end ? r u using ADO ? if so use MoveNext, MovePrevious.Srinika |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-09 : 11:13:23
|
@prod_id is the current value of @prod_id-- previous row assuming ascending order of prod_idselect top 1 * from prod where prod_id < @prod_id order by prod_idor-- next row assuming ascending order of prod_idselect top 1 * from prod where prod_id > @prod_id order by prod_id KH |
 |
|
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-10 : 02:04:38
|
if i want to get the both statements together can i use unionquote: Originally posted by khtan @prod_id is the current value of @prod_id-- previous row assuming ascending order of prod_idselect top 1 * from prod where prod_id < @prod_id order by prod_idor-- next row assuming ascending order of prod_idselect top 1 * from prod where prod_id > @prod_id order by prod_id KH
|
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-10 : 02:08:23
|
Yeah somthing like this .. select top 1 * from prod where prod_id < @prod_id order by prod_idUnion All select * from prod where prod_id = @prod_id order by prod_idUnion All select top 1 * from prod where prod_id > @prod_id order by prod_id Chirag |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-10 : 03:16:26
|
You can't use ORDER BY with UNION directly.select * from (select top 1 * from prod where prod_id < @prod_id order by prod_id) aUnion All select * from (select * from prod where prod_id = @prod_id order by prod_id) bUnion All select * from (select top 1 * from prod where prod_id > @prod_id order by prod_id) c KH |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-10 : 03:25:41
|
quote: You can't use ORDER BY with UNION directly.
Aww.. Thanks for the information Chirag |
 |
|
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-10 : 03:32:54
|
| but if i want to get the 2,3,4 record then this query doesn't seem to work as its returns the first record but i want it to return me only 2nd record |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-08-10 : 03:39:22
|
quote: Originally posted by vmurali but if i want to get the 2,3,4 record then this query doesn't seem to work as its returns the first record but i want it to return me only 2nd record
didnt understood you requirement Post same sample dataChirag |
 |
|
|
vmurali
Yak Posting Veteran
88 Posts |
Posted - 2006-08-10 : 04:43:55
|
| This seems to be workingselect top 1 * from product1 where [Product-id]=(select max([Product-Id]) from (select * from Product1 where [Product-Id]<1043) as a) unionselect * from product1 where [Product-id]=1043 unionselect top 1 * from product1 where [Product-id]=(select min([Product-Id]) from (select * from Product1 where [Product-Id]>1043) as b) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-10 : 12:45:42
|
| Are you using fornt end application? If so, make use of recordset's movenext and movepreviousMadhivananFailing to plan is Planning to fail |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-08-10 : 14:00:30
|
quote: Originally posted by madhivanan Are you using fornt end application? If so, make use of recordset's movenext and movepreviousMadhivananFailing to plan is Planning to fail
 quote: Originally posted by Srinika What is ur front end ? r u using ADO ? if so use MoveNext, MovePrevious.Srinika
Srinika |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2006-08-11 : 00:14:22
|
| Personally I would not be using 'TOP 1' type of queries because it implies you don't know your data well enough to exactly specify what you want. A few questions you need to ask (yourself):a) How many rows either side do you wantb) Is productid the primary key or otherwise unique?c) If not, what if your initial select returns more than one row?d) or no rowse) If not, what if your 'previous' and 'next' criteria also return more that one row?f) or no rows?In the easy case you need something like (this is typed in as I think - it won't compile)select * from productwhere productid in(select(select productId from prodwhereproductId=whatever) pidunion ( select max(productId) from product where productID<whatever) union( select min(productId) from product where productID>whatever))order by product idThis assumes uniqueness on prodid. It's similar to your 'seems to be working' solution although guarantees order which yours wont (no order by = no guarantees) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-11 : 12:47:08
|
quote: Originally posted by Srinika
quote: Originally posted by madhivanan Are you using fornt end application? If so, make use of recordset's movenext and movepreviousMadhivananFailing to plan is Planning to fail
 quote: Originally posted by Srinika What is ur front end ? r u using ADO ? if so use MoveNext, MovePrevious.Srinika
Srinika How did I forget to read your previous replies? MadhivananFailing to plan is Planning to fail |
 |
|
|
|