数据集如下:
序号     姓名               发药窗口
1 '李玉堂      ' '发药01'
2 '魏先正      ' '发药01'
3 '王立纯      ' '发药02'
4 '郭青青      ' '发药01'
5 '张菁华      ' '发药02'
6 '付开明      ' '发药02'
改写成这种数据集:
序号      姓名              发药窗口1         姓名             发药窗口2
1 '李玉堂      ' '发药01'          '王立纯      ' '发药02'
2 '魏先正      ' '发药01'          '付开明      ' '发药02'
3 '郭青青      ' '发药01'        '张菁华      ' '发药02'

解决方案 »

  1.   

    select identity(int,1,1) as sn,姓名,发药窗口 into #t1 from table1 where 发药窗口='发药01';
    select identity(int,1,1) as sn,姓名,发药窗口 into #t2 from table1 where 发药窗口='发药02';
    select isnull(#t1.sn,#t2.sn) as 序号,#t1.姓名,#t1.发药窗口 as 发药窗口1,#t2.姓名,#t2.发药窗口 as 发药窗口2
    from #t1 full join #t2
    on #t1.sn=#t2.sn
      

  2.   

    declare @a table(序号  int,姓名 varchar(10), 发药窗口 varchar(10))
    insert @a select 1,'李玉堂 ','发药01'
    union all select 2,'魏先正 ','发药01'
    union all select 3,'王立纯 ','发药02'
    union all select 4,'郭青青 ','发药01'
    union all select 5,'张菁华 ','发药02'
    union all select 6,'付开明 ','发药02'select  aa.id 序号,aa.姓名 姓名1,aa.发药窗口 发药窗口1,bb.姓名 姓名2,bb.发药窗口 发药窗口2 from
    (select top 100 percent id=(select count(1) from @a where 发药窗口='发药01' and 序号<=a.序号),姓名,发药窗口 from @a a where 发药窗口='发药01' order by 序号) aa
    Inner Join
    (select  top 100 percent  id=(select count(1) from @a where 发药窗口='发药02' and 序号<=a.序号),姓名,发药窗口 from @a a where 发药窗口='发药02' order by 序号) bb
    On aa.id=bb.id
      

  3.   

    sdhylj(青锋-SS)(献血有利(别人的)健康) 是对的
    chuifengde(树上的鸟儿) :如果 发药01和发药02 的人数不等,结果就不对
      

  4.   

    第一种情况已经由sdhylj(青锋-SS)(献血有利(别人的)健康) 解决了
    非常感谢...等第二问题解决了一起给你加分..谢谢
    还有第二种情况:数据集如下:
    序号     姓名        发药窗口
    1      '李玉堂      ''发药01'
    2      '魏先正      ''发药01'
    3      '王立纯      ''发药01'
    4      '郭青青      ''发药01'
    5      '张菁华      ''发药01'
    6      '付开明      ''发药01'
    7      '测试一      ''发药01'
    8      '测试二      ''发药01'
    9      '测试三      ''发药01'
    改写成这种数据集1:
    或者是1  2  3
          4  5  6
          7  8  9
    的显示模式也行..
    再次谢谢各位了..
    序号      姓名       发药窗口1   序号  姓名       发药窗口   序号   姓名    发药窗口
    1      '李玉堂      ''发药01'    4      '郭青青   ''发药01' 7     '测试一 ''发药01'
    2      '魏先正      ''发药01'    5      '张菁华   ''发药01' 8     '测试二 ''发药01'
    3      '王立纯      ''发药01'    6      '付开明   ''发药01' 9     '测试三 ''发药01'
      

  5.   

    create table tb(序号  int,姓名 varchar(10), 发药窗口 varchar(10))
    insert tb select 1,'李玉堂 ','发药01'
    union all select 2,'魏先正 ','发药01'
    union all select 3,'王立纯 ','发药01'
    union all select 4,'郭青青 ','发药01'
    union all select 5,'张菁华 ','发药01'
    union all select 6,'付开明 ','发药01'
    union all select 7,'测试一 ','发药01'
    union all select 8,'测试二 ','发药01'
    union all select 9,'测试三 ','发药01'select top 3 t.序号,t.姓名,t.发药窗口,t1.序号,t1.姓名,t1.发药窗口,t2.序号,t2.姓名,t2.发药窗口 
    from tb t inner join tb t1 on t1.序号=t.序号+3 inner join tb t2 on t2.序号=t1.序号+3drop table tb
    /*
    1 李玉堂  发药01 4 郭青青  发药01 7 测试一  发药01
    2 魏先正  发药01 5 张菁华  发药01 8 测试二  发药01
    3 王立纯  发药01 6 付开明  发药01 9 测试三  发药01
    */
      

  6.   

    不用加top 3
    select t.序号,t.姓名,t.发药窗口,t1.序号,t1.姓名,t1.发药窗口,t2.序号,t2.姓名,t2.发药窗口 
    from tb t inner join tb t1 on t1.序号=t.序号+3 inner join tb t2 on t2.序号=t1.序号+3
      

  7.   

    但愿这位兄弟还在线,好用的话不忘了谢一句,我居然想了5分钟,谢谢你。
    --step1
    create table test (iSN int null,cName char(10) null, cWindow char(10) null)insert test values(1,'李玉堂      ','发药01')
    insert test values(2,'魏先正      ','发药01')
    insert test values(3,'王立纯      ','发药02')
    insert test values(4,'郭青青      ','发药01')
    insert test values(5,'张菁华      ','发药02')
    insert test values(6,'付开明      ','发药02')
    --step2 
    select t1.iSN ,t1.cName ,t1.cWindow,t2.cName,t2.cWindow from 
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药01') as iSN,cName,cWindow from test b where b.cWindow = '发药01') t1
    ,
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药02') as iSN,cName,cWindow from test b where b.cWindow = '发药02') t2
    where t1.iSN = t2.iSNunion select iSN,cName,cWindow,'','' from 
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药01') as iSN,cName,cWindow from test b where b.cWindow = '发药01') t1
    where iSN not in ( select t2.iSN from 
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药02') as iSN,cName,cWindow from test b where b.cWindow = '发药02') t2
    )union
    select iSN,'','',cName,cWindow from 
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药02') as iSN,cName,cWindow from test b where b.cWindow = '发药02') t1
    where iSN not in (select t2.iSN from 
    ( select (select count(*) from test a where a.iSN <= b.iSN and a.cWindow = '发药01') as iSN,cName,cWindow from test b where b.cWindow = '发药01') t2
    )
    order by 1
    --step3
    delete from test where iSN=6
    --step4 redo step2
    --step5 
    insert test values(6,'付开明      ','发药02')
    delete from test where iSN=1
    --step6 redo step2
      

  8.   

    也谢谢suifix...我的问题已经解决了...
    你的太长了。。我没有试
    不过也谢谢你 用的是sdhylj(青锋-SS)(献血有利(别人的)健康)、bill024(捞猴子的月亮) 的方法谢谢你们了接分