查询以基于主表列表返回Max(Date)和其他表字段

时间:2015-05-28 18:12:16

标签: sql vba maxdate

我希望有人可以帮我解决一个我似乎无法解决的查询问题;

我有两张桌子;

表1

T1_ID
T1_Serial

表2

T2_ID
T1_ID –lookup to Table 1
T2_Date
T2_History

表1数据可能看起来像这样;

T1_ID   T1_Serial
1,  ABC1
2,  ABC2
3,  ABC3
4,  ABC4

表2数据可能如下所示;

T2_ID,  T1_ID,  T2_Date,    T2_History
1,  1,  05/05/15,   “Some History1”
2,  1,  05/17/15,   “Some History2”
3,  2,  05/09/15,   “Some History3”
4,  2,  05/21/15,   “Some History4”
5,  3,  05/12/15,   “Some History5”

我想设置一个查询,为我提供包含表格中每个记录的表历史记录的最大值(日期)的记录

Table1.T1_Serial,Max(T2_Date),T2_History;

对于这个例子;

ABC1    05/17/15    “Some History2”
ABC2    05/21/15    “Some History4”
ABC3    05/12/15    “Some History5”

我已经构建了SQL来给我T1_Serial和Max(Date),它正常工作;

SELECT Table2.T1_ID,  Max(Table2.T2_Date) AS MaxDate
FROM Table2
GROUP BY Table2.T1_ID;

但是当我尝试将T2_History添加到查询中时,我最终获得除Max(Date)之外的所有其他历史记录。

希望有人能带领我走上正确的道路。谢谢!

2 个答案:

答案 0 :(得分:0)

我使用与Sam类似的方法。但是从包装的SQL中删除了T2_ID以避免GROUP BY出现问题。也按日期加入。

SQL Fiddle

class User < ActiveRecord::Base
  has_many :patients
  has_many :assessments, dependent: :destroy
end

class Patient < ActiveRecord::Base
    belongs_to :user
    has_many :assessments, dependent: :destroy
end

class Assessment < ActiveRecord::Base
  belongs_to :user
  belongs_to :template
  belongs_to :patient
  has_many :questions, :through=> :templates
  has_many :answers, :through=> :questions
  accepts_nested_attributes_for :answers
end

class Template < ActiveRecord::Base
    belongs_to :assessment
    has_many :questions
end

class Question < ActiveRecord::Base
  belongs_to :template
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

vba version fiddle

WITH MaxDate (T1_ID, MaxDate) as 
(
    SELECT T1_ID, Max(Table2.T2_Date) AS MaxDate
    FROM Table2
    GROUP BY Table2.T1_ID
)
SELECT T1_Serial, T2_Date, T2_History
FROM 
           MaxDate 
INNER JOIN Table2 ON MaxDate.MaxDate = Table2.T2_Date
INNER JOIN Table1 ON MaxDate.T1_ID = Table1.T1_ID;

答案 1 :(得分:0)

我在VBA上找到了嵌套查询的这个例子。但我无法尝试。祝你好运。

Sub TestNestedQuery()
    Dim RS As DAO.Recordset, strSql As String
    Dim qdfTemp As QueryDef
    Dim qdfNew As QueryDef
    Dim strSQL1 as string
    Dim strSQL2 as string


   strSQL1 = "SELECT RMA_ID, Max(tbl_History.Hist_Date) as MaxDate " & _
             "FROM tbl_History " & _ 
             "Group by tbl_History.RMA_ID"

   strSQL2 = "SELECT tbl_RMAunit.RMA_SN, tbl_History.Hist_Date,tbl_History.Hist_History " & _
             "FROM  qryTemp " & _
             "Inner Join tbl_History on qryTemp.MaxDate = tbl_History.Hist_Date  " & _
             "Inner Join tbl_RMAunit on qryTemp.RMA_ID = tbl_RMAunit.RMA_ID "

    With CurrentDb()
        Set qdfTemp = .CreateQueryDef("qryTemp", strSQL1) '' SQL 1

        Set qdfNew = .CreateQueryDef("qryNew", strSQL2)' SQL 2

        GetrstTemp qdfNew

        ''' Delete the two querydefs if necessary
        .QueryDefs.Delete qdfTemp.Name
        .QueryDefs.Delete qdfNew.Name
    End With

End Sub

Function GetrstTemp(qdfTemp As QueryDef)

   Dim rstTemp As DAO.Recordset
   Dim i As Integer: i = 0

   With qdfTemp
      Debug.Print .Name
      Debug.Print "  " & .sql
      '' Open Recordset from QueryDef.
      Set rstTemp = .OpenRecordset(dbOpenSnapshot)


      Do While Not rstTemp.EOF
          i = i + 1
          Debug.Print rstTemp.Fields("RMA_SN")    '' Change the field name 
          Debug.Print rstTemp.Fields("Hist_Date") '' Change the field name 
          rstTemp.MoveNext
      Loop
      Debug.Print
      Debug.Print "  Number of records = " & _
      rstTemp.RecordCount
   End With

End Function