Author |
Topic |
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-05 : 04:26:54
|
hi,I have table photo as below: and SQL 2000 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 help for SQL 2000 serverID 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.. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-05 : 04:38:12
|
If there are three or more records for same ID, how do you decide which two should be returned? E 12°55'05.63"N 56°04'39.26" |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-05 : 04:51:37
|
that is the result i want first 2 record for each photo id. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-05 : 04:59:27
|
Please define FIRST.There are no such things as "FIRST" in a relational database.If you mean "any two" or "random 2" I can help you.Otherwise you have to provide information how you decide which records that are considered to be "first". E 12°55'05.63"N 56°04'39.26" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-05 : 05:01:21
|
If you have a DateAdded column which is a datetime column, you can rephrase your question to"I want two oldest/newest records per group, according to DateAdded column".Or if you have an IDENTITY column, you can rephrase your question to"I want two first/last records per group, according to IDENTITY column". E 12°55'05.63"N 56°04'39.26" |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-05 : 11:09:44
|
thanks for the reply...what i want in sql server is i have lots of photos and its comments and one photo has multiple comments..andi want last two comments for each photo..as below --------------------------------------ID photoname comments--------------------------------------1 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 goodand output says..I 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 help for SQL 2000 server------------------------------------ID photoname comments------------------------------------1 1.jpg good one...1 2good.jpg test2 i.jpg testest2 ii.jpg testtesttest3 i.good.jpg test3 1td.jpg test4 tt.jpg good two rows can be lastly added so we have a column date of comments in table which is not here...so coupon of rows 2 for all photos will be an output.I have tried lots of ways but still problem occur in SQL 2000 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-05 : 11:23:33
|
try thisDECLARE @TEMP TABLE ( ID INT, photoname VARCHAR(30), comments VARCHAR(30))INSERT INTO @tempSELECT 1, '1.jpg', 'good one...' UNION ALLSELECT 1, '2good.jpg', 'test' UNION ALLSELECT 1, '3img.jpg', 'test comments..' UNION ALLSELECT 2, 'i.jpg', 'testest' UNION ALLSELECT 2, 'ii.jpg', 'testtesttest' UNION ALLSELECT 3, 'i.good.jpg', 'test' UNION ALLSELECT 3, '1td.jpg', 'test' UNION ALLSELECT 3, '3td.jpg', 'test' UNION ALLSELECT 3, '4td.jpg', 'test' UNION ALLSELECT 4, 'tt.jpg', 'good' SELECT a.id,b.photoname,b.comments FROM (SELECT DISTINCT id FROM @temp t) aCROSS APPLY (SELECT TOP 2 * FROM @TEMP WHERE id = a.id) b |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-05 : 11:31:40
|
This doesn't work in SQL 2000. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-05 : 11:32:53
|
I have already given query for SQL 2000.You need to determine what you need:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119256 |
|
|
Padmaja
Starting Member
6 Posts |
Posted - 2009-02-06 : 04:33:56
|
DECLARE @temp TABLE ( ID INT, photoname VARCHAR(30), comments VARCHAR(30))INSERT INTO @tempSELECT 1, '1.jpg', 'good one...' UNION ALLSELECT 1, '2good.jpg', 'test' UNION ALLSELECT 1, '3img.jpg', 'test comments..' UNION ALLSELECT 2, 'i.jpg', 'testest' UNION ALLSELECT 2, 'ii.jpg', 'testtesttest' UNION ALLSELECT 3, 'i.good.jpg', 'test' UNION ALLSELECT 3, '1td.jpg', 'test' UNION ALLSELECT 3, '3td.jpg', 'test' UNION ALLSELECT 3, '4td.jpg', 'test' UNION ALLSELECT 4, 'tt.jpg', 'good' DECLARE @temp1 TABLE ( ID INT, photoname VARCHAR(30), comments VARCHAR(30))DECLARe @var INTSELECT @var = (SELECT MIN(id) FROM @temp)WHILE(@var IS NOT NULL)BEGININSERT INTO @temp1SELECT TOP 2 * FROM @tempWHERE id = @varSELECT @var = (SELECT MIN(id) FROM @temp WHERE id > @var)ENDSELECT * FROM @temp1padmaja |
|
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2009-02-06 : 08:00:45
|
thanks for help i have no criteria just last inserted two rows for each photo comment..Thanks again you all for your precious time :)Go to Top of Page |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-09 : 11:36:45
|
Hi,Try like this select * from @tempwhere photoname like '%[0-2]%'or photoname like 'i%' or photoname like 't%' |
|
|
|