| Author |
Topic |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-05 : 00:48:15
|
| hi,I have table photo as below:ID photoname comments1 1.jpg good one...1 2good.jpg test1 3img.jpg test comments..2 i.jpg testest2 ii.jpg testtesttest3 i.good.jpg test3 1td.jpg test3 3td.jpg test3 4td.jpg test4 tt.jpg goodI want first 2 rows from above table ID Wise as below. If only one row for ID then it come once so how can i do this i have tried but confused.... please helpID photoname comments1 1.jpg good one...1 2good.jpg test2 i.jpg testest2 ii.jpg testtesttest3 i.good.jpg test3 1td.jpg test4 tt.jpg goodthanks.. |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-05 : 00:52:31
|
| select * from ( select *,row_number() over ( partition by ID order by id ) as rn from urtable ) twhere t.rn in (1,2) |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-05 : 01:12:43
|
| i have sql 2000 and above query worked in sql 2005.. any other way.. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-05 : 07:17:37
|
| [code]Select Identity(int,1,1) as PhotoID,* into #H from photoSelect M.ID,M.photoname,M.Commentsfrom(Select ID,photoname,Comments,(Select Count(*) from #H Where ID = Z.ID and PhotoID <= Z.PhotoID) as Seqfrom #H Z)MWhere M.seq in (1,2)[/code] |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-05 : 07:58:13
|
| Which 2 rows ?? You need to have a criteria. Sodeep's query would just randomly give you any 2 rows. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-05 : 11:57:47
|
| you need to have a column on your table to determine. there's no such thing like first,second ,...records of a table unless you specify sequence with respect to column(s) value by means of an order by. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-05 : 11:59:36
|
| even using IDENTITY(int,1,1) while inserting into a table using a select with ORDER BY does not guarantee records will inserted and id values will be generated in same orderhttp://support.microsoft.com/kb/273586 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-06 : 07:51:42
|
| thanks for help i have no criteria just last inserted two rows for each photo comment.. Thanks again you all for your precious time :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-06 : 09:09:33
|
quote: Originally posted by keyursoni85 thanks for help i have no criteria just last inserted two rows for each photo comment.. Thanks again you all for your precious time :)
that itself is a criteria for determining insertion sequence, you should have an id field or an audit field like datemodified which stores inserted date value |
 |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-06 : 10:17:59
|
| yes you are right.... :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-07 : 08:44:34
|
| cheers! |
 |
|
|
|