0

所以这一直让我发疯。我正在使用以下查询

SELECT *
FROM
    CensusFacility_Records
WHERE
    Division_Program ='Division 1'
ORDER by JMS_UpdateDateTime DESC

我正在尝试获取最新记录。请记住,在 Division_Program 字段中有多行带有“Division 1”。我只需要从今天开始获取包含“Division 1”的最新记录。

JMS_UpdateDateTime 字段使用月、日、年和时间格式填充时间戳(即 8/23/2013 8:00:05 AM)

我怎样才能得到今天的最新记录?

我正在更新我的问题。我正在尝试写入表中的最新记录。

当我查看表格时,最新记录未更新

<%  
divrec = request.QueryString("div")
Set rstest = Server.CreateObject("ADODB.Recordset")
rstest.locktype = adLockOptimistic
sql = "SELECT TOP 1 * FROM CensusFacility_Records WHERE Division_Program ='Division 1' ORDER BY JMS_UpdateDateTime DESC"
rstest.Open sql, db
%>

<%
Shipment_Current = request.form("Shipment_Current")
Closed_Bed_Current = request.form("Closed_Bed_Current")
Available_Current = request.form("Available_Current")

rstest.fields("Shipment") = Shipment_Current
rstest.fields("Closed_Bed") = Closed_Bed_Current
rstest.fields("Current") = Available_Current
rstest.update
Response.Redirect("chooseScreen.asp")

%>
4

3 回答 3

1

您可以使用相同的查询,但添加 Select Top 1

"SELECT TOP 1 * FROM CensusFacility_Records WHERE Division_Program ='Division 1' ORDER by JMS_UpdateDateTime desc "
于 2013-08-23T18:01:52.573 回答
1

据我了解,TOP关键字就是您要查找的内容。您介意指定要查询的 MSSQL 服务器版本吗?此解决方案仅在您的数据以有效的日期格式(如时间戳)存储时才有效,如果您的数据以文本格式存储,您只需要在对其进行排序之前对其进行转换。

SELECT TOP 1 *
FROM
    CensusFacility_Records
WHERE
    Division_Program ='Division 1'
ORDER BY
    JMS_UpdateDateTime DESC

如果您有任何问题,请随时发表评论!:-)


编辑

好的,现在这是一个 ASP 问题!我从未做过经典的 ASP,但受到几分钟教程的启发,我建议使用这种方法:

  1. 获取从 SQL 选择中获得的对象的主键
  2. 构造以下更新查询
  3. 执行更新查询

查询

UPDATE CensusFacility_Records SET
    Shipment = @Shipment_Current,
    Closed_Bed = @Closed_Bed_Current,
    Current = @AvailableCurrent
WHERE CensusFacility_Records.ID = @ID

使用一个可能类似于此的 ASP 脚本:

sql = "UPDATE CensusFacility_Records SET "
sql = sql & "Shipment ='" & Request.Form("Shipment_Current") & "'," &
sql = sql & "Closed_Bed ='" & Request.Form("Closed_Bed_Current") & "'," &
sql = sql & "Current ='" & Request.Form("AvailableCurrent") &
sql = sql & "WHERE CensusFacility_Records.ID = " & ID
conn.Execute sql
conn.close
于 2013-08-23T19:36:45.030 回答
0

您可以使用子查询查找最后一个 dattimestamp,然后加入该子查询。另外,请不要SELECT *在现实生活中使用。

SELECT  * 
FROM    CensusFacility_Records CR1
INNER JOIN
        (SELECT CR2.Division_Program
                ,Max(CR2.JMS_UpdateDateTime) as LAST_DATE
        FROM    CensusFacility_Records CR2
        GROUP BY
                CR2.Division_Program
        ) SQ
ON      SQ.Division_Program =  CR1.Division_Program
AND     SQ.LAST_DATE = CR1.JMS_UpdateDateTime
WHERE   CR1.Division_Program ='Division 1'
于 2013-08-23T18:05:45.963 回答