order by case when id>50 then id-50 else id end

解决方案 »

  1.   

    ... order by (case when id%50 = 0 then 50 else id%50 end),id
      

  2.   

    邹建的更直观
    order by (case when id>50 then id-50 else id end),id
      

  3.   

    对不起,对不起大家,我没有说清楚,我所说的ID并不是1-100,我的意思是根据ID排序后的记录号顺序是1-50,ID是我首先要排序的记录字段,它是一个字符型,长度为8,而且里面还有数据和字母。
      

  4.   

    SQL中只有排序,没有记录号。你到底想要怎样的结果,最好能列出示例数据和想要的结果来。
      

  5.   

    是不是这个意思select a.* from 
    (select (select count(*) from t where 排序字段<=a.排序字段) as 序号,* from t a) aorder by order by case when a.序号>50 then a.序号-50 else id end
      

  6.   

    select a.* from 
    (select (select count(*) from t where 排序字段<=a.排序字段) as 序号,* from t a) aorder by order by case when a.序号>50 then a.序号-50 else a.序号 end
      

  7.   

    有错误  呵呵
    select a.* from 
    (select (select count(*) from t where 排序字段<=a.排序字段) as 序号,* from t a) aorder by case when a.序号>50 then a.序号-50 else a.序号 end
      

  8.   

    是这样的,是想打印学生的数据,ID代表学生的学号,我打印的时候想一张纸只打印两名学生的信息,即实现所谓的“切纸打印”,假设有一百条数据,第一张纸的左边打印第一条,右边打印第51条记录;第二张纸的左边打印第二条记录,右边打印第52条记录,以此类推,等我打印完成后,只要将这50张纸切为两半,将左半放在右右半部分的上面,那么合在一起的就全部是按ID顺序的打印结果了。
    因为在报表 中我没有办法实现这种效果,只好求助于打开记录的时候排序有没有办法了??
      

  9.   

    --如果id 是主键,按id顺序决定记录号,则可以用:select * from 表 a
    order by case when (select count(*) from 表 where id<=a.id)>50 then (select count(*) from 表 where id<=a.id)-50 else (select count(*) from 表 where id<=a.id) end
      

  10.   

    借用邹建的:declare @a int
    select @a = ceiling(count(*) * 1.0/2) from 表select * from 表 a
    order by case when (select count(*) from 表 where id<=a.id)>@a then (select count(*) from 表 where id<=a.id)-@a else (select count(*) from 表 where id<=a.id) end或:
    select * from 表 a
    order by (case when (select count(*) from 表 where id<=a.id)>(select ceiling(count(*) * 1.0/2) from 表) 
       then (select count(*) from 表 where id<=a.id)-(select ceiling(count(*) * 1.0/2) from 表) else (select count(*) from 表 where id<=a.id) end)好象有点乱,结果应该差不多。
      

  11.   

    是不是这个意思,先按照id(id既可以为数字也可以为字母)排序,再按照1……n排序?如果是这样的话,可能不好实现。
      

  12.   

    是不是这个意思,先按照id(id既可以为数字也可以为字母)排序,再按照另一字段1……n排序?如果是这样的话,可能不好实现。
      

  13.   

    我看楼主别折腾了,不就是分列打印吗?还是从报表里做吧,access报表和vfp报表都有这个功能的(当然还有其他的软件),很简单的.
      

  14.   

    --测试数据
    declare @t table(id varchar(10))
    insert @t select '004'
    union all select '003'
    union all select '005'
    union all select '008'
    union all select '007'
    union all select '009'
    union all select '001'
    union all select '006'
    union all select '002'
    union all select '000'
    union all select '011'--查询
    select id from @t a
    order by case 
    when (select count(*) from @t where id<=a.id)>6 
    then (select count(*) from @t where id<=a.id)-6
    else (select count(*) from @t where id<=a.id) end,id/*--测试结果id         
    ---------- 
    000
    006
    001
    007
    002
    008
    003
    009
    004
    011
    005(所影响的行数为 11 行)
    --*/
      

  15.   

    SELECT * FROM tA
    ORDER BY (Id % 6), Id
      

  16.   

    SELECT @COUNT:=CEILING(COUNT(KSBH)/2) FROM net_sbm;
    SELECT @Id:=-1;
    SELECT KSBH, IMG FROM
    (
      SELECT @Id := @Id + 1 Id, @Id%@COUNT, KSBH, IMG FROM net_sbm ORDER BY KSBH
    ) tA;
      

  17.   

    SELECT @COUNT:=CEILING(COUNT(KSBH)/2) FROM net_sbm;
    SELECT @Id:=-1;
    SELECT KSBH, IMG FROM
    (
      SELECT @Id := @Id + 1 Id, @Id%@COUNT Index_2, KSBH, IMG FROM net_sbm ORDER BY KSBH
    ) tA
    ORDER BY Index_2, Id;呵呵,应该是这个.
      

  18.   

    zjcxc(邹建)::您的方法在记录增加的时候就不适合了,因为我举的例子只是11条记录;
     shuixin13(犬犬(心帆)) ::您的方法完全正确,但现在还没有办法将其应用到VB中去执行,还要加工。
    待这个问题彻底解决后,本人另开100分贴,到时短消息请你们来领分,绝不食言。
      

  19.   

    --处了,给你写成动态的,你自己修改一下表名就行了,其他的不用改--测试数据
    declare @t table(id varchar(10))
    insert @t select '004'
    union all select '003'
    union all select '005'
    union all select '008'
    union all select '007'
    union all select '009'
    union all select '001'
    union all select '006'
    --union all select '002'
    --union all select '000'
    --union all select '011'--查询
    select a.* from @t a,(select cnt=(count(*)+1)/2 from @t)b
    order by case 
    when (select count(*) from @t where id<=a.id)>b.cnt
    then (select count(*) from @t where id<=a.id)-b.cnt
    else (select count(*) from @t where id<=a.id) end,id/*--测试结果id         
    ---------- 
    001
    006
    003
    007
    004
    008
    005
    009(所影响的行数为 8 行)
    --*/
      

  20.   

    shuixin13(犬犬(心帆)) 的那个在SQL Server数据库中不行吧?
      

  21.   

    zjcxc(邹建)::shuixin13(犬犬(心帆)) 的语句在正确的,我指的是在MySQL语句中能正确通过。
      

  22.   

    zjcxc(邹建)::
    我正在测试您的数据。
      

  23.   

    zjcxc(邹建)::
    我正在测试您的语句。
      

  24.   

    SELECT
    (@Id := @Id + 1) Id,
    @Id%@COUNT Index_2,
    KSBH
    FROM net_sbm,
     (SELECT @Id := -1) tA,
     (SELECT @COUNT:=CEILING(COUNT(KSBH)/2) FROM net_sbm) tB
    ORDER BY KSBH嗯.由于他用的 ODBC 无法支持执行多条语句.
    所以修改成上面的语句应该就可以了.
      

  25.   

    结帐了,请邹建和心帆到以下贴子领分.http://community.csdn.net/Expert/topic/3575/3575437.xml?temp=.8825647