sql query

Posted on 2002-05-06
Last Modified: 2010-05-02
I hava a table in Microsoft Access.
The table name is real_progress.
Below is the structure + value of the table.

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
    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
    MsgBox "no record"
    Exit Sub
End If
Set rs = Nothing

Thank You in advance.

Question by:boyyle
LVL 43

Expert Comment

ID: 6993535
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)
LVL 18

Expert Comment

ID: 6993680
the above seems ok if sheet number always increases as date increases.  otherwise I could only come up with

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)))

Accepted Solution

pjbaratelli earned 50 total points
ID: 7009775
Here is a single statement that should do what you want. I substituted Date_Time for Date as it is a key word.

SELECT     Date_Time, Sheet_No, Student_ID
FROM  answer
WHERE (Date_Time IN (SELECT MAX(Date_Time) AS Date_Time
FROM answer
WHERE Student_ID = 100
GROUP BY MONTH(Date_Time), Student_ID)) AND (Student_ID = 100)

The second select statement by itself can list all the Student_IDs and the MAX Date_Time for each student if you remove the WHERE clause.

SELECT MAX(Date_Time) AS Date_Time,Student_ID
FROM answer
GROUP BY MONTH(Date_Time), Student_ID

Expert Comment

ID: 7018686
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.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

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.

*****  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
POINTS FOR EXPERTS awaiting comments are listed in the link below
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.
Moderator @ Experts Exchange

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now