jcv*_*gan 0 sql sql-server-2008
我正在创建一个包含三个表的联合的选择查询....就像这样
select a as A,b as B c as C where c = x union
select b as A,d as B e as C where e = y and d = a union
select f as A,g as B,h as C
Run Code Online (Sandbox Code Playgroud)
查询结果如下:
A B C
===========
1 abc ...
55 def ...
1 sas ...
Run Code Online (Sandbox Code Playgroud)
所以我想要一个计算行数的列,只是为了防止重复标识符.像这样的东西
Row A B C
================
1 1 abc ...
2 55 def ...
3 1 sas ...
Run Code Online (Sandbox Code Playgroud)
....
我的问题是它是如何做到的?
您可以像这样使用ROW_NUMBER():
SELECT ROW_NUMBER() OVER (ORDER BY A,B,C) AS RowNo, *
FROM
(
select a as A,b as B c as C where c = x
union
select b as A,d as B e as C where e = y and d = a
union
select f as A,g as B,h as C
) x
Run Code Online (Sandbox Code Playgroud)