首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 一个插入编号的问题 [已结贴,结贴人:song116148565]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 12:44:51 楼主
    有表 A 如下:

    name  age
    a      18
    b      20
    c      30
    d      40

    我的查询

    select name,age into ##temp from A where age <40

    select * from ##temp

    ##temp结果----------
    name  age
    a      18
    b      20
    c      30

    但想得到 ##temp 为
    name  age  sort
    a      18  1
    b      20  2
    c      30  3

    怎么写 SQL 带有编号的语句,在线等.
    30  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 12:45:151楼 得分:0
    自己先SF
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 12:48:212楼 得分:3
    这是一个类似的
    SQL code
    declare @A table(type nvarchar(2),point nvarchar(5),price int) insert @A select 'A','aaa',12 insert @A select 'A','bbb',13 insert @A select 'A','ccc',12 insert @A select 'B','ddd',11 insert @A select 'B','eee',10 insert @A select 'C','fff',33 insert @A select 'C','ggg',2 select * , iden = (select count(*) from @a where type=a.type and point<a.point)+1 from @a a
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • viva369
    • 等级:
    发表于:2008-04-03 13:03:583楼 得分:8
    --sql 2000
    select name,age,identity(int,1,1) sort into ##temp from A where age <40

    --sql 2005
    select name,age,row_number() over(order by name) sort into ##temp from A where age <40

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 13:18:364楼 得分:0
    引用 2 楼 Internetroot 的回复:
    这是一个类似的

    SQL codedeclare @A table(type nvarchar(2),point nvarchar(5),price int)
    insert @A select 'A','aaa',12
    insert @A select 'A','bbb',13
    insert @A select 'A','ccc',12
    insert @A select 'B','ddd',11
    insert @A select 'B','eee',10
    insert @A select 'C','fff',33
    insert @A select 'C','ggg',2

    select * , iden = (select count(*) from @a where type=a.type and point <a.point)+1
    f…

    嗯 .谢谢..不过如果 point 有相等的值,iden 也会有相同的了.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • laowan688
    • 等级:
    发表于:2008-04-03 13:28:425楼 得分:4
    declare @table table
    (
    name  nvarchar(20),
    age    int
    )
    insert @table select 'a', '18'
    union  select 'b', '20'
    union  select 'c', '30'
    union  select 'd', '40'

    select * from @table
    declare  @temp_t table
    (
    name  nvarchar(20),
    age    int,
    sort    int  identity(1,1)
    )

    insert  into @temp_t

    select * from @table  where age <40

    select  * from  @temp_t


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • laowan688
    • 等级:
    发表于:2008-04-03 13:29:276楼 得分:0
    注意,  你的临时表,增加这样一列 sort    int  identity(1,1)
    就ok了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ojuju10
    • 等级:
    发表于:2008-04-03 13:37:407楼 得分:3
    SQL code
    --2000 select name,age,sort=(select count(1) from tab where a.age<=age and age<40) into #temp from tab a where age<40 --2005 select name,age,sort=row_number() over (order by age) into #temp from tab a where age<40
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 13:42:138楼 得分:3
    SQL code
    select *,sort=(select count(1) from @table where age<=a.age) from @table a where age<40
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 13:49:009楼 得分:4
    if object_id('A') is not null
      drop table A
    go
    create table A(name varchar(100),age int)
    insert into A
    select 'a',18 union all
    select 'b',20 union all
    select 'c',30 union all
    select 'd',40

    create table #1(name varchar(100),age int,sort int identity(1,1))
    insert into #1
    select name,age from A where age <40
    select * from #1
    drop table #1

    select name,age,sort=identity(int,1,1) into #2 from A where age <40
    select * from #2
    drop table #2

    以上2种做法都可以
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 13:56:2410楼 得分:4
    SQL code
    2005:三种效果 SELECT *, ROW_number()over(order by price) FROM Ta select *, row=dense_rank()over(order by Price) from Ta select *, row=rank()over(order by Price) from Ta
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuda_1985
    • 等级:
    发表于:2008-04-03 13:59:3111楼 得分:1
    引用 6 楼 laowan688 的回复:
    注意,  你的临时表,增加这样一列 sort    int  identity(1,1) 
    就ok了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-03 15:26:0912楼 得分:0
    好.问题解决了..散分
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved