前提是还有一个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 ——整除、取模 的操作符可能不一定对
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;结帖!
要用存储过程循环
,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
——整除、取模 的操作符可能不一定对
1.出报表的话基本上任何一个报表工具都有CROSS-TABLE,不用硬写SQL
2.如果显示在GRID中,数据库是什么?SQL2005明显要比SQL2000比较简单一些
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;结帖!