CREATE TABLE temp
(
DEPTNO int,
ENAME varchar(20),
SEX varchar(20),
AGE varchar(20)
);
insert into temp values(10,'aaa','F','18');
insert into temp values(10,'bbb','F','19');
insert into temp values(10,'ccc','F','20');
insert into temp values(20,'ddd','M','21');
insert into temp values(20,'eee','M','22');
insert into temp values(20,'fff','M','23');
insert into temp values(30,'hhh','X','24');
insert into temp values(30,'ggg','X','25');
insert into temp values(30,'yyy','X','26');
insert into temp values(30,'ttt','X','27');
要求结果如下:
10|F| aaa bbb ccc
20|M| ddd eee fff
30|X| ggg hhh ttt yyy
用Oracle的语句在 Toad中运行DB2 数
据库! 老报错!求修改!或者其他的实现SQL语句? 多谢!
SELECT deptno,
SYS_CONNECT_BY_PATH (ename, ' ') AS ename,
sex,
SYS_CONNECT_BY_PATH (age, ' ') AS age
FROM (SELECT deptno,
ename,
sex,
age,
RANK () OVER (ORDER BY deptno)
+ ROW_NUMBER () OVER (ORDER BY deptno)
rn,
ROW_NUMBER () OVER (PARTITION BY deptno ORDER BY deptno) rm
FROM temp) a1
WHERE a1.ROWID IN (SELECT MAX (a2.ROWID)
FROM temp a2
WHERE a2.deptno = a1.deptno)
START WITH rm = 1
CONNECT BY PRIOR rn = rn - 1
昨天试了一下,还是用递归来做!
WITH Recursive_Test_Par(deptNo,ename,sex,age,Rk_Num) as(
SELECT deptNo,ename,sex,age,ROW_NUMBER() OVER(PARTITION BY deptno) --分组,生成序列,自我关联之用
FROM temp
),
Ename_Join(deptNo,ename,sex,age,R_Num) as(
SELECT deptno,CAST(ename AS VARCHAR(100)),sex,age,Rk_Num from Recursive_Test_Par WHERE Rk_Num=1
UNION ALL
SELECT a1.deptNo,CAST(a1.ename||'#'||b1.ename AS VARCHAR(100)),a1.sex,a1.age,a1.R_Num+1
from Ename_Join a1,Recursive_Test_Par b1
WHERE a1.deptNo=b1.deptNo and a1.R_Num=b1.Rk_Num-1
)
SELECT a.deptNo,a.ename,a.sex,a.age FROM Ename_Join a INNER JOIN
(SELECT deptNo,max(R_Num) R_Num from Ename_Join
GROUP BY deptNo) b
ON a.deptNo=b.deptNo and a.R_Num=b.R_Num;
结果如下:
没有报错!
DB2,实现查询结果中多行字段合并到一行上的SQL语句写法 ?
db2 =>
db2中怎么用SQL将一张表中的两条数据合并成一行数据?
英语 as [英语(一)], 时间 as [时间(一)], null as [数学(二)], null as [
db2 中 多列合并成一列
select 'a'||','||'b'||','||'c' from sysibm.sysdummy1 ;列的修改要注意只能连接字符串
请教关于DB2中多字段使用IN查询的问题
select * from PERSON where (PERSONID,NAME) in (values ('1', '管理员1'), ('2', '管理员2'), ('3', '管理员3'));
DB2中merge相关用法,请举例说明
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。 下面看个具体的例子: SQL> CREATE TABLE T AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A;表已...
DB2数据库问题,列转行问题
我就不管你前面的结果是怎么查出来的,你就以你前面结果为基本进行查询 with a1 as (select row_number() over(partition by name order by type) seq,a.username,a.loginid,substr(a.orgcode,1,6) zq,a.orgcode,a.userstatus,a.mobile,c.rolename from s_u_user a left join s_u_...
sql查询结果拼接
1 王五,小明 2 李四,小赵,张三 (2 行受影响)
sql语句合并数据
理论上讲 楼主的写法 性能比楼下的好 然后楼主要明白 union和union all的区别 我们一般用后者居多 union包含了去重的功能 union all就是连接所有的数据 不考虑重复值 性能要比union好 有什么疑问可以随时问我 希望采纳
SQL语句,在同一张表中把三个条件查询的三个结果组合到一起,并加起来...
from (select count(PICTURE)*5 PICTURE from "DB2ADMIN"."C_NEWS" where PICTURE is not null union all select count(*)*5 DETAILS from "DB2ADMIN"."C_NEWS" where length(DETAILS)<1000 and="">0 union all select count(*)*10 DETAILS from "DB2ADMIN"."C_NEWS" where length(...
mysql怎么把固定的几个字符串,转成列的形式作为临时表查询
db2数据库 有个values函数可用于你说的这种操作,但是mysql本身是 没有这样的函数的。下面是取巧的一种写法,也能得到你想要的数据。如图:select trim(substring_index(substring_index(a.num,',',b.help_topic_id + 1),',' ,- 1)) AS numbfrom (select '39982665399,39023431098,39764974...