U-SQL是否接受使用多个FIRST_值删除特定列中的重复项?
我有一个表,其中多行是重复的,因为两个日期列的值不同 我想知道这样在两列中使用第一个\u值来删除指定列上的重复项是否被接受:U-SQL是否接受使用多个FIRST_值删除特定列中的重复项?,sql,u-sql,Sql,U Sql,我有一个表,其中多行是重复的,因为两个日期列的值不同 我想知道这样在两列中使用第一个\u值来删除指定列上的重复项是否被接受: SELECT EmployeeName, FIRST_VALUE(StartDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS StartDateTime, FIRST_VALUE(UpdatedDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS UpdatedDateTim
SELECT
EmployeeName,
FIRST_VALUE(StartDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS StartDateTime,
FIRST_VALUE(UpdatedDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS UpdatedDateTime
FROM @Employees;
如果需要删除某些字段上的重复项,则需要使用
行号()
和CTE:
-- Sample data: dates duplicates
declare @t table (id int, dt date);
insert into @t values
(1, '2018-01-14'),
(1, '2018-01-14'),
(1, '2018-01-15'),
(1, '2018-01-15');
with cte as (
select *,
-- assign row number for each partition consisting of same date
row_number() over (partition by dt order by dt) as cnt
from @t
)
-- we're interested only in one row (i.e. first)
select id, dt from cte where cnt = 1;
/*
Output:
+-------+---------+
| id | dt |
+----+------------+
| 1 | 2018-01-14 |
|----|------------|
| 1 | 2018-01-15 |
+----+------------+
*/
如果这是您需要的,那么就使用它。@GordonLinoff我不确定这是删除特定列上重复项的正确方式,我想知道最有效的方式。如果您需要删除重复项,您需要在这些日期使用
ROW\u NUMBER()
。@JohnyL您能提供一个例子作为答案吗?我已经回答了