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 "号同学学习的课 ...
随机推荐
- SpringMVC(十):SpringMVC 处理输出模型数据之Map及Model
Spring MVC提供了以下几种途径输出模型数据: 1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据: 2)Map及Model:处理方法 ...
- requests post一个json数据
# post一个json数据 import requests headers={ "Accept":"application/json, text/plain, */*& ...
- python操作MySQL数据库并将数据写入excel
#!/usr/bin/python# -*- coding:utf-8 -*-'''方法:通过pymsql模块连接mysql数据库,然后通过游标cursor查询SQL语句将结果存储在Excel文件中, ...
- 顺序或者说优先级的重要性---解决dom生成问题有感
我们的大脑有逻辑,程序也有逻辑,只要一切都刚刚好,那么我们大脑的逻辑和程序的逻辑是没有冲突的:但是,有时候,我们想当然,只顾自己头脑中的逻辑,而随意臆想程序的逻辑,这个时候,就会有很多我们觉得不可思议 ...
- mysql列约束
列属性(约束)1: 是否允许为空(not null) --not null不允许为空create table t_1( a tinyint(3) zerofill not null, b ...
- [Codeforces 933B]A Determined Cleanup
Description 题库链接 给你两个正整数 \(p,k\) ,询问是否能够构造多项式 \(f(x)=\sum\limits_{i=0}^{d-1}a_ix^i\) ,使得存在多项式 \(q(x) ...
- [HNOI2015]菜肴制作
题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴 ...
- 【线段树】【BZOJ1798】【AHOI2009】维护序列
还是那个学弟@lher出的丧题之一. 链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题意简析:就是题目啊... 解题思路:显然是线段树 ...
- UVA 1451 Average
A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...
- [hdu5608]function
题意:$\sum_{d|n}f(d)=n^{2}-3n+2$,求$\sum_{i=1}^{n}f(i)\mod 10^{9}+7$ , $n \leqslant 10^{9}$ $\left( T \ ...