Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

关于asp+access记录逐条累加求和

lukeaccess  发表于:2012-05-23 19:21:52  
复制

我以前有access做过一个仓库管理系统,做库存统计的时候用Dsum累加求和语句做了流水库存,sql语句如下:
物资类别, 报表联合.编号, 报表联合.入库编号 AS 出入库编号, 报表联合.日期, 报表联合.材料名称, 报表联合.型号规格, 报表联合.计量单位, 报表联合.调入单位 AS 出入库单位, 报表联合.收料人 AS 收发料人, 报表联合.入库数量, 报表联合.出库数量, nz(DSum("[入库数量]","报表联合","编号='" & [编号] & "'and [型号规格] = '" & [型号规格] & "'and [型号规格] = '" & [型号规格] & "'and 日期<=#" & [日期] & "#"))-nz(DSum("[出库数量]","报表联合","编号='" & [编号] & "'and [材料名称] = '" & [材料名称] & "'and [型号规格] = '" & [型号规格] & "'and 日期<=#" & [日期] & "#")) AS 结存数量
FROM 报表联合
ORDER BY 报表联合.日期, 报表联合.材料名称

但是我现在改用asp+access把上方的这个sql移植过去却不行,asp不支持
我现在用的sql语句如下:
sql="SELECT 编号,出入库编号,日期,材料名称,型号规格,计量单位,Sum(入库数量) AS 入库,Sum(出库数量) AS 出库,[入库]-[出库] as 结存,调入单位 FROM 报表联合 GROUP BY 日期,编号,出入库编号,材料名称,型号规格,计量单位,调入单位 HAVING " & cond(cond为查询条件)
但是却不能逐条累加。望高手指点

我要的效果是

编号  出入库编号  日期  材料名称 型号规格  计量单位  入库  出库  结存 出入库单位

113-gg  R-01-01   01-05   钢管     Φ45       米      100    0    100    原厂

113-gg  C-01-01   01-06   钢管     Φ45       米      0      70     30    工地

113-gg  C-01-01   01-06   钢管     Φ45       米      100    10    120    工地

 

Top
煮江品茶 发表于:2012-05-23 19:41:21
sql="select a.*, "
sql=sql & "(select sum(b.入库数量-b.出库数量) from 报表联合 as b where b.编号=a.编号 and b.日期<=a.日期) as 结存 "
sql=sql & "from 报表联合 as a "
sql=sql & "order by a.编号,a.日期"

lukeaccess 发表于:2012-05-25 12:44:36

谢了

可是我想在后面加一个查询条件,修改代码如下
sql="select a.*, "
sql=sql & "(select sum(b.入库数量-b.出库数量) from 报表联合 as b where b.编号=a.编号 and b.日期<=a.日期) as 结存 "
sql=sql & "from 报表联合 as a "
sql=sql & "order by a.编号,a.日期"
sql=sql & "HAVING" & cond(cond为查询条件)但是出错,可在普通聚和函数查询中能用
我的asp代码是这样的
if request("tzc")="查" then
if request("bh")<>""then
if cond="" then
cond="编号 like '%" & trim(request("bh")) & "%'"
else
cond = cond & " AND 编号 like '%" & trim(request("bh")) & "%'"
end if
end if
if request("crkbh")<>""then
if cond="" then
cond="出入库编号 like '%" & trim(request("crkbh")) & "%'"
else
cond = cond & " AND 出入库编号 like '%" & trim(request("crkbh")) & "%'"
end if
end if
if request("clmc")<>"" then
If cond="" Then
cond="材料名称 like '%" & trim(request("clmc")) & "%'"
else
cond = cond & "and 材料名称 like '%" & trim(request("clmc")) & "%'"
end if
end if
if request("ksrq")<>"" then
if cond="" then
cond="日期>=# " & trim(request("ksrq")) & "#"
else
cond=cond & "and 日期>=# " & trim(request("ksrq")) & "#"
end if
end if
if request("jsrq")<>"" then
if cond="" then
cond="日期<=# " & trim(request("jsrq")) & "#"
else
cond=cond &"and 日期<=# " & trim(request("jsrq")) & "#"
end if
end if
if request("dcdw")<>""then
if cond="" then
cond="出入库单位 like '%" & trim(request("crkdw")) & "%'"
else
cond = cond & " AND 出入库单位 like '%" & trim(request("crkdw")) & "%'"
end if
end if
if cond<>""then
sql="select a.*, "
sql=sql & "(select sum(b.入库数量-b.出库数量) from 报表联合 as b where b.编号=a.编号 and b.日期<=a.日期) as 结存 "
sql=sql & "from 报表联合 as a "
sql=sql & "order by a.编号,a.日期"



煮江品茶 发表于:2012-05-25 18:03:12
HAVING改成where不就完事了

煮江品茶 发表于:2012-05-25 18:15:14

鬼打架,哪需要这些乱七八糟的代码,简单的如下书写就可以了:

 

....
if request("tzc")="查" then
  return
end if

cond="True"

cond = cond & " and 编号 like '%" & trim(request("bh")) & "%'"
cond = cond & " and 出入库编号 like '%" & trim(request("crkbh")) & "%'"
cond = cond & " and 材料名称 like '%" & trim(request("clmc")) & "%'"
cond = cond & " and 日期>=#" & nz((request("ksrq"),now()) & "#"
......



lukeaccess 发表于:2012-05-30 12:37:04

谢了再请教一个问题,把记录通过loop中加条件,按需如的样式进行输出,代码如下

<%
dc=session("sql")(此行为传过来的sql语句)
      Response.ContentType   =   "application/vnd.ms-excel"     
      Set rs=Server.CreateObject("ADODB.Recordset")  
  
      rs.open dc,conn,3,1
   
      if   rs.eof   and   rs.bof   then  
            Response.Write"<div   align=center><br>没有记录</div>"  
      else
   crkbh=rs("出入库编号") 
  %>  
      <table cellSpacing=0   cellPadding=0   width="100%"   border=1>  
          <tr>  
              <td align="center" width="10%">物资类别</td>
          <td align="center" width="10%">材料名称</td>
          <td align="center" width="10%">型号规格</td>
        <td align="center" width="10%">单位</td>
        <td align="center" width="10%">调入数量</td>
        <td align="center" width="10%">调入材料描述</td>
        <td align="center" width="10%">调入时间</td>
        <td align="center" width="10%">调出数量</td>
        <td align="center" width="10%">调出单位</td>
        <td align="center" width="10%">调出时间</td>
        <td align="center" width="10%">剩余库存</td>
          </tr>  
  <%do   while   (Not   RS.Eof)   and   (I<RS.PageSize)%> 
  <%if mid(crkbh,1,1)="R" then %>
<tr>
<td align="center" width="%10"><%=rs("物资类别")%></td>
<td align="center" width="%10"><%=rs("材料名称")%></td> 
<td align="center" width="%10"><%=rs("型号规格")%></td>
<td align="center" width="%10"><%=rs("计量单位")%></td>
<td align="center" width="%10"><%=rs("入库数量")%></td>
<td align="center" width="%10">  </td>
<td align="center" width="%10"><%=rs("日期")%></td>
<td align="center" width="%10">  </td>
<td align="center" width="%10">  </td>
<td align="center" width="%10">  </td>
<td align="center" width="%10"><%=rs("结存")%></td>
</tr>
<% else %>
 <tr>
<td align="center" width="%10"><%=rs("物资类别")%></td>
<td align="center" width="%10"><%=rs("材料名称")%></td> 
<td align="center" width="%10"><%=rs("型号规格")%></td>
<td align="center" width="%10"><%=rs("计量单位")%></td>
<td align="center" width="%10">  </td>
<td align="center" width="%10">  </td>
<td align="center" width="%10">  </td>
<td align="center" width="%10"><%=rs("出库数量")%></td>
<td align="center" width="%10"><%=rs("出入库单位")%></td>
<td align="center" width="%10"><%=rs("日期")%></td>
<td align="center" width="%10"><%=rs("结存")%></td>
</tr> 
  <%  
      Rs.MoveNext  
      Loop
      end   IF  
      end   IF  
      Set   Conn   =   Nothing  
      Set   Rs   =   Nothing  
  %>  
  </TABLE>
但是调试显示
错误代码:Loop
错误描述:'loop' 语句缺少 'do'
不知道哪里了,请高手帮忙,这不是 do while 语句中的判断语句出错



总记录:5篇  页次:1/1 9 1 :