我的表如下:
DECLARE @Table TABLE(
NAME VARCHAR(20),
MOBILE VARCHAR(20),
TIME INT
)
INSERT INTO @Table SELECT 'A','1111111',10
INSERT INTO @Table SELECT 'B','222222',20
INSERT INTO @Table SELECT 'C','333333',60
INSERT INTO @Table SELECT 'D','4444444',70
INSERT INTO @Table SELECT 'A','1111111',100
INSERT INTO @Table SELECT 'B','222222',10
SELECT NAME,MOBILE,TIME,
RANK() OVER (ORDER BY TIME asc) AS xRank
FROM
(
SELECT NAME ,MOBILE ,TIME
FROM @Table
) AS a
ORDER BY 1
我想要的结果如下所示。如果两行有相同的NAME
和MOBILE
,则只显示一行时间较短的行:
NAME MOBILE TIME XRANK
A 1111111 10 1
B 222222 10 1
C 333333 60 3
D 4444444 70 4
NAME MOBILE TIME XRANK
A 1111111 10 1
B 1111111 10 2
B 222222 20 3
C 333333 60 4
D 4444444 70 5
答案 0 :(得分:2)
;with cteRank
as
(
SELECT
NAME, MOBILE, TIME,
rn = row_number() OVER (partition by name, mobile ORDER BY TIME asc)
FROM @Table
)
SELECT NAME, MOBILE, TIME
FROM cteRank
where rn = 1
ORDER BY 1
输出(“如果两行具有相同的NAME和MOBILE,则只显示一行时间较短”):
NAME MOBILE TIME
A 1111111 10
B 1111111 10
B 222222 20
C 333333 60
D 4444444 70
答案 1 :(得分:1)
试试这个
SELECT name,
mobile,
time,
rn,
Row_number()
OVER (
ORDER BY name) AS XRANK
FROM (SELECT name,
mobile,
time,
Row_number()
OVER(
partition BY name, mobile
ORDER BY name ASC) AS RN
FROM @Table) AS T
WHERE rn = 1
<强>输出强>
NAME MOBILE TIME XRANK
A 1111111 10 1
B 1111111 10 2
B 222222 20 3
C 333333 60 4
D 4444444 70 5