我一直试图找到一个类似的案例,但找不到符合我案例的案例。
我在TestDB中有2个表:带有StartDate列的Activity表,以及带有Duration列数据的子表ActivityLog表。
Create Table Activity (
ActivityID int Not Null,
StartDate Date,
Summary varchar(255),
Primary Key (ActivityID)
);
和
Create Table ActivityLog (
ActivityID int Foreign Key References Activity(ActivityID),
LogID int Not Null Primary Key,
Duration int Not Null,
Comment varchar(255) Not Null
);
我编写ASP Classic来生成表格。我的代码如下:
<%
'Set Variable
Dim objCN 'ADO Connection Object
Dim objRS 'ADO Recordset Object
'Create a Connection Object
Set objCN = Server.CreateObject("ADODB.Connection")
'Connect to SQL Server Database, connection string in Global.asa file
objCN.Open Application("ConnStrTestDB")
'SQL Query for select Person Name
strSQLAct="SELECT a.StartDate, al.Duration, al.Comment FROM Activity AS a INNER JOIN ActivityLog AS al ON a.ActivityID = al.ActivityID"
'Create Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQLAct, objCN
%>
<table>
<thead class="thead-light">
<tr>
<th>Start Time</th>
<th>Duration (hours)</th>
<th>End Time</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<% Do While Not objRS.EOF %>
<tr>
<td><%Response.Write(objRS("StartDate"))%></td>
<td><%Response.Write(objRS("Duration"))%></td>
<td><%Response.Write(objRS("StartDate"))%></td>
<td><%Response.Write(objRS("Comment"))%></td>
</tr>
<%
objRS.MoveNext
Loop
%>
</tbody>
</table>
<!--Close Recordset-->
<%
objRS.Close
Set objRS = Nothing
%>
我想在结果表中添加“开始时间”和“结束时间”列。开始时间使用时间值从a.StartDate数据计算得出。结束时间是开始时间+持续时间。持续时间以小时为单位。
我真的很感激如何实现它。
谢谢
我按照@Aleksei Lychev的回答,我在SQL Management Studio中测试了查询,它运行得很完美。然后我更新了我的ASP Classic,如下所示:
<%
'Set Variable
Dim objCN 'ADO Connection Object
Dim objRS 'ADO Recordset Object
'Create a Connection Object
Set objCN = Server.CreateObject("ADODB.Connection")
'Connect to SQL Server Database, connection string in Global.asa file
objCN.Open Application("ConnStrTestDB")
'SQL Query for select Person Name
strSQLAct = "SELECT a.StartDate, a.Summary, al.Duration, al.Comment " + _
"DATEADD(hh, (SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE UNBOUNDED PRECEDING) - SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE CURRENT ROW)), CONVERT(SMALLDATETIME, a.StartDate)) AS StartTime " + _
"DATEADD(hh, SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE UNBOUNDED PRECEDING), CONVERT(SMALLDATETIME, a.StartDate)) AS EndTime " + _
"FROM Activity AS a " + _
"INNER JOIN ActivityLog AS al ON al.ActivityID = a.ActivityID " + _
"ORDER BY a.ActivityID, al.LogID;"
'Create Recordset
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open strSQLAct, objCN
%>
<table>
<thead class="thead-light">
<tr>
<th>Start Time</th>
<th>Duration (hours)</th>
<th>End Time</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<% Do While Not objRS.EOF %>
<tr>
<td><%Response.Write(objRS("StartTime"))%></td>
<td><%Response.Write(objRS("EndTime"))%></td>
<td><%Response.Write(objRS("Duration"))%></td>
<td><%Response.Write(objRS("Comment"))%></td>
</tr>
<%
objRS.MoveNext
Loop
%>
</tbody>
</table>
<!--Close Recordset-->
<%
objRS.Close
Set objRS = Nothing
%>
出现以下错误:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'hh'.
/TestDBTimeLog.asp, line 30
我已经尝试了谷歌,但我找不到导致问题的原因。
答案 0 :(得分:0)
在我的脑海中,假设一个Activity中的ActivityLogs由LogID和#34; sql-server&#34;标签是对的:
SELECT
a.StartDate,
a.Summary,
al.Duration,
al.Comment,
DATEADD(hh,
SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE UNBOUNDED PRECEDING) - SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE CURRENT ROW),
CONVERT(SMALLDATETIME, a.StartDate)) AS StartTime,
DATEADD(hh,
SUM(al.Duration) OVER (PARTITION BY a.ActivityID ORDER BY al.LogID RANGE UNBOUNDED PRECEDING),
CONVERT(SMALLDATETIME, a.StartDate)) AS EndTime
FROM Activity AS a
INNER JOIN ActivityLog AS al
ON al.ActivityID = a.ActivityID
ORDER BY a.ActivityID, al.LogID
我对窗口功能不太好,这肯定可以做得更好,但我希望你有一个想法。