数据库一个字段姓名
A
B
C
D
E
F
G
H
想查询为
姓名1 姓名2 姓名3
A      B     C
D      E     F
G      H问:SQL.TEXT 如何写
 

解决方案 »

  1.   

    一句sql搞不定的
    要用存储过程循环
      

  2.   

    前提是还有一个id列(连续,无缺),如果没有,可以导入一个带id列的临时表select a.姓名 姓名1
    ,b.姓名 姓名2
    ,c.姓名 姓名3
    from (select id/3 x,姓名 from t where id mod 3=1) a
    left join (select id/3 x,姓名 from t where id mod 3=2) b on a.x=b.x
    left join (select id/3 x,姓名 from t where id mod 3=0) c on a.x=c.x
    order by a.x
    ——整除、取模 的操作符可能不一定对
      

  3.   

    LZ是出报表还是显示在GRID中?
    1.出报表的话基本上任何一个报表工具都有CROSS-TABLE,不用硬写SQL
    2.如果显示在GRID中,数据库是什么?SQL2005明显要比SQL2000比较简单一些
      

  4.   

    sz_haitao 答案正确 稍加修改为:   ADOQuery1.Close;
       ADOQuery1.SQL.Text :=
    ' select a.姓名 as 姓名1,b.姓名 as 姓名2,c.姓名 as 姓名3  '+
    '      from (select (id-1) / 3 x, 姓名 from cj where (id % 3)=1) a  '+
    ' left join (select (id-1) / 3 x, 姓名 from cj where (id % 3)=2) b on a.x=b.x '+
    ' left join (select (id-1) / 3 x, 姓名 from cj where (id % 3)=0) c on a.x=c.x '+
    ' order by a.x ';
       ADOQuery1.Open;结帖!