Link to home
Start Free TrialLog in
Avatar of boyyle
boyyle

asked on

sql query

Hi,
I hava a table in Microsoft Access.
The table name is real_progress.
Below is the structure + value of the table.
Student_ID,Date,Sheet_No
100,03/01/2002,25
100,05/01/2002,50
100,14/01/2002,87
100,27/01/2002,110
100,01/02/2002,120
100,05/02/2002,130
100,21/02/2002,145
100,14/03/2002,150
100,17/03/2002,170
100,09/04/2002,200

Now,i'd like to write a sql query to get the Sheet_No whereby this Sheet_No is the
LAST record for that month.
So,the result i expect is 110,145,170 and 200

Meanwhile,i manage to get this result using 2 loops.But i wonder can i done this in a faster way?

Dim query as String
Dim query1 as String
Dim m as Integer
Set rs = New ADODB.Recordset
Set rs1 = New ADODB.Recordset


query = "select distinct Month(Date) as month_temp from real_progress where Student_ID=100 order by Month(Date)"
Set rs = cn.Execute(query)
If Not rs.EOF Then
    rs.MoveFirst
    Do While Not rs.EOF
        m = rs("month_temp")
        query1 = "select Sheet_No from real_progress where Student_ID=100 and Month(Date) = " & m & " order by Date desc"
        Set rs1 = cn.Execute(query1)
        If Not rs1.EOF Then
         Debug.Print m & "<>" & rs1("Sheet_No")
        End If
     Set rs1 = Nothing
        rs.MoveNext
    Loop
Else
    MsgBox "no record"
    Exit Sub
End If
Set rs = Nothing

Thank You in advance.

Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

You should be able to do this in a single statement:

Select Max(Sheet_No) As Sheet_No From Real_Progress Where Student_ID = 100 Group By Month(Date) Order By Month(Date)
the above seems ok if sheet number always increases as date increases.  otherwise I could only come up with

SELECT *
FROM real_progress AS temp
WHERE temp.sheet_no IN  (Select val(mid(buffo,9)) from (select left(fiddle,6), max(fiddle) as buffo from  (Select format(Year(date) ,"0000") + format(month(date),"00") + format(day(date),"00") + cstr(Sheet_No) as fiddle from real_progress where student_id = 100) group by  left(fiddle,6)))
ASKER CERTIFIED SOLUTION
Avatar of pjbaratelli
pjbaratelli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Moondancer
Moondancer

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20186185.html
https://www.experts-exchange.com/questions/Q.20190489.html
https://www.experts-exchange.com/questions/Q.20237648.html
https://www.experts-exchange.com/questions/Q.20252089.html
https://www.experts-exchange.com/questions/Q.20271435.html
https://www.experts-exchange.com/questions/Q.20294821.html
https://www.experts-exchange.com/questions/Q.20294822.html
https://www.experts-exchange.com/questions/Q.20297641.html
https://www.experts-exchange.com/questions/Q.20297636.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange