主题:[原创]基于Access数据库的列转行
网上搜索了都是SQL Server的。于是自己初步写了用Access数据库的列转行:
测试地址1:
http://www.lxasp.com/_DEMO/ctr/index.asp
测试地址2:
http://www.lxasp.com/_DEMO/ctr/index1.asp
更多的列转行!
[quote]
<%@LANGUAGE="VBScript" CODEPAGE="936"%>
<%
Option Explicit
Public startime,endtime,conn,connstr,rs,sql,sqlcount,dbpath
startime=Timer()
sqlcount=0
Session.CodePage=936
Response.ContentType="text/html"
Response.Charset="gb2312"
dbpath="PP.mdb"
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbpath)
'connstr="Provider=SQLOLEDB.1;User ID=sa;Password=sa;Initial Catalog=lxcms;Data Source=(local)"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr
'conn.CursorLocation=3
Dim i,j,arr,pre,pre1
Dim ttCol
Server.ScriptTimeOut=99999
'按小到大的顺序
arr=Array(101,102,201,202,301,302,401,402)
ttCol=UBound(arr)
pre1=ttCol+1
SQL = " SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=101 AND cf_data>'150') ORDER BY cd_id, cf_cid " & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=102 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=201 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=202 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=301 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=302 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=401 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=402 AND cf_data>'150') ORDER BY cd_id, cf_cid"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.Open SQL,conn,1,1 '11 for Read '13 for Write
sqlcount=sqlcount+1
If rs.EOF And rs.BOF Then
Else
rs.MoveFirst
Response.Write "<table border=1><tr><th>名称</th>"
For i=0 To ttCol
Response.Write "<th>" & arr(i) & "</th>"
Next
Do Until rs.EOF
If pre<>rs(0) Then
pre=rs(0)
For i=pre1 To ttCol
Response.Write "<td> </td>"
Next
'输出前N个非列转行的
Response.Write "</tr>" & vbCrLf & "<tr><td>" & rs(1) & "</td>"
For i=0 To ttCol
If rs(2)=arr(i) Then
For j=1 To i
Response.Write "<td> </td>"
Next
Response.Write "<td>" & rs(3) & "</td>"
pre1=i+1
Exit For
End If
Next
Else
For i=1 To ttCol
If rs(2)=arr(i) And i>pre1 Then
For j=pre1 To i-1
Response.Write "<td> </td>"
pre1=pre1+1
Next
Exit For
End If
Next
Response.Write "<td>" & rs(3) & "</td>"
pre1=pre1+1
End If
rs.MoveNext
Loop
For i=pre1 To ttCol
Response.Write "<td> </td>"
Next
Response.Write "</tr></table>"
End If
rs.Close
Set rs = Nothing
Response.Write "<br >Script Execute Time:" & FormatNumber((Timer-startime)*1000,3) & "<br/>Read Template/RecordSet Count:" & sqlcount
%>
[/quote]
测试地址1:
http://www.lxasp.com/_DEMO/ctr/index.asp
测试地址2:
http://www.lxasp.com/_DEMO/ctr/index1.asp
更多的列转行!
[quote]
<%@LANGUAGE="VBScript" CODEPAGE="936"%>
<%
Option Explicit
Public startime,endtime,conn,connstr,rs,sql,sqlcount,dbpath
startime=Timer()
sqlcount=0
Session.CodePage=936
Response.ContentType="text/html"
Response.Charset="gb2312"
dbpath="PP.mdb"
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(dbpath)
'connstr="Provider=SQLOLEDB.1;User ID=sa;Password=sa;Initial Catalog=lxcms;Data Source=(local)"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstr
'conn.CursorLocation=3
Dim i,j,arr,pre,pre1
Dim ttCol
Server.ScriptTimeOut=99999
'按小到大的顺序
arr=Array(101,102,201,202,301,302,401,402)
ttCol=UBound(arr)
pre1=ttCol+1
SQL = " SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=101 AND cf_data>'150') ORDER BY cd_id, cf_cid " & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=102 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=201 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=202 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=301 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=302 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=401 AND cf_data>'150') ORDER BY cd_id, cf_cid" & _
" UNION ALL SELECT cd_id, cd_name, cf_cid, cf_data FROM Cont_Field INNER JOIN Cont_Detail ON Cont_Field.cf_did = Cont_Detail.cd_id WHERE (cf_cid=402 AND cf_data>'150') ORDER BY cd_id, cf_cid"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.Open SQL,conn,1,1 '11 for Read '13 for Write
sqlcount=sqlcount+1
If rs.EOF And rs.BOF Then
Else
rs.MoveFirst
Response.Write "<table border=1><tr><th>名称</th>"
For i=0 To ttCol
Response.Write "<th>" & arr(i) & "</th>"
Next
Do Until rs.EOF
If pre<>rs(0) Then
pre=rs(0)
For i=pre1 To ttCol
Response.Write "<td> </td>"
Next
'输出前N个非列转行的
Response.Write "</tr>" & vbCrLf & "<tr><td>" & rs(1) & "</td>"
For i=0 To ttCol
If rs(2)=arr(i) Then
For j=1 To i
Response.Write "<td> </td>"
Next
Response.Write "<td>" & rs(3) & "</td>"
pre1=i+1
Exit For
End If
Next
Else
For i=1 To ttCol
If rs(2)=arr(i) And i>pre1 Then
For j=pre1 To i-1
Response.Write "<td> </td>"
pre1=pre1+1
Next
Exit For
End If
Next
Response.Write "<td>" & rs(3) & "</td>"
pre1=pre1+1
End If
rs.MoveNext
Loop
For i=pre1 To ttCol
Response.Write "<td> </td>"
Next
Response.Write "</tr></table>"
End If
rs.Close
Set rs = Nothing
Response.Write "<br >Script Execute Time:" & FormatNumber((Timer-startime)*1000,3) & "<br/>Read Template/RecordSet Count:" & sqlcount
%>
[/quote]