CREATE TABLE score ( name NVARCHAR(20), subject NVARCHAR(20), score INT ) --2.插入测试数据 INSERT INTO score(name,subject,score) VALUES('张三','语文',98) INSERT INTO score(name,subject,score) VALUES('张三','数学',80) INSERT INTO score(name,subject,score) VALUES('张
今天有个同事找我,他说他有个需求,需要进行行转列,但是又跟一般的行转列有些区别,具体需求如下所说,需要将表1的数据转换为表2的显示格式. 我想了一下,给出了一个解决方法,具体如下所示(先给出测试数据) INSERT INTO TEST SELECT 1, 1, '定型名称', '预定型' UNION ALL SELECT 1, 2, '进布方式', '调平' UNION ALL SELECT 1, 3,
原帖:http://www.cnblogs.com/nayitian/p/3231734.html wmsys.wm_concat Definition: The Oracle PL/SQL WM_CONCAT function is used to aggregate data from a number of rows into a single row, giving a list of data associated with a specific COMMENT_BODY. In ef
表结构和数据如下(表名Test): NO VALUE NAME 1 a 测试1 1 b 测试2 1 c 测试3 1 d 测试4 2 e 测试5 4 f 测试6 4 g 测试7 Sql语句: select No, ltrim(max(sys_connect_by_path(Value, ';')), ';') as Value, ltrim(max(sys_connect_by_path(Name, ';')), ';') as Name from (select No, Value, Name,
oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' || ELEMENT) ELEMENT FROM T_MERCH_ELEMENT ' group by CODE_TS; 得到的结果: 上面大家可能会发现序号没有按顺序排列下来.如果没有要求,就这样就可以了.如果要排序看方法二. 方法二:WM_CONCAT OVER 有特殊数据时会报错.报:“操
这里行转列的基本思想就是使用max,因为其他列下面都是NULL,所以可以Max最后就只能得到有值的这行 普通的查询: SELECT icd , case when (ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ )) = then SLIDE_QTY END as SLIDE_QTY1, case when (ROW_NUMBER() OVER(PARTITION BY INNER_CD ORDER BY SLIDE_SEQ
行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nation
SQL中只有两列数据(字段1,字段2),将其相同字段1的行转列 转换前: 转换后: --测试数据 if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([MDF_LOT_NO] int,[ERP_MODE_CD] int) Insert #T , union all , union all , union all , Go --测试数据结束 DECLARE @name VARCHAR(max),@sql
这就是典型的行转列问题. 首先说下最简单的思路 用union all select year,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4 from ( select year,count m1,0 m2,0 m3,0 m4 from atest where month = 1 union all select year,0 m1,count m2,0 m3,0 m4 from atest where month = 2 union all selec