해당 열에 의해 순서로 열 이후 열을 수립하십시오 (순위 설정)
-
12-11-2019 - |
문제
SQL에서 와 같은 일을하고 싶습니다.
update table set col = rank(col) order by col
.
어떻게해야합니까?
목적 :
현재 col은 -14000, 23, 4000, 23000과 같은 극단적 인 값 을가집니다. asc, desc 정렬이지만 슬라이더에 플롯 할 때 각 슬라이더 위치는 고르지 않습니다.데이터, 심지어 컬럼을 다시 바꾸고 싶습니다.
-14000 becomes 0
23 becomes 1
4000 becomes 2
.
등
해결책
사용 :
update table set col = (select count(*) from (select col from table) as temptable where temptable.col <table.col );
. 다른 팁
SQL Server에서 두 개의 하위 쿼리와 ROW_NUMBER
기능을 사용할 수 있습니다.col
에서 중복 된 경우 표준 경쟁 순위 .
샘플 스크립트 :
SELECT * INTO #TABLE
FROM
(
select -14000 col
union all SELECT 23
union all select 4000
union all SELECT 23 --sample duplicated data
) Unioned
UPDATE #TABLE
SET col =
(
SELECT top 1 rowNum
FROM
(
SELECT
col
, row_number() OVER (order by col) - 1 rowNum --starts at 0 rank
FROM #TABLE
) MySubQuery
WHERE MySubQuery.col = #TABLE.col
)
. 제휴하지 않습니다 StackOverflow