SQL 每列序列
SQL Sequence per column
Table
City Classes
------------------
SCL X
SCL Y
SCL Z
Blah X
Blah Y
Narf X
查询以获得此输出:
City Classes Sequence
SCL X 1
SCL Y 2
SCL Z 3
Blah X 1
Blah Y 2
Narf X 1
有人可以帮助我并告诉我如何实现吗?
SELECT
City,
Classes,
row_number() over (partition by City order by classes) Sequence
FROM (values
('SCL','X'),('SCL','Y'),('SCL','Z'),
('Blah','X'),('Blah','Y'),('Narf','X')) x(City, Classes)
结果:
City Classes Sequence
Blah X 1
Blah Y 2
Narf X 1
SCL X 1
SCL Y 2
SCL Z 3
Table
City Classes
------------------
SCL X
SCL Y
SCL Z
Blah X
Blah Y
Narf X
查询以获得此输出:
City Classes Sequence
SCL X 1
SCL Y 2
SCL Z 3
Blah X 1
Blah Y 2
Narf X 1
有人可以帮助我并告诉我如何实现吗?
SELECT
City,
Classes,
row_number() over (partition by City order by classes) Sequence
FROM (values
('SCL','X'),('SCL','Y'),('SCL','Z'),
('Blah','X'),('Blah','Y'),('Narf','X')) x(City, Classes)
结果:
City Classes Sequence
Blah X 1
Blah Y 2
Narf X 1
SCL X 1
SCL Y 2
SCL Z 3