简体   繁体   中英

Sort by date but group similar fields

I am new to Stack Overflow and to ASP, but this site has bailed me out many times! I am very unfamiliar with ASP and VBS but more familiar with PHP, so if there is a PHP solution to my problem, that will be okay too.

A little background - My access DB has two tables (that are relevant to this query) one is called SignUpLog and the other Notes . The SignUpLog.FirstNoteAddr field corresponds to the Notes.NoteKey field in the other table.

I've been successful in showing all the entries in the DB, but what I'd like to do is group all entries for a specific patient together in one row, while still ordering by date (newest at top).

Here's my code:

Set DataConn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.RecordSet")
DataConn.Open "DBQ=" & Server.Mappath("/path/to/mydb") & ";Driver={Microsoft Access Driver (*.mdb)};Uid=user;Pwd=pass;"

Set rsDsp = DataConn.Execute("SELECT SignUpLog.PatientFileNumber, SignUpLog.ArrivalDateTime, Notes.Note, SignUpLog.Called, SignUpLog.DrName FROM SignUpLog, Notes WHERE (((Notes.NoteKey)=[SignUpLog].[FirstNoteAddr])) ORDER BY SignUpLog.ArrivalDateTime DESC;")

If rsDsp.EOF Then
    Response.Write "Sorry, no entries in the database!"
Else
%>
<div align="center"><center>
    <table BORDER="0" width="700">
        <tr>
            <th width="105">Name</th>
            <th width="105">Arrival Time</th>
            <th width="105">Doctor</th>
            <th width="105">Notes</th>
        </tr>
 <%
   While Not rsDsp.EOF
     x = x + 1
     If x = 1 Then
       Response.Write "<TR><TD>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</TD>"
       Response.Write "<TD>" & rsDsp("ArrivalDateTime").Value & "</TD>"
        Response.Write "<TD>" & rsDsp("DrName").Value & "</TD>"
        Response.Write "<TD>" & rsDsp("Note").Value & "</TD></TR>"
     Else 
       Response.Write "<TR><TD BGCOLOR=E4E4E4>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</TD>"
       Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("ArrivalDateTime").Value & "</TD>"
       Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("DrName").Value & "</TD>"
       Response.Write "<TD BGCOLOR=E4E4E4>" & rsDsp("Note").Value & "</TD></TR>"
       x = 0
     End If

     rsDsp.MoveNext
   Wend
   Response.Write "</TABLE>"  


   DataConn.Close

   End If
  %>
 </table>
 </center></div>

This gives me an output similar to this:

Patient A | 9/18/2012 12:56:21 PM | Appt | Note1
Patient A | 9/18/2012 12:56:21 PM | Appt | Note2
Patient A | 9/18/2012 12:56:21 PM | Appt | Note3
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note1
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note2

What i would like is this :

Patient A | 9/18/2012 12:56:21 PM | Appt | Note1, Note2, Note3
Patient B | 9/18/2012 1:56:21 PM | WalkIn | Note1, Note2

I've tried playing around with Group By and keep getting hung up on Aggregate functions, which is confusing because I'm not trying to do anything mathematical. Like said I am a complete ASP noob, and I'm not a programmer by no means.

I believe there already is a solution for your problem here on SO, try looking at THIS question. You will just need to define (a bit complicated) function and use it as in example.

This is really a SQL query issue that has little to do with ASP. Since you're using MDB you may find it easier to model your queries using MS Access, then paste the generated SQL statement back into your ASP code.

The following question can help:

SQL Group with Order by

Serious fails in the HTML.

Anyway, here's a quick solution by making a few mods to the logic for looping through the result set and displaying it.

<%
Set DataConn = Server.CreateObject("ADODB.Connection") 
Set RS = Server.CreateObject("ADODB.RecordSet") 
DataConn.Open "DBQ=" & Server.Mappath("/path/to/mydb") & ";Driver={Microsoft Access Driver (*.mdb)};Uid=user;Pwd=pass;" 

Set rsDsp = DataConn.Execute("SELECT SignUpLog.PatientFileNumber, SignUpLog.ArrivalDateTime, Notes.Note, SignUpLog.Called, SignUpLog.DrName FROM SignUpLog, Notes WHERE (((Notes.NoteKey)=[SignUpLog].[FirstNoteAddr])) ORDER BY SignUpLog.ArrivalDateTime DESC;") 

If rsDsp.EOF Then 
    Response.Write "Sorry, no entries in the database!" 
Else 
%> 
<div align="center">
    <table BORDER="0" width="700"> 
        <tr> 
            <th width="105">Name</th> 
            <th width="105">Arrival Time</th> 
            <th width="105">Doctor</th> 
            <th width="105">Notes</th> 
        </tr> 
 <%
   Dim LastPatient;
   Dim Notes = "";
   Dim x = 0;
   While Not rsDsp.EOF

     If LastPatient = rsDsp.Field.Item("PatientFileNumber").Value Then
       Response.Write "<br />" & rsDsp("Note").Value
     Else
       If LastPatient <> NULL Then
         Response.Write "</td></tr>"
       If x Mod 2 = 0 Then 
         Response.Write "<tr><td>" & rsDsp.Fields.Item("PatientFileNumber").Value & "</td>" 
         Response.Write "<td>" & rsDsp("ArrivalDateTime").Value & "</td>" 
         Response.Write "<td>" & rsDsp("DrName").Value & "</td>" 
         Response.Write "<td>" & rsDsp("Note").Value
       Else  
         Response.Write "<tr><td bgcolor=""E4E4E4"">" & rsDsp.Fields.Item("PatientFileNumber").Value & "</td>" 
         Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("ArrivalDateTime").Value & "</td>" 
         Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("DrName").Value & "</td" 
         Response.Write "<td bgcolor=""E4E4E4"">" & rsDsp("Note").Value
       End If
       x = x + 1
     End If

     LastPatient = rsDsp.Fields.Item("PatientFileNumber").Value
     rsDsp.MoveNext 
   Wend
   Response.Write "</td></tr>"   


   DataConn.Close 

End If
%> 
 </table> 
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM