在asp中操作行转列的问题。把一个日程计划转换成按星期表述

jianice 2009-12-02 10:56:13
(我是要在asp中完成的,实际的表比这些要复杂,有很多行,我把一些不相关的去掉了,只是一个尽量简化的表。)

表内数据如下:
=================================
id content pdate
------------------------
a 吃 饭  2009-08-01
a 睡 觉  2009-08-01
a 吃 饭  2009-08-02
a 吃 饭  2009-08-04
a 坐 车  2009-08-04
a 吃 饭  2009-08-05
a 吃 饭  2009-08-06
a 吃 饭  2009-08-07
b 工 作  2009-08-01
b 工 作  2009-08-02
b 工 作  2009-08-03
b 工 作  2009-08-04
b 工 作  2009-08-05
b 工 作  2009-08-07
===========================================

假设2009-08-01是星期一,那么,我最终想要如下形式的表格:
=========================================================
Id 星期一 星期二 星期三 星期四 星期五 星期六 星期天
-------------------------------------------------------
a  吃 饭 吃 饭     吃 饭 吃 饭 吃 饭 吃 饭
  睡 觉        坐 车
--------------------------------------------------------
b  工 作 工 作 工 作 工 作 工 作     工 作
=========================================================
请问如何实现?谢谢各位达人!!!是要在asp中实现的噢,不是仅把这十几行用存储过程或sql代码直接行转列,那达不到我的目的。

我现在是在asp中用循环实现的,思路是先把每个id查出来,然后再循环嵌套查询每个id每天做的事情,再显示,能达到目标,但速度太慢,居然要5秒钟左右。哭!!! 如果只能采用如下的方式,那如何优化该程序?
(以下是我的程序)
---------------
<tr bgcolor="#ffffff" height="20px">
<td width=45 align=center>[人员]</td>
<td width=75 align=center>星期一</td>
<td width=75 align=center>星期二</td>
<td width=75 align=center>星期三</td>
<td width=75 align=center>星期四</td>
<td width=75 align=center>星期五</td>
<td width=75 align=center>星期六</td>
<td width=75 align=center>星期日</td>
</tr>

<%
Set Rsm = Server.CreateObject("ADODB.RecordSet")
Sqlm="Select * From users order by name"
Rsm.Open Sqlm,Conn,2,3
member=Rsm.getrows 'member是数组,其中,member(3,行)为真实姓名,member(1,行)为id
membercount=UBound(member,2) 'membercount为用户总数
Rsm.close
set Rsm=nothing

for im=0 to membercount '对每一个用户进行循环,

%>

<tr>
<td><%=Trim(member(3,im))%></td>

<%
current_week_begin="2009-08-01" 设置该天为星期一

Set Rsplan = Server.CreateObject("ADODB.RecordSet")

For iplan=0 To 6 '对每一个用户的星期一至星期天进行查询,共七次
Sqlplan="Select content From plans where id='"&member(1,im)&"' and pdate ='"&cdate(current_week_begin)+iplan&"'"
Rsplan.Open Sqlplan,conn,0,1
%>
<td valign=top width=100px>
<%
If rsplan.eof Or rsplan.bof Then
response.write(" ")
Else
rsplan.movefirst
Do While Not Rsplan.Eof '有多条,则显示多条记录
response.write(rsplan("content"))
response.write("<br>")
rsplan.movenext
Loop
End If
%>
</td>

<%
rsplan.close
Next '结束内循环,即每用户的周一至周日
%>
</tr>

<%
next '结束外循环,即每一个用户
%>


-------------------------------------------
以上程序能达到我的要求,但就是太慢啦,为何呢?如何优化我的程序,或者,不用这种记录集的嵌套,而用其他的方法?要求在asp中实现噢。
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianice 2009-12-10
  • 打赏
  • 举报
回复
谢谢ls!!
尽管代码有点问题,但帮我解决了难题!
homel 2009-12-02
  • 打赏
  • 举报
回复
create table aa(id varchar(10),
contens varchar(20),
pdate datetime)
insert into aa select 'a','吃饭','2009-08-01'
union
select 'a','睡觉','2009-08-02'

select * from aa


select id, max(case when datepart(dw,pdate)=2 then contens else ''end) as 星期一,
max(case when datepart(dw,pdate)=3 then contens else ''end) as 星期二,
max(case when datepart(dw,pdate)=4 then contens else ''end) as 星期三,
max(case when datepart(dw,pdate)=5 then contens else ''end) as 星期四,
max(case when datepart(dw,pdate)=6 then contens else ''end) as 星期五,
max(case when datepart(dw,pdate)=7 then contens else ''end) as 星期六,
max(case when datepart(dw,pdate)=1then contens else ''end) as 星期天
from aa
group by id
Jimdo_260122332 2009-12-02
  • 打赏
  • 举报
回复
OO...UP
lzp4881 2009-12-02
  • 打赏
  • 举报
回复
<%
str="<table border=1>"
str=str&"<tr bgcolor='#ffffff' height='20px'>"
str=str&"<td width=45 align=center>[人员]</td>"
str=str&"<td width=75 align=center>星期一</td>"
str=str&"<td width=75 align=center>星期二</td>"
str=str&"<td width=75 align=center>星期三</td>"
str=str&"<td width=75 align=center>星期四</td>"
str=str&"<td width=75 align=center>星期五</td>"
str=str&"<td width=75 align=center>星期六</td>"
str=str&"<td width=75 align=center>星期日</td>"
str=str&"</tr>"

current_week_begin="2009-08-01"
Sql="select a.trueName,b.content,pdate from users a inner join plans b on a.id=b.id where b.pdate between '"& current_week_begin &"' and '"&cdate(current_week_begin)+6&"' order by a.trueName,b.pdate"
set rs=conn.execute(sql)
Dim wk(6)
do while not rs.eof
for i=0 to 6
wk(i) = ""
next
trueName=rs("trueName")
do while trueName=rs("trueName")
dim i : i = WeekDay(rs("pDate"))-1
wk(i) = wk(i) & rs("content") & "<br>"
rs.movenext
if rs.eof then exit do
loop

str = str & "<tr valign='top'>"
str = str & "<td align=center>" & trueName & "</td>"
for i=1 to 6
str = str&"<td align=center>"&wk(i)&"</td>"
next
str = str & "<td align=center>"&wk(0)&"</td>"
str = str & "</tr>"
loop
rs.close
set rs=nothing

str=str&"</table>"
response.write str
%>

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧