Sybase数据库实现等效的mysql中group_concat功能
在MySQL中,如果想实现将分组之后的多个数据合并到一列,可以使用group_concat函数,如下图所示:
但是,在Sybase中没有这样的函数(别问我为什么使用Sybase,因为公司用的Sybaseo(╯□╰)o)。因为我的Sybase是ASE的,使用变量累计的方法实现了该功能。憋说话,看代码:
IF OBJECT_ID('#test') IS NOT NULL
drop table #test
go
CREATE TABLE #test(
id int null
,comment varchar(100) null
)
GO
insert into #test values(1,'111')
insert into #test values(1,'222')
insert into #test values(1,'333')
insert into #test values(1,'444')
insert into #test values(1,'555')
insert into #test values(1,'666')
insert into #test values(1,'777')
insert into #test values(2,'123')
insert into #test values(2,'456')
insert into #test values(2,'789')
insert into #test values(2,'012')
insert into #test values(2,'345')
insert into #test values(2,'678')
insert into #test values(3,'123')
insert into #test values(3,'456')
insert into #test values(3,'789')
insert into #test values(4,'123')
insert into #test values(4,'456')
insert into #test values(5,'234')
insert into #test values(6,'345')
insert into #test values(7,'789')
GO
BEGIN
declare @cc varchar(500)
declare @cc1 int
declare @num int
set @cc=''
set @num=1
select id,comment,space(500) as sub_comment,0000 as lev into #tt from #test order by id
update #tt
set sub_comment=(case when @cc1=id then @cc || ',' || comment else comment end)
,@cc=(case when @cc1=id then @cc || ',' || comment else comment end)
,lev=(case when @cc1=id then @num+1 else 1 end)
,@num=(case when @cc1=id then @num+1 else 1 end)
,@cc1=id
select t.id,t.sub_comment
from #tt t inner join (select id, max(lev) as tl from #tt group by id) c
on t.id=c.id and t.lev=c.tl
--如果一个分组中的comment多于5个,最多显示5个comment
-- select t.id,t.sub_comment
-- from #tt t inner join (select id, (case when max(lev) > 5 then 5 else max(lev) end) as tl from #tt group by id) c
-- on t.id=c.id and t.lev=c.tl
truncate table #tt
drop table #tt
END
Sybase数据库实现等效的mysql中group_concat功能的更多相关文章
- oracle数据库不支持mysql中limit功能
oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的. (1)使查询结果最多返回前10行 ...
- MySQL中group_concat函数-和group by配合使用
MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ...
- MySQL中group_concat函数深入理解
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . 一.MySQL中group_concat函数 完整的语法如下: gr ...
- MySQL中group_concat函数
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) .MySQL中group_concat函数完整的语法如下:group_c ...
- MySQL中group_concat函数 --- 很有用的一个用来查询出所有group by 分组后所有 同组内的 内容
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- mysql中group_concat函数用法
该函数返回带有来自一个组的连接的非NULL值的字符串结果.该函数是一个增强的Sybase SQL Anywhere支持的基本LIST()函数. 语法结构: GROUP_CONCAT([DISTINCT ...
- MYSQL中group_concat有长度限制!默认1024
在mysql中,有个函数叫"group_concat",平常使用可能发现不了问题,在处理大数据的时候,会发现内容被截取了,其实MYSQL内部对这个是有设置的,默认不设置的长度是10 ...
- MYSQL中group_concat有长度限制!默认1024(转载)
在mysql中,有个函数叫“group_concat”,平常使用可能发现不了问题,在处理大数据的时候,会发现内容被截取了,其实MYSQL内部对这个是有设置的,默认不设置的长度是1024,如果我们需要更 ...
- MYSQL中group_concat( )函数中参数的排序方法
使用mysql中的group_concat( )函数连接指定字段时,可以先对该字段进行排序. PS:是因为二刷mysql的51道题的第12题遇到的:查询和" 01 "号同学学习的课 ...
随机推荐
- 基于DFS的拓扑排序
传送门:Kahn算法拓扑排序 摘录一段维基百科上的伪码: L ← Empty list that will contain the sorted nodes S ← Set of all nodes ...
- jacascript document对象
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! Document 类型表示文档,或文档的根节点,这个节点是隐藏的,没有具体的节点标签:而 html 是根标 ...
- Django项目实战之用户上传与访问
1 将文件保存到服务器本地 upload.html <!DOCTYPE html> <html lang="en"> <head> <me ...
- 盒子浮动float
一.float的基本规律 规律1: 标准流模型中的块级盒子,默认宽度100%: 而浮动的块级盒子,宽度不会自动伸展,而是由内容(文字.padding)撑开: 浮动后的行级元素,可以设置宽度高度等属性. ...
- [JSOI 2011]分特产
Description JYY 带队参加了若干场ACM/ICPC 比赛,带回了许多土特产,要分给实验室的同学们. JYY 想知道,把这些特产分给N 个同学,一共有多少种不同的分法?当然,JYY 不希望 ...
- 【BZOJ1026】【SCOI2009】windy数
Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间,包括A和B,总共有多少个windy数? In ...
- UVA 11584 划分回文字串
将其划分为尽可能少的回文串 dp[i] = min(dp[i],dp[j] + 1) 来表示j+1~i是回文串 #include <iostream> #include <cs ...
- hdu 5130(2014广州 圆与多边形相交模板)
题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y), (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...
- bzoj4596[Shoi2016]黑暗前的幻想乡 Matrix定理+容斥原理
4596: [Shoi2016]黑暗前的幻想乡 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 464 Solved: 264[Submit][Sta ...
- [APIO2008]
A.免费道路roads 题意:给定n个点m条边的图,边有黑白颜色,求是否有一个生成树满足恰好有K条黑边,并输出方案. 题解:先加白边,求出必须加的黑边,然后加黑边到K条,剩下的用白边填充. 顺便说说, ...