Sql 按日期排序,但将类似字段分组

Sql 按日期排序,但将类似字段分组,sql,asp-classic,grouping,Sql,Asp Classic,Grouping,我是新的堆栈溢出和ASP,但这个网站已经救了我很多次了!我非常不熟悉ASP和VBS,但更熟悉PHP,所以如果有一个PHP解决方案来解决我的问题,那也可以 一点背景知识-MyAccess DB有两个与此查询相关的表,一个称为SignUpLog,另一个称为Notes。SignUpLog.FirstNoteAddr字段对应于另一个表中的Notes.NoteKey字段 我已经成功地显示了数据库中的所有条目,但我想做的是将特定患者的所有条目分组在一行中,同时仍按顶部的最新日期排序 这是我的密码: Set

我是新的堆栈溢出和ASP,但这个网站已经救了我很多次了!我非常不熟悉ASP和VBS,但更熟悉PHP,所以如果有一个PHP解决方案来解决我的问题,那也可以

一点背景知识-MyAccess DB有两个与此查询相关的表,一个称为SignUpLog,另一个称为Notes。SignUpLog.FirstNoteAddr字段对应于另一个表中的Notes.NoteKey字段

我已经成功地显示了数据库中的所有条目,但我想做的是将特定患者的所有条目分组在一行中,同时仍按顶部的最新日期排序

这是我的密码:

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>
我想要的是:

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

我曾经尝试过使用GROUPBY,并一直挂在聚合函数上,这让人困惑,因为我不想做任何数学上的事情。就像我说的,我是一个彻头彻尾的ASP noob,我绝对不是一个程序员

我相信你们的问题已经有了解决方案,所以,试着看看这个问题。您只需要定义一个有点复杂的函数,并像示例中那样使用它。

这实际上是一个SQL查询问题,与ASP几乎没有关系。由于您使用的是MDB,您可能会发现使用MS Access对查询建模更容易,然后将生成的SQL语句粘贴回ASP代码中

以下问题可能会有所帮助:


HTML中的严重错误

无论如何,这里有一个快速的解决方案,通过对逻辑进行一些mod来循环遍历结果集并显示它

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

我不确定这是一个SQL答案还是一个我正在搜索的可搜索的答案。我在你的帖子中尝试了解决方案,但不断出现错误,我试图排序的列不是聚合函数的一部分。谢谢你的快速回复!试着在MS Access visual query builder界面中做你想做的事情,这会让它变得非常简单。这有点让我头疼。我想做的事情似乎有点复杂。也许你可以详细说明一下,记住我现在是一个noob谢谢所有的帮助,我最终使用mdb export将数据直接导入mysql,然后我可以使用PHP和mysql中内置的函数获得我想要的结果。小组赛获胜!
<%
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>