Select 分钟 BeginDate 从重复
Select min BeginDate from duplicates
select [BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate]
from (
select
[BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate],
count(*) over (partition by [BK_Vendedor]) as dc
from [test_SA].[dbo].[Dim_Vendedor]
) as T
where dc > 1
此 table 是此查询的结果,该查询仅从另一个更大的 table 的 BK_Vendedor 列中选取重复值。但在同一个查询中,我也只想要每个 BK_Vendedor 中具有最小 BeginDate 的行。我该怎么做?
添加 ROW_NUMBER 在 BK_Vendedor
上分区并过滤行号 = 1。当您按字段(或多个字段!)分区 ROW_NUMBER 时,它会重置计数每次字段更改时都为 1。您有效地按字段分组,每个组都有自己的一系列行号。我们用它来获得每个组中的第一个 BeginDate
。
请注意,如果我们想要 last BeginDate
我们将反转顺序,因此最后一个是 #1 (ROW_NUMBER () over (partition by BK_Vendedor ORDER BY BeginDate DESC) as Ord
) 并保留 WHERE 子句指定 Ord
= 1
with cteSampleData as (
SELECT * FROM (VALUES (100, 10, '2022-01-10', '2022-01-25')
, (100, 10, '2022-01-08', '2022-01-15')
, (101, 11, '2022-03-10', '2022-04-15')
, (102, 12, '2022-02-10', NULL)
, (103, 13, '2022-02-15', NULL)
, (103, 13, '2022-03-15', '2022-04-10')
, (103, 15, '2022-02-21', '2022-04-01') --EDIT: Add this row
) as DimVend(BK_Vendedor,NIF,BeginDate,EndDate)
) --The above is just sample data as I don't have your [test_SA].[dbo].[Dim_Vendedor]
select [BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate]
from (
select
[BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate],
count(*) over (partition by [BK_Vendedor]) as dc
--Add the line below
, ROW_NUMBER () over (partition by BK_Vendedor ORDER BY BeginDate) as Ord
from cteSampleData
) as T
where dc > 1
AND Ord = 1 --And add this line too
输入如下所示:
BK_Vendedor
NIF
BeginDate
EndDate
100
10
2022-01-10
2022-01-25
100
10
2022-01-08
2022-01-15
101
11
2022-03-10
2022-04-15
102
12
2022-02-10
NULL
103
13
2022-02-15
NULL
103
13
2022-03-15
2022-04-10
输入(按原始顺序)加上 dc
和 Ord
:
BK_Vendedor
NIF
BeginDate
EndDate
dc
Ord
100
10
2022-01-10
2022-01-25
2
2
100
10
2022-01-08
2022-01-15
2
1
101
11
2022-03-10
2022-04-15
1
1
102
12
2022-02-10
NULL
1
1
103
13
2022-02-15
NULL
3
1
103
13
2022-03-15
2022-04-10
3
3
103
15
2022-02-21
2022-04-01
3
2
输出如下所示:
BK_Vendedor
NIF
BeginDate
EndDate
100
10
2022-01-08
2022-01-15
103
13
2022-02-15
NULL
select [BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate]
from (
select
[BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate],
count(*) over (partition by [BK_Vendedor]) as dc
from [test_SA].[dbo].[Dim_Vendedor]
) as T
where dc > 1
此 table 是此查询的结果,该查询仅从另一个更大的 table 的 BK_Vendedor 列中选取重复值。但在同一个查询中,我也只想要每个 BK_Vendedor 中具有最小 BeginDate 的行。我该怎么做?
添加 ROW_NUMBER 在 BK_Vendedor
上分区并过滤行号 = 1。当您按字段(或多个字段!)分区 ROW_NUMBER 时,它会重置计数每次字段更改时都为 1。您有效地按字段分组,每个组都有自己的一系列行号。我们用它来获得每个组中的第一个 BeginDate
。
请注意,如果我们想要 last BeginDate
我们将反转顺序,因此最后一个是 #1 (ROW_NUMBER () over (partition by BK_Vendedor ORDER BY BeginDate DESC) as Ord
) 并保留 WHERE 子句指定 Ord
= 1
with cteSampleData as (
SELECT * FROM (VALUES (100, 10, '2022-01-10', '2022-01-25')
, (100, 10, '2022-01-08', '2022-01-15')
, (101, 11, '2022-03-10', '2022-04-15')
, (102, 12, '2022-02-10', NULL)
, (103, 13, '2022-02-15', NULL)
, (103, 13, '2022-03-15', '2022-04-10')
, (103, 15, '2022-02-21', '2022-04-01') --EDIT: Add this row
) as DimVend(BK_Vendedor,NIF,BeginDate,EndDate)
) --The above is just sample data as I don't have your [test_SA].[dbo].[Dim_Vendedor]
select [BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate]
from (
select
[BK_Vendedor]
,[NIF]
,[BeginDate]
,[EndDate],
count(*) over (partition by [BK_Vendedor]) as dc
--Add the line below
, ROW_NUMBER () over (partition by BK_Vendedor ORDER BY BeginDate) as Ord
from cteSampleData
) as T
where dc > 1
AND Ord = 1 --And add this line too
输入如下所示:
BK_Vendedor | NIF | BeginDate | EndDate |
---|---|---|---|
100 | 10 | 2022-01-10 | 2022-01-25 |
100 | 10 | 2022-01-08 | 2022-01-15 |
101 | 11 | 2022-03-10 | 2022-04-15 |
102 | 12 | 2022-02-10 | NULL |
103 | 13 | 2022-02-15 | NULL |
103 | 13 | 2022-03-15 | 2022-04-10 |
输入(按原始顺序)加上 dc
和 Ord
:
BK_Vendedor | NIF | BeginDate | EndDate | dc | Ord |
---|---|---|---|---|---|
100 | 10 | 2022-01-10 | 2022-01-25 | 2 | 2 |
100 | 10 | 2022-01-08 | 2022-01-15 | 2 | 1 |
101 | 11 | 2022-03-10 | 2022-04-15 | 1 | 1 |
102 | 12 | 2022-02-10 | NULL | 1 | 1 |
103 | 13 | 2022-02-15 | NULL | 3 | 1 |
103 | 13 | 2022-03-15 | 2022-04-10 | 3 | 3 |
103 | 15 | 2022-02-21 | 2022-04-01 | 3 | 2 |
输出如下所示:
BK_Vendedor | NIF | BeginDate | EndDate |
---|---|---|---|
100 | 10 | 2022-01-08 | 2022-01-15 |
103 | 13 | 2022-02-15 | NULL |