Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get last similur rows

Author  Topic 

stbox
Starting Member

6 Posts

Posted - 2012-12-04 : 17:02:02
hi
i want to get last rows from table with a similr sid
id resom date time sid uid uname
895 1 1391/09/12 23:18:03 12 9 GFSDG
896 3 1391/09/12 23:18:15 12 9 GFSDG
897 1 1391/09/12 23:38:27 3 9 MJHMK
898 2 1391/09/14 12:21:27 17 9 MJHMK
899 2 1391/09/14 12:21:33 3 9 MJHMK
901 9 1391/09/14 12:21:53 3 9 MJHMK
902 1 1391/09/14 12:21:53 17 9 MJHMK
903 1 1391/09/14 12:22:06 17 9 MJHMK
904 2 1391/09/14 12:22:12 17 9 MJHMK
905 2 1391/09/14 12:22:20 17 9 MJHMK
906 1 1391/09/14 12:22:24 17 9 MJHMK
907 1 1391/09/14 12:22:30 17 9 MJHMK
908 2 1391/09/14 12:22:35 17 9 MJHMK
900 1 1391/09/14 12:21:39 3 9 MJHMK


i want to get last 7 row with sid 17 per user
i cant this !
please help !

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-04 : 17:15:05
so you want last 7 rows for all user if it exists? what is the criteria
Go to Top of Page

GetSome
Starting Member

4 Posts

Posted - 2012-12-04 : 17:15:41
SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-12-04 : 18:03:34
DECLARE @Table TABLE (ID int,resom tinyint,[date] datetime2(0),sid int, uid int, uname char(5))
INSERT INTO @Table
VALUES


(895, 1,'1391/09/12 23:18:03', 12, 9,'GFSDG'),
(896 ,3,'1391/09/12 23:18:15', 12, 9,'GFSDG'),
(897 ,1,'1391/09/12 23:38:27', 3, 9,'MJHMK'),
(898 ,2,'1391/09/14 12:21:27', 17, 9,'MJHMK'),
(899 ,2,'1391/09/14 12:21:33', 3, 9,'MJHMK'),
(901 ,9,'1391/09/14 12:21:53', 3, 9,'MJHMK'),
(902 ,1,'1391/09/14 12:21:53', 17, 9,'MJHMK'),
(903 ,1,'1391/09/14 12:22:06', 17, 9,'MJHMK'),
(904 ,2,'1391/09/14 12:22:12', 17, 9,'MJHMK'),
(905 ,2,'1391/09/14 12:22:20', 17, 9,'MJHMK'),
(906 ,1,'1391/09/14 12:22:24', 17, 9,'MJHMK'),
(907 ,1,'1391/09/14 12:22:30', 17, 9,'MJHMK'),
(908 ,2,'1391/09/14 12:22:35', 17, 9,'MJHMK'),
(900, 1,'1391/09/14 12:21:39', 3, 9,'MJHMK')

SELECT TOP 7 *
FROM @Table
WHERE sid = 17
ORDER BY [DATE] desc


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-04 : 20:59:57
quote:
Originally posted by GetSome

SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17



I guess he wants per user
Go to Top of Page

stbox
Starting Member

6 Posts

Posted - 2012-12-06 : 05:46:13
no
i cant use the id Like this

SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17

and i cant use a count of row Like this :

SELECT TOP 7 *
FROM @Table
WHERE sid = 17
ORDER BY [DATE] desc
number of rows in unknown !
becuse It is not clear how many rows exist !

i have the last sid per user
I want to get the last row of a uname and sid like this:
id reason date time sid uid uname
902 1 1391/09/14 12:21:53 17 9 MJHMK
903 1 1391/09/14 12:22:06 17 9 MJHMK
904 2 1391/09/14 12:22:12 17 9 MJHMK
905 2 1391/09/14 12:22:20 17 9 MJHMK
906 1 1391/09/14 12:22:24 17 9 MJHMK
907 1 1391/09/14 12:22:30 17 9 MJHMK
908 2 1391/09/14 12:22:35 17 9 MJHMK

i want use a uname and sid !

http://i49.tinypic.com/cs55d.png

this link show this sample for this !
last row of user mjmhk and sid 3
3 row
last row of user mjhmk and sid 17
7 row
last wor of user test5 and sid 11
4 row

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-06 : 08:44:08
What is the catch here? Why you don't want 3rd and 4th row?
Go to Top of Page

BobsDesk
Starting Member

11 Posts

Posted - 2012-12-06 : 12:31:03
quote:
Originally posted by stbox

no
i cant use the id Like this

SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17

and i cant use a count of row Like this :

SELECT TOP 7 *
FROM @Table
WHERE sid = 17
ORDER BY [DATE] desc
number of rows in unknown !
becuse It is not clear how many rows exist !

i have the last sid per user
I want to get the last row of a uname and sid like this:
id reason date time sid uid uname
902 1 1391/09/14 12:21:53 17 9 MJHMK
903 1 1391/09/14 12:22:06 17 9 MJHMK
904 2 1391/09/14 12:22:12 17 9 MJHMK
905 2 1391/09/14 12:22:20 17 9 MJHMK
906 1 1391/09/14 12:22:24 17 9 MJHMK
907 1 1391/09/14 12:22:30 17 9 MJHMK
908 2 1391/09/14 12:22:35 17 9 MJHMK

i want use a uname and sid !

http://i49.tinypic.com/cs55d.png

this link show this sample for this !
last row of user mjmhk and sid 3
3 row
last row of user mjhmk and sid 17
7 row
last wor of user test5 and sid 11
4 row




Your Date and time columns are separate, can you tell us the data type?

I think you want the max datetime per uname and sid???

Robert '); drop table students;-- ?
Go to Top of Page

stbox
Starting Member

6 Posts

Posted - 2012-12-15 : 06:50:28
quote:
Originally posted by BobsDesk

quote:
Originally posted by stbox

no
i cant use the id Like this

SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17

and i cant use a count of row Like this :

SELECT TOP 7 *
FROM @Table
WHERE sid = 17
ORDER BY [DATE] desc
number of rows in unknown !
becuse It is not clear how many rows exist !

i have the last sid per user
I want to get the last row of a uname and sid like this:
id reason date time sid uid uname
902 1 1391/09/14 12:21:53 17 9 MJHMK
903 1 1391/09/14 12:22:06 17 9 MJHMK
904 2 1391/09/14 12:22:12 17 9 MJHMK
905 2 1391/09/14 12:22:20 17 9 MJHMK
906 1 1391/09/14 12:22:24 17 9 MJHMK
907 1 1391/09/14 12:22:30 17 9 MJHMK
908 2 1391/09/14 12:22:35 17 9 MJHMK

i want use a uname and sid !

http://i49.tinypic.com/cs55d.png

this link show this sample for this !
last row of user mjmhk and sid 3
3 row
last row of user mjhmk and sid 17
7 row
last wor of user test5 and sid 11
4 row




Your Date and time columns are separate, can you tell us the data type?

I think you want the max datetime per uname and sid???

Robert '); drop table students;-- ?




i want get last rows after last change sid per uname!

id resom date time sid uid uname
895 1 1391/09/12 23:18:03 12 9 GFSDG
896 3 1391/09/12 23:18:15 12 9 GFSDG
897 1 1391/09/12 23:38:27 3 9 MJHMK
898 2 1391/09/14 12:21:27 17 9 MJHMK
899 2 1391/09/14 12:21:33 3 9 MJHMK
901 9 1391/09/14 12:21:53 3 9 MJHMK
902 1 1391/09/14 12:21:53 17 9 MJHMK
903 1 1391/09/14 12:22:06 17 9 MJHMK
904 2 1391/09/14 12:22:12 17 9 MJHMK
905 2 1391/09/14 12:22:20 17 9 MJHMK
906 1 1391/09/14 12:22:24 17 9 MJHMK
907 1 1391/09/14 12:22:30 17 9 MJHMK
908 2 1391/09/14 12:22:35 17 9 MJHMK
900 1 1391/09/14 12:21:39 3 9 MJHMK

sid of uname MJHMK after row 898 is changed !
and row 902 - 908 no changed !
i want to get last row after last change sid per uname !
tnx

Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2012-12-15 : 09:26:59
Post your question in this format to get answer

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -