gpt4 book ai didi

sql - 向表中添加数字 id

转载 作者:行者123 更新时间:2023-12-02 08:48:41 33 4
gpt4 key购买 nike

SELECT top 5
day_description,
count(numberofdays) as number

FROM mytable

where department = 'departmentname'

group by day_description

我的选择带回了这样的东西:

day description           number           
holiday 32
working day 212
training day 5
meeting day 26
admin day 66

有没有办法添加一个新列,它指定 1-5,1 是最高值行,5 是最低值行。看起来像这样:

day description           number           top5ID
holiday 32 3
working day 212 1
training day 5 5
meeting day 26 4
admin day 66 2

最佳答案

也许是这样的:

;WITH CTE
AS
(
SELECT top 5
day_description,
count(numberofdays) as number
FROM mytable
where department = 'departmentname'
group by day_description
)
SELECT
ROW_NUMBER() OVER(ORDER BY CTE.number DESC) AS Top5ID,
CTE.*
FROM
CTE

或者没有 CTE:

SELECT
ROW_NUMBER() OVER(ORDER BY t.number DESC) AS Top5ID,
t.*
FROM
(
SELECT top 5
day_description,
count(numberofdays) as number
FROM mytable
where department = 'departmentname'
group by day_description
) AS t

关于sql - 向表中添加数字 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413362/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com