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.
Author |
Topic |
devbox
Starting Member
1 Post |
Posted - 2007-09-24 : 19:52:50
|
Hello,I have the following query that provides the data I need from a table with id, name, date, and mark. I need the query to get up to the last 10 marks per name based on all marks from last month and only those >=50 further than last month (currently 2 months past last month) Can anyone point me in the right direction? Here is what I have so far...SELECT A.ID, A.NAME, A.EDATE, A.EMARKFROM DATA AS A INNER JOIN DATA AS B ON (A.NAME = B.NAME) and (A.edate<=b.edate)WHERE (((A.EDATE)>=DateSerial(Year(Date()),Month(Date())-1,1) And (A.EDATE)<=DateSerial(Year(Date()),Month(Date()),0))) OR (((A.EDATE)<=DateSerial(Year(Date()),Month(Date())-1,0) And (A.EDATE)>=DateSerial(Year(Date()),Month(Date())-3,1)) AND ((A.EMARK)>=50))GROUP BY A.ID, A.NAME, A.EDATE, A.EMARKHAVING (((Count(*))<=10))ORDER BY 2,3 asc;Here is the test data...19826 Bill 06/01/07 10019829 Bill 06/30/07 9019830 Bill 07/01/07 10019832 Bill 07/29/07 8019833 Bill 07/31/07 9019835 Bill 08/01/07 10019836 Bill 08/02/07 4919828 Bill 08/29/07 5019837 Bill 08/29/07 8019838 Bill 08/31/07 90Here is the current query result...19829 Bill 06/30/07 9019830 Bill 07/01/07 10019832 Bill 07/29/07 8019833 Bill 07/31/07 9019835 Bill 08/01/07 10019836 Bill 08/02/07 4919828 Bill 08/29/07 5019837 Bill 08/29/07 8019838 Bill 08/31/07 90For some reason it does not show the 10th record on 06/01/2007.Help :) |
|
|
|
|
|
|