oracle行转列sql
Ⅰ oracle中如何写sql实现行列转换
问下是不是课程只有语文 ,数学,英语没有别的?
如果是的话那么:
数据库是sqlserver的话执行:
select b.stu_name,
max(case a.subject when '语文' then a.grade else '' end) as 语文,
max(case a.subject when '数学' then a.grade else '' end) as 数学,
max(case a.subject when '英语' then a.grade else '' end) as 英语
from stu_grade a,stu_master b
where a.stu_no=b.stu_no
group by b.stu_name
数据库为oralce的话执行
select b.stu_name,
max(case a.subject when '语文' then to_char(a.grade) else '' end) as 语文,
max(case a.subject when '数学' then to_char(a.grade) else '' end) as 数学,
max(case a.subject when '英语' then to_char(a.grade) else '' end) as 英语
from stu_grade a,stu_master b
where a.stu_no=b.stu_no
group by b.stu_name
Ⅱ oracle 行转列 动态的考试科目,不用存储过程,一条sql能写出来么,怎么写!在线等!!
这是我前两天为另一个哥们写的
test_bak 的数据
1 a,b,c
2 d,e
转换后的数据为
1 a
1 b
1 c
2 d
2 e
不知道是不是你想要的,如果是列转行,oracle 10G以上可用此函数wm_concat
select cc.*,
substr(cc.b,
decode(r_id, 1, 1, instr(cc.b, ',', 1, cc.r_id - 1) + 1),
instr(cc.b, ',', 1, cc.r_id) -
decode(r_id, 1, 1, instr(cc.b, ',', 1, cc.r_id - 1) + 1))
from (select row_number() over(partition by c order by c) r_id,
b || ',' b,
c
from test_bak
connect by c = prior c
AND prior dbms_random.VALUE IS NOT NULL
and level <= length(b) - length(replace(b, ',', '')) + 1) cc;
Ⅲ oracle 行转列
select
a.DEPARTNAME门店,
b.DEPARTNAME片区,
c.DEPARTNAME区域,
d.DEPARTNAME总公司
from表名a
innerjoin表名bona.PARENTDEPARTMODELID=a.DEPARTMODEID
innerjoin表名conb.PARENTDEPARTMODELID=c.DEPARTMODEID
innerjoin表名donc.PARENTDEPARTMODELID=d.DEPARTMODEID
Ⅳ 【求助】有关oracle 动态行转列
select*fromcjb.test
createtablecjb.test
(a01varchar2(20),
a02varchar2(20),
a03varchar2(20)
)
selecta.*,a.rowidfromcjb.testa
selecta.a01,
case
whenwm_concat(a.a02||'&'||a.a03)like'%math%'then
(length(wm_concat(a.a02||'&'||a.a03))-
length(replace(wm_concat(a.a02||'&'||a.a03),'math','')))/4else0
endasmath,
case
whenwm_concat(a.a02||'&'||a.a03)like'%chinese%'then
(length(wm_concat(a.a02||'&'||a.a03))-
length(replace(wm_concat(a.a02||'&'||a.a03),'chinese','')))/7else0
endaschinese
fromcjb.testa
groupbya.a01
科目多就往上添加casewhen就可以了
Ⅳ oracle行转列写法,麻烦大家帮忙写个sql,谢谢
可以用wm_concat函数先把数据变成行显示,然后再通过截取来显示具体的月份,wm_concat转换如下
selectcompay_namecn,wm_concat(income)ic
from(selectcompay_name,sum(income)income,substr(time,1,6)time
fromincome
---wheretime>=start_monthandtime<=end_month
groupbycompay_name,substr(time,1,6)
orderbycompay_name,substr(time,1,6))
groupbycompay_name;
Ⅵ ORAClE sql如何实现行转列
如果“站名”、“条码”、“时间”都是一样的话,可以这么写:
with
t_temp as (select row_number() over (partition by station_name order by param_name asc) id, t.* from t),
t_temp1 as (select * from t_temp where id = 1),
t_temp2 as (select * from t_temp where id = 2),
t_temp3 as (select * from t_temp where id = 3)
select '站名' col1, '条码' col2, t_temp1.参数名 col3, t_temp2.参数名 col4, t_temp3.参数名 col5, '时间' col6
from t_temp1, t_temp2, t_temp3
where t_temp1.站名 = t_temp2.站名
and t_temp2.站名 = t_temp3.站名
union all
select t_temp1.站名, t_temp1.条码, to_char(t_temp1.数值), to_char(t_temp2.数值), to_char(t_temp3.数值), to_char(t_temp1.时间)
from t_temp1, t_temp2, t_temp3
where t_temp1.站名 = t_temp2.站名
and t_temp2.站名 = t_temp3.站名
Ⅶ oracle/sql/toad语句怎么写,行转列
select 姓名,地址, wmsys.WM_CONCAT(VALUE1),max(电话) 编号 from 表A GROUP BY 姓名,地址
看max(电话)
Ⅷ oracle的sql语句列转行
不同人的uuid是不一样的吗?
select
(selectzfromtabnameaawherezmc='姓名'andaa.uuid=a.uuid)姓名,
(selectzfromtabnameaawherezmc='年龄'andaa.uuid=a.uuid)年龄,
(selectzfromtabnameaawherezmc='英文名称'andaa.uuid=a.uuid)英文名称,
(selectzfromtabnameaawherezmc='性别'andaa.uuid=a.uuid)性别,
(selectzfromtabnameaawherezmc='入职日期'andaa.uuid=a.uuid)入职日期,
(selectzfromtabnameaawherezmc='个人信息'andaa.uuid=a.uuid)个人信息
from(selectdistinctuuidfromtabname)a
Ⅸ oracle 数据库,写了一个行转列的sql,但就是报错,不知道哪里错了。还请大家帮忙看看
Sum VALDATA
没括号?SUM(VALDATA)
还有pivot是11g的函数,确保数据库是11g或以后版本