我想问一下刚才这个帖子
http://topic.csdn.net/u/20080123/12/53988160-2bfa-4de3-affa-ac9f73578d5e.htmlwhere cnt < n.cnt
这里判断的条件,其中cnt代表的字段一定要用数字么?用汉字的话可以么?比如:where 省份名称 < n.省份名称
这样查出来的结果,px整个一列都为1了,可是sql集查询出来没有数字列,该怎么办呢?

解决方案 »

  1.   

    select row_number()over(order by 列),* from t--2005用
      

  2.   

    select px=identity(int,1,1) ,a.type,b.type ,count(*) cnt into tmp FROM a ,b where b.r_id=a.r_id GROUP BY a.type,b.type
      select * from tmp
      

  3.   

    --这样生成自增
    with t(col)
    as
    (select 1 
    union all
    select col+1 from t where col+1<100)----2000只有用记录数、必须有一列是有大小关系、没有时用唯一列判断生成的递增不一值。。通常用临时表
      

  4.   

    declare @tb table (v varchar(80))
    insert into @tb select '河北'
    insert into @tb select '河南'
    insert into @tb select '河西'
    insert into @tb select '河东'
    select id=row_number() over (order by v),v from @tbid v
    1 河北
    2 河东
    3 河南
    4 河西
      

  5.   

    with   t(col) --2005用法
      

  6.   

    declare @tb table (v varchar(80))
    insert into @tb select '河北'
    insert into @tb select '河南'
    insert into @tb select '河西'
    insert into @tb select '河东'
    select Pid = (select count(1) + 1 from @tb where v <a.v),*
    from @tb a
    order by pId/*Pid         v                                                                                
    ----------- -------------------------------------------------------------------------------- 
    1           河北
    2           河东
    3           河南
    4           河西(所影响的行数为 4 行)
    */
      

  7.   

    不好意思刚才不在,我的sql是这样的:
    select 序列 = (select count(1)  from 
    (
      SELECT t.RES_DESCR 省份名称
    FROM v_res_netdevice_desc t ,res_def_class d
    ) m where 省份名称 < n.省份名称) + 1,* from
    (
     SELECT t.RES_DESCR 省份名称
    FROM v_res_netdevice_desc t ,res_def_class d 
    ) n
      

  8.   

    省份名称   <   n.省份名称

    省份名称   =  n.省份名称 and id<n.id找个标识字段替换了id
      

  9.   

    select 序列 = (select count(1)  from 
    (
      SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
    t.appsystem as 业务系统, t.location 地理位置 
    FROM v_res_netdevice_desc t ,res_def_class d
    ) m where id < n.id) + 1,* from
    (
     SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
    t.appsystem as 业务系统, t.location 地理位置
    FROM v_res_netdevice_desc t ,res_def_class d 
    ) n
    这么写的话,说列名id无效,该怎么写呢?
      

  10.   

    declare @tb table (v varchar(80))
    insert into @tb select '河北'
    insert into @tb select '河南'
    insert into @tb select '河西'
    insert into @tb select '河东'
    select Pid = (select count(1) + 1 from (select * from @tb) b where v <a.v),*
    from (select * from @tb) a
    order by pId/*Pid         v                                                                                
    ----------- -------------------------------------------------------------------------------- 
    1           河北
    2           河东
    3           河南
    4           河西(所影响的行数为 4 行)
    */
      

  11.   

    老大,直接row_number()不就行了吗?
    要不就写存储过程用临时表,这样写不行
      

  12.   

    如果2000,你要找出你需要的排序组合,比如这个组合是(a,b),这时你应该判断这个组合(a,b)是否唯一,如果不唯一,需要加哪些字段才能唯一,比如组合(a,b,c,d)是唯一的,那语句就可以这么写:select *,         --可以把*替换成你需要的字段列表
    (select count(1) from tab
      where a<x.a or a=x.a and b<x.b or a=x.a and b=x.b and c<x.c or a=x.a and b=x.b and c=x.c and d<=x.d
    ) as 序号
    from tab x
    order by a,b,c,d
      

  13.   

    不想用临时表啊,是2000的库不能用row_number()
      

  14.   

    happyflystone 
    无枪狙击手 
    等 级:
     发表于:2008-01-23 14:45:2318楼 得分:0 
    ID在你的列表中没有呀 
     --------------
    是没有呀,没有的话,这里的where条件要怎么写呢?多谢帮忙啊!
      

  15.   

    第二个方法,你不必找唯一字段组合,直接
    select *,
    IDENTITY(int,1,1) as 序号
    into #t
    from tab
    order by a,b                           --注意a,b可能不唯一select * from #tdrop table #t
      

  16.   

    select 序列 = (select count(1)  from 
                (
                  SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
                t.appsystem as 业务系统, t.location 地理位置 
                FROM v_res_netdevice_desc t ,res_def_class d
                ) m where 地理位置 < n.地理位置) + 1,* 
    from
                (
                 SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
                t.appsystem as 业务系统, t.location 地理位置
                FROM v_res_netdevice_desc t ,res_def_class d 
                ) n你这里面没有  “省份名称”
      

  17.   

    --加个条件:
    where 省份名称 < n.省份名称 or (省份名称 < n.省份名称 and cnt < n.cnt)
      

  18.   

    --错了.加个条件:
    where 省份名称 < n.省份名称 or (省份名称 = n.省份名称 and cnt < n.cnt)
      

  19.   

    dawugui 
    潇洒老乌龟(原名:爱新觉罗.毓华) 
    等 级:
     发表于:2008-01-23 14:55:5226楼 得分:0 
    SQL code--错了.加个条件:
    where 省份名称 < n.省份名称 or (省份名称 = n.省份名称 and cnt < n.cnt)
    -----------------
     现在没有 cnt 这个字段了,改怎么写呢?
      

  20.   

    happyflystone 
    无枪狙击手 
    等 级:
     发表于:2008-01-23 14:55:0324楼 得分:0 
    SQL codeselect 序列 = (select count(1)  from 
                (
                  SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
                t.appsystem as 业务系统, t.location 地理位置 
                FROM v_res_netdevice_desc t ,res_def_class d
                ) m where 地理位置 < n.地理位置) + 1,* 
    from
                (
                 SELECT t.RES_DESCR 设备名称,t.model as 设备型号,d.class_descr as 设备类型,t.vendor as 生产厂商, t.integrator as 集成商,t.supplier as 供应商, 
                t.appsystem as 业务系统, t.location 地理位置
                FROM v_res_netdevice_desc t ,res_def_class d 
                ) n
    你这里面没有  “省份名称” 
     --------------
    哦,错了,是设备名称
      

  21.   

    现在没有   cnt   这个字段了,改怎么写呢?--
    只要能通过某个(或某多个字段的组合)能区分大小即可.如果没有,则只能插入临时表(或使用sql 2005 row_number).具体方法(某个的我不写了,如cnt < n.cnt)
    多个的:con1 < n.con1 or (con1 = n.con1 and con2 < n.con2) or (con1 = n.con1 and con2 = n.con2 and con3 < n.con3)
      

  22.   

    不能建临时表,这几个字段也都不是数字的,也不能安装SqlServer2005数据库,可不可以前面加一个空列再根据行数添上数字呢?
      

  23.   

    declare @tb table (v varchar(80))
    insert into @tb select '河北'
    insert into @tb select '河南'
    insert into @tb select '河西'
    insert into @tb select '河东'
    select Pid = (select count(1) + 1 from (select * from @tb) b where v <a.v),*
    from (select * from @tb) a
    order by pId/*Pid         v                                                                                
    ----------- -------------------------------------------------------------------------------- 
    1           河北
    2           河东
    3           河南
    4           河西(所影响的行数为 4 行)
    */
    ------------------
    这段代码是什么意思啊?这个不是按汉字的字段排出来了么?