连接选择查询中的行(在Advantage Data Architect中) [英] Concatenate rows in select query (in Advantage Data Architect)

查看:90
本文介绍了连接选择查询中的行(在Advantage Data Architect中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连接选择查询中的行? (在Advantage Data Architect中)



我尝试运行以下脚本:



第一个脚本:

 声明@str字符串; 
set @str =’’;
从my_table1中选择@str = @str + field_1

但是我得到的结果是行包含 false,如下图所示:





第二个脚本:

  declare @str串; 
select @str = coalesce(@str +',','')+ field_1 from my_table1

这一次,所有行均为空(注意: my_table1中的字段不为空)。



图片:





我试图在Internet上搜索Advantage Data Architect的解决方案,但找不到解决方案。

解决方案

我假设您想要在MySQL中使用 GROUP_CONCAT 之类的东西code> string_agg 在Oracle / Postgres中。



通常的算法是这样的:

  DECLARE @S STRING; 

将游标C作为
选择
CONVERT(field_1,SQL_CHAR)AS val

my_table1;

OPEN C;

在抓取C的同时执行
-由于第一行的@S为NULL,因此这将确保
-结果不会以分隔符开头。
@S = COALESCE(@S +’,’+ C.val,C.val);
END;

CLOSE C;

SELECT @S;

可以在ADS论坛上找到常规功能。



PS:这与将a字符串分成单独的行


How can I concatenate rows in select query? (in Advantage Data Architect)

I tried run the following scripts:

The first script:

declare @str string;
set @str = '';
select @str = @str + field_1 from my_table1

But I get a result where all rows contain "false", like this picture:

Second script:

declare @str string;
select @str = coalesce(@str + ', ','') + field_1 from my_table1

This time, all rows are empty (note: the field from "my_table1" is not null).

Picture:

I tried to search the solution on the Internet for Advantage Data Architect, but I could not find a solution.

解决方案

I'm assuming you want something like GROUP_CONCAT in MySQL or string_agg in Oracle / Postgres.

The general algorithm for that is something like:

DECLARE @S STRING;

DECLARE CURSOR C AS 
SELECT
  CONVERT(field_1, SQL_CHAR) AS "val"
FROM 
  my_table1;

OPEN C;

WHILE FETCH C do
  -- Since @S is NULL for the first row this will ensure 
  -- that the result does not start with the separator.
  @S = COALESCE(@S + ', ' + C.val, C.val);
END;

CLOSE C;

SELECT @S;

A general function can be found on the ADS forum.

PS: This is the reverse of splitting a string into separate rows.

这篇关于连接选择查询中的行(在Advantage Data Architect中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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