sql - U-SQL 是否接受使用多个 FIRST_VALUE 来删除特定列中的重复项?
问题描述
我有一个表,其中多行重复,因为它们的值不同的两个日期列。
我想知道是否可以接受在这样的两列中使用 FIRST_VALUE 来删除指定列上的重复项:
SELECT
EmployeeName,
FIRST_VALUE(StartDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS StartDateTime,
FIRST_VALUE(UpdatedDateTime) OVER(ORDER BY UpdatedDateTime DESC) AS UpdatedDateTime
FROM @Employees;
解决方案
如果您需要删除某些字段的重复项,则需要使用ROW_NUMBER()
和 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 |
+----+------------+
*/
推荐阅读
- markdown - 如何在 Markdown 中创建指向某个单词的链接?
- mapbox - 是否可以在 plotly.express scatter_mapbox 中使用本地 GeoTIFF 文件作为背景层
- typescript - 使用泛型或继承哪种方式更正确?
- vb.net - 修改天真的搜索算法以允许容忍
- wordpress - 如何禁用 WordPress 网站的浏览器阅读器视图
- amazon-web-services - AWS StepFunction:从异步执行结束根 Stepfunction 执行
- c# - 将数组从一行复制到一个数组对象中-“_numbersArray 从未分配给,并且始终具有其默认值 null”
- json - Aeson:将 JSON 对象转换为键、值类型的列表
- linux - FTP 大小命令在 linux shell 中运行,但在 bash 脚本中不起作用
- javascript - 使用 javascript 显示/隐藏 html 元素