SQL 按日期和顺序分区
SQL Partition By Date and Sequence
下面是我的数据。
Date
Item
Location
Seqn
2/25/2022 17:00
A1234
L1
2
3/4/2022 18:00
A1234
L1
1
4/7/2022 3:00
A1234
L1
2
4/7/2022 3:00
A1234
L1
4
4/7/2022 3:00
A1234
L1
6
4/8/2022 11:00
A1234
L1
1
4/13/2022 8:00
A1234
L1
2
4/16/2022 17:00
A1234
L1
2
4/29/2022 15:00
A1234
L1
1
5/10/2022 5:00
A1234
L1
2
5/10/2022 5:00
A1234
L1
4
5/10/2022 5:00
A1234
L1
6
5/10/2022 5:00
A1234
L1
8
5/10/2022 5:00
A1234
L1
10
5/19/2022 4:00
A1234
L1
1
我需要根据日期和顺序使用分区依据和 row_number 进行分隔。因为我想获得每个日期的第一条记录。
table 下方将是上述示例的结果:
Date
Item
Location
Seqn
Row_number
2/25/2022 17:00
A1234
L1
2
1
3/4/2022 18:00
A1234
L1
1
1
4/7/2022 3:00
A1234
L1
2
1
4/7/2022 3:00
A1234
L1
4
2
4/7/2022 3:00
A1234
L1
6
3
4/8/2022 11:00
A1234
L1
1
1
这是我的查询:
select * from (
WITH test AS (
SELECT
row_number() OVER(
PARTITION BY Item, Location, Date, Seqn
ORDER by date, Seqn
) row_num,
date,
item,
location,
seqn
FROM
table1
)
SELECT * FROM test WHERE row_num = 1
)T1
where
item='A1234'
and location='L1'
Order by
trdt,
seqn
我已经弄明白这两天了。请帮助
我 认为 你缺少的是 date
列包含时间部分,你需要在 PARTITION BY
.
中使用它之前删除它
WITH
sorted AS
(
SELECT
row_number() OVER (
PARTITION BY item, location, seqn, TRUNC(date)
ORDER BY date
)
AS row_num,
date,
item,
location,
seqn
FROM
table1
)
SELECT
*
FROM
sorted
WHERE
row_num = 1
AND item = 'A1234'
AND location = 'L1'
ORDER BY
location,
item,
seqn,
date
WITH
sorted AS
(
SELECT
row_number() OVER (
PARTITION BY item, location, TRUNC(date)
ORDER BY date,seqn
)
AS row_num,
item,
location,
seqn
FROM
table1
)
SELECT
*
FROM
sorted
WHERE row_num = 1
下面是我的数据。
Date | Item | Location | Seqn |
---|---|---|---|
2/25/2022 17:00 | A1234 | L1 | 2 |
3/4/2022 18:00 | A1234 | L1 | 1 |
4/7/2022 3:00 | A1234 | L1 | 2 |
4/7/2022 3:00 | A1234 | L1 | 4 |
4/7/2022 3:00 | A1234 | L1 | 6 |
4/8/2022 11:00 | A1234 | L1 | 1 |
4/13/2022 8:00 | A1234 | L1 | 2 |
4/16/2022 17:00 | A1234 | L1 | 2 |
4/29/2022 15:00 | A1234 | L1 | 1 |
5/10/2022 5:00 | A1234 | L1 | 2 |
5/10/2022 5:00 | A1234 | L1 | 4 |
5/10/2022 5:00 | A1234 | L1 | 6 |
5/10/2022 5:00 | A1234 | L1 | 8 |
5/10/2022 5:00 | A1234 | L1 | 10 |
5/19/2022 4:00 | A1234 | L1 | 1 |
我需要根据日期和顺序使用分区依据和 row_number 进行分隔。因为我想获得每个日期的第一条记录。 table 下方将是上述示例的结果:
Date | Item | Location | Seqn | Row_number |
---|---|---|---|---|
2/25/2022 17:00 | A1234 | L1 | 2 | 1 |
3/4/2022 18:00 | A1234 | L1 | 1 | 1 |
4/7/2022 3:00 | A1234 | L1 | 2 | 1 |
4/7/2022 3:00 | A1234 | L1 | 4 | 2 |
4/7/2022 3:00 | A1234 | L1 | 6 | 3 |
4/8/2022 11:00 | A1234 | L1 | 1 | 1 |
这是我的查询:
select * from (
WITH test AS (
SELECT
row_number() OVER(
PARTITION BY Item, Location, Date, Seqn
ORDER by date, Seqn
) row_num,
date,
item,
location,
seqn
FROM
table1
)
SELECT * FROM test WHERE row_num = 1
)T1
where
item='A1234'
and location='L1'
Order by
trdt,
seqn
我已经弄明白这两天了。请帮助
我 认为 你缺少的是 date
列包含时间部分,你需要在 PARTITION BY
.
WITH
sorted AS
(
SELECT
row_number() OVER (
PARTITION BY item, location, seqn, TRUNC(date)
ORDER BY date
)
AS row_num,
date,
item,
location,
seqn
FROM
table1
)
SELECT
*
FROM
sorted
WHERE
row_num = 1
AND item = 'A1234'
AND location = 'L1'
ORDER BY
location,
item,
seqn,
date
WITH
sorted AS
(
SELECT
row_number() OVER (
PARTITION BY item, location, TRUNC(date)
ORDER BY date,seqn
)
AS row_num,
item,
location,
seqn
FROM
table1
)
SELECT
*
FROM
sorted
WHERE row_num = 1