一个sql的问题,怎么在查询中增加一个自增列(给出结果立刻结帖)
比如查询语句select stor_id,stor_name,stor_address from pubs..stores的结果中为
stor_id stor_name stor_address
6380 Eric the Read Books 788 Catamaugus Ave.
7066 Barnum 's 567 Pasadena Ave.
7067 News & Brews 577 First St.
7131 Doc-U-Mat: Quality Laundry and Books 24-A Avogadro Way
7896 Fricative Bookshop 89 Madison St.
8042 Bookbeat 679 Carson St.
我想在结果中增加一列,形式为:
id stor_id stor_name stor_address
1 6380 Eric the Read Books 788 Catamaugus Ave.
2 7066 Barnum 's 567 Pasadena Ave.
3 7067 News & Brews 577 First St.
4 7131 Doc-U-Mat: Quality Laundry and Books 24-A Avogadro Way
5 7896 Fricative Bookshop 89 Madison St.
6 8042 Bookbeat 679 Carson St.
请大家帮下忙,环境是mssql2000
------解决方案--------------------select identity(1,1),stor_id,stor_name,stor_address from pubs.stores
------解决方案--------------------select id=(select count(*)+1 from stores where stor_id <a.stor_id),* from stores a
------解决方案--------------------多加个条件不能解决?
--try
select id=(select count(*)+1 from stores where col=a.col and stor_id <a.stor_id),* from stores a
------解决方案--------------------可以用rank()函数吗?
就是
select stor_id,stor_name,stor_address,rank()over(partion by stor_id)as rank
------解决方案--------------------select stor_id,stor_name,stor_address,rank()over(partion by stor_id)as rank from pubs.stores
------解决方案--------------------楼上正解。
套用一下:
关系社会,本来就是靠关系,不靠能力,
硬来即使串上去,成活率也会奇低。
------解决方案--------------------如果是2005,可以使用ROW_NUMBER(),2000好象只能借助临时表!