第一列生成序号的问题

wudileiyu 2008-01-23 01:56:47
我想问一下刚才这个帖子
http://topic.csdn.net/u/20080123/12/53988160-2bfa-4de3-affa-ac9f73578d5e.html

where cnt < n.cnt
这里判断的条件,其中cnt代表的字段一定要用数字么?用汉字的话可以么?比如:where 省份名称 < n.省份名称
这样查出来的结果,px整个一列都为1了,可是sql集查询出来没有数字列,该怎么办呢?
...全文
297 34 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
xufei547 2008-01-23
  • 打赏
  • 举报
回复


支持
lingling295 2008-01-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 行)
*/

------------------
这段代码是什么意思啊?这个不是按汉字的字段排出来了么?
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
没有标识列怎么弄?
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
不能建临时表,这几个字段也都不是数字的,也不能安装SqlServer2005数据库,可不可以前面加一个空列再根据行数添上数字呢?
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
楼主说不行,还没字段,诶
dawugui 2008-01-23
  • 打赏
  • 举报
回复
现在没有 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)
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
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



你这里面没有  “省份名称”
--------------
哦,错了,是设备名称
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
dawugui
潇洒老乌龟(原名:爱新觉罗.毓华)
等 级:
发表于:2008-01-23 14:55:5226楼 得分:0
SQL code--错了.加个条件:
where 省份名称 < n.省份名称 or (省份名称 = n.省份名称 and cnt < n.cnt)
-----------------
现在没有 cnt 这个字段了,改怎么写呢?
dawugui 2008-01-23
  • 打赏
  • 举报
回复
--错了.加个条件:
where 省份名称 < n.省份名称 or (省份名称 = n.省份名称 and cnt < n.cnt)
dawugui 2008-01-23
  • 打赏
  • 举报
回复
--加个条件:
where 省份名称 < n.省份名称 or (省份名称 < n.省份名称 and cnt < n.cnt)
-狙击手- 2008-01-23
  • 打赏
  • 举报
回复
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


你这里面没有  “省份名称”
昵称被占用了 2008-01-23
  • 打赏
  • 举报
回复
第二个方法,你不必找唯一字段组合,直接
select *,
IDENTITY(int,1,1) as 序号
into #t
from tab
order by a,b --注意a,b可能不唯一

select * from #t

drop table #t
dawugui 2008-01-23
  • 打赏
  • 举报
回复
来晚了.
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
happyflystone
无枪狙击手
等 级:
发表于:2008-01-23 14:45:2318楼 得分:0
ID在你的列表中没有呀
--------------
是没有呀,没有的话,这里的where条件要怎么写呢?多谢帮忙啊!
wudileiyu 2008-01-23
  • 打赏
  • 举报
回复
不想用临时表啊,是2000的库不能用row_number()
昵称被占用了 2008-01-23
  • 打赏
  • 举报
回复
如果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

-狙击手- 2008-01-23
  • 打赏
  • 举报
回复
ID在你的列表中没有呀
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
3楼飞天小虫
用临时表,最好的方法了
wzy_love_sly 2008-01-23
  • 打赏
  • 举报
回复
老大,直接row_number()不就行了吗?
要不就写存储过程用临时表,这样写不行
-狙击手- 2008-01-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 行)
*/
加载更多回复(14)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧