SELECT IDENTITY(int, 1,1) AS xh,clid ,  vehid,    cardid,   name   ,  thid into #aa FROM table order by clid,     vehid    ,cardid,   name ,    thid
select * from #aa order by xh

解决方案 »

  1.   

    数据是动态的,不能用IDENTITY(int, 1,1)之类。
      

  2.   

    SELECT (
    select count(*) from tablename 
    where clid<a.clid 
    or (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and cardid<a.cardid)
    or (clid=a.clid  and vehid=a.vehid and cardid=a.cardid and name<a.name)
    or (clid=a.clid  and vehid=a.vehid and cardid=a.cardid and name=a.name and thid<a.thid)
    ) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid如果对同一个clid,vehid组合,保证cardid,name相同,可以写成:
    SELECT (
    select count(*) from tablename 
    where clid<a.clid 
    or (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and thid<a.thid)
    ) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid
      

  3.   

    有点问题,修正一下:SELECT 1+isnull((
    select count(*) from tablename 
    where clid<a.clid 
    or (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and thid<a.thid)
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid
      

  4.   

    不好意思,我的问题说有错误,应是这样的:
    clid     vehid    cardid   name     thid10946 10947 青A12936 陈道明 1
    10946 10947 青A12936 陈道明 4
    10946 10948 青A12930 赵若兵 1
    10946 10948 青A12930 赵若兵 4
    10947 10948 青A12930 赵若兵 4
    10947 10948 青A12930 赵若兵 1
    10947 10946 青A12923 饶家洪 1
    10947 10946 青A12923 饶家洪 4
    10947 10932 青A12907 林师宏 1 要求得到:
    xh   clid     vehid    cardid   name     thid
    1 10946 10947 青A12936 陈道明 1
    1 10946 10947 青A12936 陈道明 4
    2 10946 10948 青A12930 赵若兵 1
    2 10946 10948 青A12930 赵若兵 4
    1 10947 10932 青A12907 林师宏 1
    2 10947 10946 青A12923 饶家洪 1
    2 10947 10946 青A12923 饶家洪 4
    3 10947 10948 青A12930 赵若兵 4
    3 10947 10948 青A12930 赵若兵 1

    xh   clid     vehid    cardid   name     thid
    1 10946 10947 青A12936 陈道明 1
    2 10946 10947 青A12936 陈道明 4
    3 10946 10948 青A12930 赵若兵 1
    4 10946 10948 青A12930 赵若兵 4
    1 10947 10932 青A12907 林师宏 1
    2 10947 10946 青A12923 饶家洪 1
    3 10947 10946 青A12923 饶家洪 4
    4 10947 10948 青A12930 赵若兵 4
    5 10947 10948 青A12930 赵若兵 1
      

  5.   

    SELECT IDENTITY(int, 1,1) AS xh,clid ,vehid,cardid,name, thid FROM table
      

  6.   

    1、先把clid相同的一组,不同重新开始
    2、vehid 不同的一组,从1.2.3排下来
      

  7.   

    第二种:SELECT 1+isnull((
    select count(*) from tablename 
    where (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and thid<a.thid)
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid
    第一种不好写!
      

  8.   

    比如clid相同,而vehid相同,则xh=1,若vehid不相同则xh=2,依此排下来.
      

  9.   

    SELECT 1+isnull((
    select count(distinct vehid) from tablename 
    where (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and thid<a.thid)
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid
      

  10.   

    修正:
    SELECT 1+isnull((
    select count(distinct vehid) from tablename 
    where clid=a.clid  and vehid<a.vehid
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  thid into FROM tablename a
    order by clid,     vehid    ,cardid,   name ,    thid
      

  11.   

    CCEO() 你的第二种方法是正确的。
      

  12.   

    zqllyh()我前台是用pb的,不过,这不是最终结果,还要連接别的表
    CCEO()的第二种方法是:
    SELECT 1+isnull((
    select count(*) from v_qf_ht_dbtz 
    where (clid=a.clid  and vehid<a.vehid)
    or (clid=a.clid  and vehid=a.vehid and htid<a.htid)
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  htid  FROM v_qf_ht_dbtz a
    order by clid,     vehid    ,cardid,   name ,    htid 
      

  13.   

    cceo()的第一种方法也是对的:
    SELECT 1+isnull((
    select count(distinct vehid) from v_qf_ht_dbtz 
    where clid=a.clid  and vehid<a.vehid
    ),0) as  xh,clid ,  vehid,    cardid,   name   ,  htid  FROM v_qf_ht_dbtz a
    order by clid,     vehid    ,cardid,   name ,    htid
      

  14.   

    先分组,再在数据窗口明细区加一计算列,
    getrow() - first(getrow() for group 1) + 1
      

  15.   

    我的同事的方法(第一种):
    select (select count(1)+1 from (select vehid from v_qf_ht_dbtz where clid=b.clid group by vehid) as a
            where a.vehid<b.vehid) as xh,clid,vehid,cardid,name,htid,qysj
    from v_qf_ht_dbtz b