Office中国论坛/Access中国论坛
标题:
求SQL语法~~
[打印本页]
作者:
xingzhihao
时间:
2009-3-3 08:06
标题:
求SQL语法~~
如何通过SQL语句把 左表结构 查询 为右表结构??
老大,指点了~~
编号 字段1 编号 字段1 字段2 字段3
1 a 1 a b c
2 b 2 d e f
3 c 3 g h i
4 d 4 j k l
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
13 1
14 m
例子:[attach]36178[/attach]
作者:
Henry D. Sy
时间:
2009-3-3 08:34
只用sql语句估计不行
作者:
Henry D. Sy
时间:
2009-3-3 09:16
Private Sub Command3_Click()
Dim rs As New ADODB.Recordset
Dim strArray() As Long
Dim strSQL As String
Dim i As Long, j As Long
Dim sSQL As String
Dim strValues As String
strSQL = "select 编号 from 表1 where 编号 mod 3=0 order by 编号"
With rs
.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
i = .RecordCount - 1
ReDim strArray(i) As Long
For i = 0 To .RecordCount - 1
strArray(i) = .Fields(0)
.MoveNext
Next
.Close
For i = 0 To UBound(strArray)
strSQL = "select top 3 * from 表1 where 编号<=" & strArray(i) & " order by 编号 desc"
.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
.MoveLast
Do While Not .BOF
strValues = strValues & "'" & .Fields(1) & "',"
.MovePrevious
Loop
If strValues <> "" Then
strValues = Left(strValues, Len(strValues) - 1)
End If
j = j + 1
sSQL = "insert into a(编号,字段1,字段2,字段3)values(" & j & "," & strValues & ")"
CurrentDb.Execute sSQL
strValues = ""
.Close
Next
End With
Set rs = Nothing
Me.Child0.Requery
End Sub
复制代码
作者:
ACMAIN_CHM
时间:
2009-3-3 09:22
没用你的例子,下载不方便。
自己建表 table1, 两列,
编号 数字
字段1 字符
然后把你的数据贴进去了。
TRANSFORM Min(字段1) AS 字段1OfMin
SELECT rno
FROM (SELECT int((编号-1)/3) AS rno, '字段' & ((编号-1) mod 3 +1) AS cno, 字段1
FROM table1)
GROUP BY rno
PIVOT cno;
得到结果
rno 字段1 字段2 字段3
0 a b c
1 d e f
2 g h i
3 j k l
4 1 m
******************
* 一切皆有可能 *
******************
.
ACMAIN - Access论坛回贴准则(个人)
.
.
同
一
贴
子
不
回
复
第
二
次
.
QQ群 48866293 / 12035577 / 7440532 / 13666209
http://forum.csdn.net/BList/OtherDatabase
.
http://www.accessbbs.cn/bbs/index.php
.
http://www.accessoft.com/bbs/index.asp
.
http://www.access-programmers.co.uk/forums
.
http://www.office-cn.net
.
.
http://www.office-cn.net/home/space.php?uid=141646
.
作者:
xingzhihao
时间:
2009-3-3 14:09
非常感谢 Henry D. Sy 、ACMAIN_CHM 朋友的帮助!
真是高手呀!厉害!!!问题已经解决。
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3