Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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!BoyblesExample:TABLE:Employee----------EmployeeID (smalint)EmployeeName (varchar)Mail-----------MailID (smallint)EmployeeID (smallint)MailName (varchar)MailDate (date)DATA:Employee----------1|Jim2|Jane3|JohnMail---------1|2|Test...|2/2/20082|1|Hello!!|2/5/20083|1|Yo!!!!!|2/7/20084|1|Heya...|2/9/20085|2|Today..|2/10/20086|1|Great!!|2/11/20087|3|welcome|2/14/20088|1|Hmmm...|2/15/20089|1|Orange.|2/16/200810|2|No.....|2/17/200811|1|Geez...|2/19/200812|1|Golly..|2/20/2008DESIRED RESULT:EmployeeName|MailID|MailName|Date---------------------------------Jim|4|Heya...|2/9/2008Jim|9|Orange.|2/16/2008Jane|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.MailDateFROM(SELECT ROW_NUMBER() OVER(PARTITION BY e.EmployeeName ORDER BY m.MailID) AS RowNo,e.EmployeeName,m.MailID,m.MailName,n.MailDateFROM Employee eINNER JOIN Mail mON m.EmployeeID=e.EmployeeID)tWHERE t.RowNo % 3 =0
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.EmployeeIDWHERE ((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.maildateDallr
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.EmployeeIDWHERE ((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.maildateDallr
yeah that was right. thanks for the spot.
Dallr
Yak Posting Veteran
87 Posts
Posted - 2008-07-09 : 14:20:34
No problem, just trying to help out where I can. Dallr