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
 Specifying A Certain Count

Author  Topic 

boybles
Starting Member

23 Posts

Posted - 2008-07-09 : 05:20:00
I would like to do a select where every 3rd mail record (in order of date) for a certain employee outputs. How would I do this?
THANKS!
Boybles


Example:

TABLE:

Employee
----------
EmployeeID (smalint)
EmployeeName (varchar)

Mail
-----------
MailID (smallint)
EmployeeID (smallint)
MailName (varchar)
MailDate (date)


DATA:

Employee
----------
1|Jim
2|Jane
3|John

Mail
---------
1|2|Test...|2/2/2008
2|1|Hello!!|2/5/2008
3|1|Yo!!!!!|2/7/2008
4|1|Heya...|2/9/2008
5|2|Today..|2/10/2008
6|1|Great!!|2/11/2008
7|3|welcome|2/14/2008
8|1|Hmmm...|2/15/2008
9|1|Orange.|2/16/2008
10|2|No.....|2/17/2008
11|1|Geez...|2/19/2008
12|1|Golly..|2/20/2008

DESIRED RESULT:

EmployeeName|MailID|MailName|Date
---------------------------------
Jim|4|Heya...|2/9/2008
Jim|9|Orange.|2/16/2008
Jane|10|No.....|2/17/2008

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-09 : 06:43:47
if you're using sql 2005 do like this

SELECT t.EmployeeName,t.MailID,t.MailName,t.MailDate
FROM
(SELECT ROW_NUMBER() OVER(PARTITION BY e.EmployeeName ORDER BY m.MailID) AS RowNo,
e.EmployeeName,m.MailID,m.MailName,n.MailDate
FROM Employee e
INNER JOIN Mail m
ON m.EmployeeID=e.EmployeeID
)t
WHERE t.RowNo % 3 =0



Go to Top of Page

Dallr
Yak Posting Veteran

87 Posts

Posted - 2008-07-09 : 12:44:12
This will work in any version and it might also be faster than Visakh16 option. You can test it to make sure.


SELECT M.*, E.*
FROM Mail M
INNER JOIN Employee E ON M.EmployeeID = E.EmployeeID
WHERE ((SELECT COUNT(M_Sub.MailID)
FROM Mail M_Sub
WHERE M_Sub.EmployeeID = M.EmployeeID AND M_Sub.MailID <= M.MailID) % 3)= 0



PS: Visakh16 there is a little typo in your sql. N.Maildate should be. M.maildate

Dallr
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-09 : 12:53:01
quote:
Originally posted by Dallr

This will work in any version and it might also be faster than Visakh16 option. You can test it to make sure.


SELECT M.*, E.*
FROM Mail M
INNER JOIN Employee E ON M.EmployeeID = E.EmployeeID
WHERE ((SELECT COUNT(M_Sub.MailID)
FROM Mail M_Sub
WHERE M_Sub.EmployeeID = M.EmployeeID AND M_Sub.MailID <= M.MailID) % 3)= 0



PS: Visakh16 there is a little typo in your sql. N.Maildate should be. M.maildate

Dallr


yeah that was right. thanks for the spot.
Go to Top of Page

Dallr
Yak Posting Veteran

87 Posts

Posted - 2008-07-09 : 14:20:34
No problem, just trying to help out where I can.

Dallr
Go to Top of Page
   

- Advertisement -