在同一请求中使用string_agg和last_value函数
我如何从ID订单中找到的最后一个记录中获得777777,以
SELECT
STRING_AGG(name,'-') WITHIN GROUP (ORDER BY id) as names,
STRING_AGG([numerToCall],'-') as calledNumbers,
--LAST_VALUE([numerToCall])) OVER (ORDER BY id) as lastCalled
'777777' as lastCalled
FROM
SimpleTest1
创建表格和数据。
CREATE TABLE [dbo].[simpleTest1](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[numerToCall] [varchar](50) NOT NULL,
CONSTRAINT [PK_simpleTest1] PRIMARY KEY CLUSTERED
(
[id] ASC
)
) ON [PRIMARY]
SET IDENTITY_INSERT [dbo].[simpleTest1] ON
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (1, N'benny', N'555555')
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (2, N'helle', N'999999')
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (3, N'hans', N'777777')
SET IDENTITY_INSERT [dbo].[simpleTest1] OFF
解决方案是
SELECT
STRING_AGG(t.name,'-') WITHIN GROUP (ORDER BY id) as names,
STRING_AGG(t.numerToCall, '-') as calledNumbers,
MIN(t.xxxx) as lastCalled
FROM (
SELECT *,
LAST_VALUE(t.numerToCall) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) as xxxx
from
SimpleTest1 t
) t;
@Charlieface
How do I get the 777777 from the last record found in the order by id
SELECT
STRING_AGG(name,'-') WITHIN GROUP (ORDER BY id) as names,
STRING_AGG([numerToCall],'-') as calledNumbers,
--LAST_VALUE([numerToCall])) OVER (ORDER BY id) as lastCalled
'777777' as lastCalled
FROM
SimpleTest1
to create table and data
CREATE TABLE [dbo].[simpleTest1](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[numerToCall] [varchar](50) NOT NULL,
CONSTRAINT [PK_simpleTest1] PRIMARY KEY CLUSTERED
(
[id] ASC
)
) ON [PRIMARY]
SET IDENTITY_INSERT [dbo].[simpleTest1] ON
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (1, N'benny', N'555555')
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (2, N'helle', N'999999')
INSERT [dbo].[simpleTest1] ([id], [name], [numerToCall]) VALUES (3, N'hans', N'777777')
SET IDENTITY_INSERT [dbo].[simpleTest1] OFF
The solution was
SELECT
STRING_AGG(t.name,'-') WITHIN GROUP (ORDER BY id) as names,
STRING_AGG(t.numerToCall, '-') as calledNumbers,
MIN(t.xxxx) as lastCalled
FROM (
SELECT *,
LAST_VALUE(t.numerToCall) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) as xxxx
from
SimpleTest1 t
) t;
thanks @Charlieface
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将窗口函数放入派生表中,然后对其使用聚合函数(
MIN
或MAX
)You can put the window function in a derived table, and then use an aggregation function on it (either
MIN
orMAX
)您可以使用子查询
,也可以解析聚合的结果并从字符串中获取结果,而无需其他查询(无子查询)
检查您的特定数据库中的两个解决方案,然后选择为您提供更好性能的一个解决方案(SSMS执行计划分析表明,第二个查询更好,但不要依靠它,并在您的服务器中检查IO和时间)
You can use sub-query
Or you can parse the result of the aggregation and get the result from the string without the need of another query (no sub-query)
Check both solutions in your specific database and choose the one which provide you better performance (SSMS execution Plan analyze shows that second query is better but don't count on it and check IO and TIME in your server)