SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get last similur rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stbox
Starting Member

6 Posts

Posted - 12/04/2012 :  17:02:02  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/04/2012 :  17:15:05  Show Profile  Reply with Quote
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 - 12/04/2012 :  17:15:41  Show Profile  Reply with Quote
SELECT * FROM TABLENAME WHERE ID>900 AND [SID]=17
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 12/04/2012 :  18:03:34  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/04/2012 :  20:59:57  Show Profile  Reply with Quote
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 - 12/06/2012 :  05:46:13  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/06/2012 :  08:44:08  Show Profile  Reply with Quote
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 - 12/06/2012 :  12:31:03  Show Profile  Reply with Quote
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 - 12/15/2012 :  06:50:28  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/15/2012 :  09:26:59  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000