create table tb(Id int, proname varchar(32), publishdate datetime,city varchar(50), boss varchar(32)) 
insert tb select 1, 'x1', '2007-10-11','北京','boss1' 
union all select 2, 'x2', '2007-10-11','北京', 'boss1' 
union all select 3, 'x3', '2007-10-11', '北京','boss1'   
union all select 4, 'y1', '2007-10-11', '上海','boss2' 
union all select 5, 'y2', '2007-10-11', '上海','boss2' 
union all select 6, 'y3', '2007-10-11', '上海','boss2' 
union all select 7, 'z1', '2007-10-11','长沙', 'boss3' 
union all select 8, 'z2', '2007-10-11', '长沙','boss3' 
union all select 9, 'z3', '2007-10-11', '长沙','boss3'
union all select 10, 'n1', '2007-10-11','北京', 'boss4' 
union all select 11, 'n2', '2007-10-11', '北京','boss4' 
union all select 12, 'n3', '2007-10-11', '北京','boss4'
union all select 13, 'y1', '2007-10-11', '长沙','boss5'现在要得出这样的排序结果
id proname publishdate city  boss
1, 'x1', '2007-10-11','北京','boss1' 
10,'n1', '2007-10-11','北京','boss4' 
2, 'x2', '2007-10-11','北京','boss1'
11,'n2', '2007-10-11','北京','boss4'
3, 'x3', '2007-10-11', '北京','boss1'
12, 'n3', '2007-10-11','北京','boss4'
4, 'y1', '2007-10-11', '上海','boss2' 
7, 'z1', '2007-10-11','长沙', 'boss3'
13, 'y1', '2007-10-11', '长沙','boss5'
5, 'y2', '2007-10-11', '上海','boss2'
8, 'z2', '2007-10-11', '长沙','boss3'
6, 'y3', '2007-10-11', '上海','boss2'
9, 'z3', '2007-10-11', '长沙','boss3'   
就是我指定城市字段的记录要优先排序,但要求boss值之间交叉不能连续,除非没其它不同值,后面其它城市就按先城市值交叉,后boss值交叉显示不知道能实现不,请指教!

解决方案 »

  1.   

    就是我指定城市字段的记录要优先排序
    --------------------------
    你的结果是不是指定'北京'了,这样北京就在顶部了,同时boss 不连续
    剩下的记录只要boss交替就行了
      

  2.   

    LZ的需求有点苛刻,要足够智能啊,在SQL中直接实现怕是不易,
    把数据取出来处理一下应该可以实现
      

  3.   

    create table tb(Id int, proname varchar(32), publishdate datetime,city varchar(50), boss varchar(32)) 
    insert tb select 1, 'x1', '2007-10-11','北京','boss1' 
    union all select 2, 'x2', '2007-10-11','北京', 'boss1' 
    union all select 3, 'x3', '2007-10-11', '北京','boss1' 
    union all select 4, 'y1', '2007-10-11', '上海','boss2' 
    union all select 5, 'y2', '2007-10-11', '上海','boss2' 
    union all select 6, 'y3', '2007-10-11', '上海','boss2' 
    union all select 7, 'z1', '2007-10-11','长沙', 'boss3' 
    union all select 8, 'z2', '2007-10-11', '长沙','boss3' 
    union all select 9, 'z3', '2007-10-11', '长沙','boss3' 
    union all select 10, 'n1', '2007-10-11','北京', 'boss4' 
    union all select 11, 'n2', '2007-10-11', '北京','boss4' 
    union all select 12, 'n3', '2007-10-11', '北京','boss4' 
    union all select 13, 'y1', '2007-10-11', '长沙','boss5' 
    goselect * from tb 
    order by case city when '北京' then 1 else 2 end ,
             cast(substring(proname,2,len(proname)) as int),
             left(proname,1)
    drop table tb
    /*
    Id          proname                          publishdate             city                                               boss
    ----------- -------------------------------- ----------------------- -------------------------------------------------- --------------------------------
    10          n1                               2007-10-11 00:00:00.000 北京                                                 boss4
    1           x1                               2007-10-11 00:00:00.000 北京                                                 boss1
    11          n2                               2007-10-11 00:00:00.000 北京                                                 boss4
    2           x2                               2007-10-11 00:00:00.000 北京                                                 boss1
    12          n3                               2007-10-11 00:00:00.000 北京                                                 boss4
    3           x3                               2007-10-11 00:00:00.000 北京                                                 boss1
    4           y1                               2007-10-11 00:00:00.000 上海                                                 boss2
    13          y1                               2007-10-11 00:00:00.000 长沙                                                 boss5
    7           z1                               2007-10-11 00:00:00.000 长沙                                                 boss3
    5           y2                               2007-10-11 00:00:00.000 上海                                                 boss2
    8           z2                               2007-10-11 00:00:00.000 长沙                                                 boss3
    6           y3                               2007-10-11 00:00:00.000 上海                                                 boss2
    9           z3                               2007-10-11 00:00:00.000 长沙                                                 boss3(13 行受影响)
    */
      

  4.   

    龟,我的前提 是proname有规则 ,最后一位能让你排序,如果没有规则 白搭
      

  5.   

    汗一个,打错
    龟,你的前提 是proname有规则 ,最后一位能让你排序,如果没有规则 白搭
      

  6.   

    这帖我看只能从proname着手,其他字段没戏.
      

  7.   

    就是指定城市字段值(如北京)的记录要优先排序,但要求boss值之间交替不能连续,除非没其它不同值,后面其它城市就按先城市值交替,后boss值交替显示 
      

  8.   

    是个麻烦事啊,对了产品名肯定是不规则的,不会像x1,x2,x3这样子
      

  9.   

    还有一个更BT的要求,呵呵
    就是假如我指定要求北京的优先显示,如果出现北京只有一个boss的话,就是先必须是北京的BOSS排在第一条,再其它城市BOSS与北京BOss这间交替!我自己都晕了总的来说就是:指定城市如BOSS字段有多个值,就先指定城市BOSS之间交替,再其它城市交替,BOSS交替;如果指定城市BOSS字段只有一个值的话,就指定城市与其它城市交替(第一条记录是指定城市的),BOSS交替
      

  10.   

    你把你的要求用手工排排,看能出结果吗?如果理论都通不过,SQL就没法写了.
      

  11.   

    楼主: 如果老龟在12楼说的中的了,你可以试着手工调整各个记录的ID,然后按ID排序,保证又快又好.
    :)
      

  12.   

    当然;如果ID已经被使用了,且相关其他数据的调整是比较麻烦的事,可以在表中加个orderID的字段来存储所需的排序序号。这样直接指明排序序号只要在文档中说明清楚了,恐怕比用复杂的表达式表示要清晰友好、易于维护。
      

  13.   

    龟,你的前提 是proname有规则 ,最后一位能让你排序,如果没有规则 白搭...貌似楼主没说清楚规则....
    如果理论上都没有排序的办法  那SQL恐怕更无能为力