select MAX (cast (id as numeric)) from table --whatever value it may return
select
(row_number () over (order by id)) as Number from table --will start at 1
如何组合这两个,所以无论第一个查询返回什么值,我都可以使用该值并从那里自动增加,以某种方式将两者结合起来(尝试嵌套它们,但不成功)或者我需要...
...一样
select
(row_number () over (order by id) + (declared_max_value)) as Number from table
答案 0 :(得分:2)
试试这个:
WITH CTE as
(SELECT max(Field) FROM Table)
SELECT WhatYouWant, cte.m FROM Table2
INNER JOIN CTE ON 0=0
或者这个:
SELECT *, t.maxField FROM Table1 OUTER APPLY (SELECT max(Field) as maxField FROM Table2) t
答案 1 :(得分:0)
declare @maxValue int
select @maxValue = select max(cast (id as integer)) from table
declare @uaerystring varchar(max)
select @queryString =
'(row_number () over (order by id) + '
+ @maxValue +
')as Number from table'
Execute(@queryString)
答案 2 :(得分:0)
试试这个:
SELECT
Seed.ID + ROW_NUMBER() OVER (order by T.ID) as Number,
T.ID
FROM
T CROSS JOIN
(SELECT MAX(ID) AS ID FROM T) AS Seed
答案 3 :(得分:0)
DECLARE @T Table (ID int , Value Varchar(20))
INSERT INTO @T (ID, Value)
VALUES (50, 'Value1'), (500, 'Value1'), (100, 'Value1'), (50, 'Value2'), (100, 'Value2'),(500, 'Value2')
;with CTE (Value, ID, rn)
AS
(
SELECT Value, MAX(ID), rn = ROW_NUMBER() OVER (ORDER BY Value ASC)
FROM
@T
GROUP BY Value
)
SELECT * FROM CTE