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
  1. Private Sub Command3_Click()
  2.     Dim rs As New ADODB.Recordset
  3.     Dim strArray() As Long
  4.     Dim strSQL As String
  5.     Dim i As Long, j As Long
  6.     Dim sSQL As String
  7.     Dim strValues As String
  8.     strSQL = "select 编号 from 表1 where 编号 mod 3=0 order by 编号"
  9.     With rs
  10.         .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
  11.         i = .RecordCount - 1
  12.         ReDim strArray(i) As Long
  13.         For i = 0 To .RecordCount - 1
  14.             strArray(i) = .Fields(0)
  15.             .MoveNext
  16.         Next
  17.         .Close
  18.         For i = 0 To UBound(strArray)
  19.             strSQL = "select top 3 * from 表1 where 编号<=" & strArray(i) & " order by 编号 desc"
  20.             .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
  21.             .MoveLast
  22.             Do While Not .BOF
  23.                 strValues = strValues & "'" & .Fields(1) & "',"
  24.                 .MovePrevious
  25.             Loop
  26.             If strValues <> "" Then
  27.                 strValues = Left(strValues, Len(strValues) - 1)
  28.             End If
  29.             j = j + 1
  30.             sSQL = "insert into a(编号,字段1,字段2,字段3)values(" & j & "," & strValues & ")"
  31.             CurrentDb.Execute sSQL
  32.             strValues = ""
  33.             .Close
  34.         Next
  35.     End With
  36.     Set rs = Nothing
  37.     Me.Child0.Requery
  38. 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