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

问个简单的分组问题
pName   cIn   cOut   cR
芬达     100     40     60
雪碧     500     250     250
芬达     300     60     240
有这个数据库,第一行为字段名,其他三行为记录,
页面输出时要将pName相同的其他字段合并,
结果是:
pName   cIn   cOut   cR
芬达     400     100     300
雪碧     500     250     250

sql= "select   cIn,cOut,cR   from   table   group   by   pName "对吗?
应当怎么写?
其他的语句也请高手帮我补完整。


------解决方案--------------------
<%
dim rs,sql,pname,expr1,expr2,expr3
set rs=server.createobject( "adodb.recordset ")
sql= "SELECT pname, SUM(cin) AS expr1, SUM(cout) AS expr2, SUM(cr) AS expr3 FROM test GROUP BY pname "
rs.open sql,conn,1,1
if rs.recordcount <> 0 then
%>
<table border= "1 " width= "50% " cellpadding= "0 " cellspacing= "0 " bordercolorlight= "#000000 " bordercolordark= "#FFFFFF ">
<tr>
<td width= "25% " align= "center "> pName </td>
<td width= "25% " align= "center "> cIn </td>
<td width= "25% " align= "center "> cOut </td>
<td width= "25% " align= "center "> cR </td>
</tr>
<%
while not rs.eof
pname=rs( "pname ")
expr1=rs( "expr1 ")
expr2=rs( "expr2 ")
expr3=rs( "expr3 ")
%>
<tr>
<td width= "25% " align= "center "> <%=pname%> </td>
<td width= "25% " align= "center "> <%=expr1%> </td>
<td width= "25% " align= "center "> <%=expr2%> </td>
<td width= "25% " align= "center "> <%=expr3%> </td>
</tr>
<%
rs.movenext
wend
end if
rs.close
set rs=nothing
conn.close
set conn=nothing
%>
</table>