sql从几个相同的值中选择一个
sql selection of one value from several identical
我有执行查询的结果。它从多个表中收集数据。他是这样的:
|Name|date |number|Id
|alex|01-01-2021 |1111 | 1
|mike|01-01-2021 |2222 | 2
|alex|02-01-2021 |1111 | 3
|alex|03-01-2021 |1111 | 4
|john|04-01-2021 |3333 | 5
我需要得到以下结果:
|Name|date |number| Id
|mike|01-01-2021|2222 | 2
|alex|any value |1111 | Any value
|john|04-01-2021|3333 | 5
我需要 select 重复值之一并显示 it.I 有一个包含许多列的大型查询。这里我只给出了一个简短的版本来说明问题的本质
select Name,max(date) as date,number
from atable
group by Name, number
您可以使用此 CTE 并管理您将获得的日期(第一个或最后一个)
WITH data AS (
SELECT
Name,
date,
number,
row_number() OVER (PARTITION BY Name ORDER BY date) AS row_num
FROM test01
)
SELECT
Name,
date,
number
FROM data
WHERE row_num = 1
我有执行查询的结果。它从多个表中收集数据。他是这样的:
|Name|date |number|Id
|alex|01-01-2021 |1111 | 1
|mike|01-01-2021 |2222 | 2
|alex|02-01-2021 |1111 | 3
|alex|03-01-2021 |1111 | 4
|john|04-01-2021 |3333 | 5
我需要得到以下结果:
|Name|date |number| Id
|mike|01-01-2021|2222 | 2
|alex|any value |1111 | Any value
|john|04-01-2021|3333 | 5
我需要 select 重复值之一并显示 it.I 有一个包含许多列的大型查询。这里我只给出了一个简短的版本来说明问题的本质
select Name,max(date) as date,number
from atable
group by Name, number
您可以使用此 CTE 并管理您将获得的日期(第一个或最后一个)
WITH data AS (
SELECT
Name,
date,
number,
row_number() OVER (PARTITION BY Name ORDER BY date) AS row_num
FROM test01
)
SELECT
Name,
date,
number
FROM data
WHERE row_num = 1