如何合并sql server 2008中的最后两行 [英] How to merge the last two rows in sql server 2008

查看:67
本文介绍了如何合并sql server 2008中的最后两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据类似

10

20

30

40

50



我想要40~50之类的数据(这意味着最后两行合并数据)

I have data like
10
20
30
40
50

I want data like 40~50 (that means last two rows merge data)

推荐答案

试试这个

try this
select  STUFF((SELECT '-' + numbers AS [text()] 
FROM 
(
select top 2* from (
select * from [TableName] EXCEPT
select top (select (COUNT(1)-2) from [TableName]) * from [TableName])A
)B
FOR XML PATH('') ), 1, 1, '' )MergedCells






尝试此查询....

Hi,

Try this query....
create table #test (id int)
insert into #test(id)
select 10 union all
select 20 union all
select 30 union all
select 40 union all
select 50

declare @Ids varchar(100)
select @Ids=coalesce(@Ids+'~','')+cast(T.id as varchar(10)) from (select top 2 id from #test order by id desc)T
select @Ids 'RequiredData'

drop table #test



问候,

GVPrabu


Regards,
GVPrabu


这篇关于如何合并sql server 2008中的最后两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆