gpt4 book ai didi

SQL 选择查询

转载 作者:行者123 更新时间:2023-12-03 02:28:02 25 4
gpt4 key购买 nike

我有以下 SQL 表:

CarId | CustomerId | Price  
1 | 1 | 5000
2 | 3 | 6000
3 | 4 | 6000
4 | 1 | 6000
5 | 3 | 6000
6 | 6 | 6000

我想创建一个新列:

CarId | CustomerId | Price | CarPerCustomer
1 | 1 | 5000 | 1/2
2 | 3 | 6000 | 1/3
3 | 4 | 6000 | 1/1
4 | 1 | 6000 | 2/2
5 | 3 | 6000 | 2/3
6 | 3 | 6000 | 3/3

所以基本上 CarPerCustomer = 行数/(该客户拥有的汽车总数)。有人吗?

最佳答案

使用窗口函数

select CarId, CustomerId, Price
, cast(row_number() over (partition by CustomerId order by CustomerId) as varchar)
+ '/' + cast(count(*) over (partition by CustomerId ) as varchar)
from t

关于SQL 选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745948/

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