当前位置:首页 » 编程语言 » sql列变行Oracle
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql列变行Oracle

发布时间: 2023-03-30 05:44:37

‘壹’ sql题:怎么把ORACLE数据库中的数据列统计转化成行显示

--将分组统计存入临时表
select substring([DATE],5,2),sum([IN]) into #tmp from 表 where substring([DATE],1,4)='1900' group by substring([DATE],5,2)

--生成行列转换的SQL语句
declare @sql varchar(1024)
set @sql = 'select '
select @sql = @sql + 'sum(case [DATE] when '''+[DATE]+''' then [IN] end) as '+[DATE] from (select distinct [DATE] from #tmp) as a
set @sql = @sql+' from #tmp'

--执行语句并清理临时表
exec(@sql)
drop table #tmp

‘贰’ oracle sql 列转行

使用NOT
IN
,我写一下给你,弊缺等等
SELECT
编号
FROM

WHERE
数据
NOT
IN
(数据='1'
and
数据=
'2'
and
数据=‘3’);
这样租陆辩你查出的结果是
数据那一项不在
123
这3个值中的
你看看能悉腔不能理解,可以继续追问

‘叁’ oracle行转列sql怎么写

行转列的准则就是通过主键进行分组,之后对行其它字段加上sum()、max()、count()函数,里边用decode()这类函数进行处理,总之分组不要用到他就成。

‘肆’ SQL语句怎么转成ORACLE语句

--定义参数
v_ids varchar(4000);

--列转行并将值写入参数
select to_char(wm_concat (MyColumn1)) into v_ids from MyTable;

‘伍’ oracle sql 中 如何实现table的行列转换

你所谓的行列转换应该是指纵表转横表,横表转纵表.
给你个例子
纵表转横表:
使用DECODE语句,可以很方便的将纵表转为横表,例子如下:
原表查询结果:
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10001 Computer Science 75
10000 Economics 66
我们要把各科成绩从同一列转到不同的列中去。
SQL>?select id,
sum(decode(major,’Computer Science’,current_credits,0)) cs,
sum(decode(major,’History’,current_credits,0)) his,
sum(decode(major,’Economics’,current_credits,0)) eco
from students
group by id
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
10001 75

横表转纵表:
使用 UNION 即可实现将横表转为纵表,以上面的表为例:
转换前:
ID CS HIS ECO
------ ----------- ------------- -----
10000 98 88 66
SQL>?select id, ’Computer Science’ major,cs current_credits
from students
union
select id, ’History’ major,his current_credits
from students
union
select id, ’Economics’ major,eco current_credits
from students
ID MAJOR CURRENT_CREDITS
------ ---------------- ---------------
10000 Computer Science 98
10000 History 88
10000 Economics 66