admin管理员组文章数量:1125710
This is my fiddle
I have a table like this
create table CTE1 (
[CC Receiver] VARCHAR(10),
Name VARCHAR(255),
[Division] varchar(10),
[Old Block] varchar(10),
[Date] Date,
Round VARCHAR(10),
Platform VARCHAR(10),
block_type VARCHAR(255),
tph_type VARCHAR(255)
);
insert into CTE (
[CC Receiver],
Name,
[Division],
[Old Block],
[Date],
Round,
Platform,
block_type,
tph_type
)
values
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '013', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '017', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '010', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '024', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '46', '013', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '46', '016', 'Bukit', 'Collection Road');
SELECT *
FROM CTE;
CC Receiver | Name | Division | Old Block | Date | Round | Platform | block_type | tph_type |
---|---|---|---|---|---|---|---|---|
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 013 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 017 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 010 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 024 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 013 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 016 | Bukit | Collection Road |
This is my fiddle
I have a table like this
create table CTE1 (
[CC Receiver] VARCHAR(10),
Name VARCHAR(255),
[Division] varchar(10),
[Old Block] varchar(10),
[Date] Date,
Round VARCHAR(10),
Platform VARCHAR(10),
block_type VARCHAR(255),
tph_type VARCHAR(255)
);
insert into CTE (
[CC Receiver],
Name,
[Division],
[Old Block],
[Date],
Round,
Platform,
block_type,
tph_type
)
values
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '013', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '017', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '010', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '45', '024', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '46', '013', 'Bukit', 'Collection Road'),
('BNAB21A020', 'ESTATE TEPIAN LANGSAT', 'A', 'M28', '2024-01-09', '46', '016', 'Bukit', 'Collection Road');
SELECT *
FROM CTE;
CC Receiver | Name | Division | Old Block | Date | Round | Platform | block_type | tph_type |
---|---|---|---|---|---|---|---|---|
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 013 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 017 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 010 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 024 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 013 | Bukit | Collection Road |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 016 | Bukit | Collection Road |
The objective is to give row_number based on the same [CC Receiver], YEAR(Date), Month(Date), Name, Division, Old Block, and order by Round.
based on the requirement, this is my query
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY YEAR(Date), MONTH(Date), [CC Receiver], Name, Division, [Old Block]
ORDER BY Round
) AS rotasi
FROM CTE;
and this is the result:
CC Receiver | Name | Division | Old Block | Date | Round | Platform | block_type | tph_type | rotasi |
---|---|---|---|---|---|---|---|---|---|
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 013 | Bukit | Collection Road | 1 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 017 | Bukit | Collection Road | 2 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 010 | Bukit | Collection Road | 3 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 024 | Bukit | Collection Road | 4 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 013 | Bukit | Collection Road | 5 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 016 | Bukit | Collection Road | 6 |
This is not suitable with my requirement as the code put the order based on row number including platform, and what I want is to exclude the platform.
Expected result:
CC Receiver | Name | Division | Old Block | Date | Round | Platform | block_type | tph_type | rotasi |
---|---|---|---|---|---|---|---|---|---|
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 013 | Bukit | Collection Road | 1 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 017 | Bukit | Collection Road | 1 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 010 | Bukit | Collection Road | 1 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 45 | 024 | Bukit | Collection Road | 1 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 013 | Bukit | Collection Road | 2 |
BNAB21A020 | ESTATE TEPIAN LANGSAT | A | M28 | 2024-01-09 | 46 | 016 | Bukit | Collection Road | 2 |
1 Answer
Reset to default 3You want to use DENSE_RANK()
here, not ROW_NUMBER()
:
SELECT t.*,
DENSE_RANK() OVER (
PARTITION BY YEAR(Date), MONTH(Date), [CC Receiver], Name, Division, [Old Block]
ORDER BY Round
) AS rotasi
FROM yourTable t;
Note that the perceived order coming from the platform column might have been by random chance. Given that the records are identical sans the platform value, your SQL database could have chosen a row number in any order it wanted.
本文标签: sqlRank rows by specified partitionStack Overflow
版权声明:本文标题:sql - Rank rows by specified partition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736673697a1947060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论