日期:2014-05-17  浏览次数:20919 次

循环数据中获取上条记录的数值并计算数值?
table表 
ID BH SZ
1 20 5
2 21 9
3 22 17
4 23 8
5 24 14

<%
set shows=server.CreateObject("ADODB.RECORDSET")
sqlshows="select * from table order by ID"
shows.open sqlshows,conn,1,1
do while not shows.eof
%>
<table>
<tr>
<td>编号</td>
<td>数值</td>
<td>计算值</td>
</tr>
<tr>
<td><%=shows("BH")%></td>
<td><%=shows("SZ")%></td>
<td>?????</td>
</tr>

</table>
<%
shows.movenext
loop
%>
<%
shows.close
set shows=nothing
%>


具体功能是想实现,如果ID=5 计算对应的SZ数值以及前两条记录的数值 14 8 17 三个数字任意加减乘除,将所得出的整数数值全部显示在表格“计算值”里面。
如果ID=4 就是计算SZ数值 8 17 9 这三个数字的任意加减乘除,能实现吗?
如果ID=2 SZ的数值无法满足三个数字,“计算值”里就显示为0

------解决方案--------------------
<%
set shows=server.CreateObject("ADODB.RECORDSET")
sqlshows="select * from table order by ID"
shows.open sqlshows,conn,1,1
do while not shows.eof
%>
<table>
<tr>
<td>编号</td>
<td>数值</td>
<td>计算值</td>
</tr>
<tr>
<td><%=shows("BH")%></td>
<td><%=shows("SZ")%></td>
<td><%=szSum(shows("ID"))%></td>
</tr>

</table>
<%
shows.movenext
loop
%>
<%
shows.close
set shows=nothing
%>

<%
function szSum(id)
Dim sum:sum=0
Dim n:n=conn.execute("select iif(isnull(count(*)),0,count(*)) from table where id<"&id)(0)
if n>1 then
sum=conn.execute("select sum(sz) from (select top 3 sz from table where id<="&id&" order by id desc)")(0)
end if
szSum=sum
end function
%>