关于一张表查询

Amy_2008 2008-08-29 03:36:41
一张表包含里有 款号、工种

查询出所有该表款号、工种不能重复,并增加一列自动编号
...全文
97 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2008-08-29
  • 打赏
  • 举报
回复
题意不明确
等不到来世 2008-08-29
  • 打赏
  • 举报
回复

select distinct 款号,工种,自动编号=row_number() over(order by 款号,工种) from tb
zoujp_xyz 2008-08-29
  • 打赏
  • 举报
回复
select identity(int,1,1) as ID,a.* into #t from (select distinct 款号,工种 from tb) a

select * from #t
dawugui 2008-08-29
  • 打赏
  • 举报
回复
--使用临时表完成
select id = identity(int ,1,1) , distinct 款号,工种 into tmp from tb
select * from tmp

--直接查询
select n.* , id = (select count(1) from
(
select distinct 款号,工种 from tb
) m where 款号 < n.款号 or (款号 = n.款号 and 工种 < n.工种)) + 1
from
(
select distinct 款号,工种 from tb
) n
wgzaaa 2008-08-29
  • 打赏
  • 举报
回复
意思不明确
dawugui 2008-08-29
  • 打赏
  • 举报
回复
--使用临时表完成
select id = identity(int ,1,1) , distinct 款号,工种 into tmp from tb
select * from tmp

--直接查询
select n.* , id = (select count(1) from
(
select distinct 款号,工种 into tmp from tb
) m where 款号 < n.款号 or (款号 = n.款号 and 工种 < n.工种)) + 1
from
(
select distinct 款号,工种 into tmp from tb
) n
水族杰纶 2008-08-29
  • 打赏
  • 举报
回复
select distinct 款号,工种 ,identity(int,1,1)ID into Newtable from oldtable
子陌红尘 2008-08-29
  • 打赏
  • 举报
回复

select identity(int,1,1) as ID,a.* into # from (select distinct 款号,工种 from tb) a

select * from #
dawugui 2008-08-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 Amy_2008 的帖子:]
一张表包含里有 款号、工种

查询出所有该表款号、工种不能重复,并增加一列自动编号
[/Quote]

select id = identity(int ,1,1) , distinct 款号,工种 into tmp from tb
select * from tmp
lgxyz 2008-08-29
  • 打赏
  • 举报
回复
多给些其他字段比较主键是什么?
jacklinchen 2008-08-29
  • 打赏
  • 举报
回复
不重复: select distinct 款号、工种 from tb

34,593

社区成员

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

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