Solved

sql query

Posted on 2002-05-06
4
240 Views
Last Modified: 2010-05-02
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.

0
Comment
Question by:boyyle
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
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)
0
 
LVL 18

Expert Comment

by:deighton
ID: 6993680
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)))
0
 
LVL 1

Accepted Solution

by:
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
0
 
LVL 1

Expert Comment

by:Moondancer
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://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.
http://www.experts-exchange.com/questions/Q.20186185.html
http://www.experts-exchange.com/questions/Q.20190489.html
http://www.experts-exchange.com/questions/Q.20237648.html
http://www.experts-exchange.com/questions/Q.20252089.html
http://www.experts-exchange.com/questions/Q.20271435.html
http://www.experts-exchange.com/questions/Q.20294821.html
http://www.experts-exchange.com/questions/Q.20294822.html
http://www.experts-exchange.com/questions/Q.20297641.html
http://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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://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
0

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