日期:2014-05-18 浏览次数:20558 次
create table tb(ModuleID varchar(3),RoleID varchar(3),ModuleOrderID varchar(3)) insert into tb values('001','001','001') insert into tb values('001','001','002') insert into tb values('001','001','003') insert into tb values('001','001','004') insert into tb values('001','001','005') insert into tb values('001','001','006') insert into tb values('001','001','007') insert into tb values('001','001','008') insert into tb values('001','001','009') insert into tb values('001','001','010') insert into tb values('001','001','011') insert into tb values('001','001','012') insert into tb values('001','001','013') go select ModuleID,RoleID,ModuleOrderID,ModuleOrderID1,ModuleOrderID2 from ( select ModuleID,RoleID,c1, max(case c2 when 0 then ModuleOrderID else '' end) ModuleOrderID, max(case c2 when 1 then ModuleOrderID else '' end) ModuleOrderID1, max(case c2 when 2 then ModuleOrderID else '' end) ModuleOrderID2 from ( select ModuleID,RoleID,ModuleOrderID,(cast(ModuleOrderID as int)-1)/3 c1,(ModuleOrderID-1)%3 c2 from tb ) t group by ModuleID,RoleID,c1 ) m drop table tb /* ModuleID RoleID ModuleOrderID ModuleOrderID1 ModuleOrderID2 -------- ------ ------------- -------------- -------------- 001 001 001 002 003 001 001 004 005 006 001 001 007 008 009 001 001 010 011 012 001 001 013 (所影响的行数为 5 行) */
------解决方案--------------------
declare @T table (ModuleID varchar(3),RoleID varchar(3),ModuleOrderID varchar(3)) insert into @T values('001','001'f