如何在结果中添加一列以显示序号
How do I add a column to the results that would show sequential numbers
我如何修改此 SQL(或创建一个不同的 SQL)以便它在结果中添加一列 - 这将显示以 1 开头并增加 1 的序号
即1、2、3、4等
注意:我不需要添加自增列。如果可能的话,我只需要结果中的递增列
select year, quarter, uploaddate, sum(wagecount) as wagecount from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate
预期的结果是这样的
Year
Quarter
UploadDate
WageCount
SequentialNumbers
2021
1
01-JAN-21
2
1
2021
1
02-JAN-21
2
2
2021
1
03-JAN-21
1
3
2021
1
04-JAN-21
5
4
使用ROW_NUMBER
:
SELECT year, quarter, uploaddate, SUM(wagecount) AS wagecount,
ROW_NUMBER() OVER (ORDER BY uploaddate) sequential_numbers
FROM wage_reporting
GROUP BY year, quarter, uploaddate
ORDER BY year, quarter, uploaddate;
使用子查询和 rownum 伪列会有帮助
select your_query.*, rownum
from (select year, quarter, uploaddate, sum(wagecount) as wagecount
from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate) your_query
select year, quarter, uploaddate, sum(wagecount) as wagecount, SUM(1) OVER (ORDER BY year, quarter, uploaddate) as SequentialNumbers from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate
我如何修改此 SQL(或创建一个不同的 SQL)以便它在结果中添加一列 - 这将显示以 1 开头并增加 1 的序号
即1、2、3、4等
注意:我不需要添加自增列。如果可能的话,我只需要结果中的递增列
select year, quarter, uploaddate, sum(wagecount) as wagecount from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate
预期的结果是这样的
Year | Quarter | UploadDate | WageCount | SequentialNumbers |
---|---|---|---|---|
2021 | 1 | 01-JAN-21 | 2 | 1 |
2021 | 1 | 02-JAN-21 | 2 | 2 |
2021 | 1 | 03-JAN-21 | 1 | 3 |
2021 | 1 | 04-JAN-21 | 5 | 4 |
使用ROW_NUMBER
:
SELECT year, quarter, uploaddate, SUM(wagecount) AS wagecount,
ROW_NUMBER() OVER (ORDER BY uploaddate) sequential_numbers
FROM wage_reporting
GROUP BY year, quarter, uploaddate
ORDER BY year, quarter, uploaddate;
使用子查询和 rownum 伪列会有帮助
select your_query.*, rownum
from (select year, quarter, uploaddate, sum(wagecount) as wagecount
from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate) your_query
select year, quarter, uploaddate, sum(wagecount) as wagecount, SUM(1) OVER (ORDER BY year, quarter, uploaddate) as SequentialNumbers from wage_reporting
group by year, quarter, uploaddate
order by year, quarter, uploaddate