原表名字:test
高密ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
三個(gè)字段:姓名:nm,選修課:xx,成績(jī):cj
分兩張情況:
一、選修科目數(shù)量確定為2:
兩種寫(xiě)法:
1、普通寫(xiě)法
with?t?as
(select?nm,
'選修'?||?row_number()?over(partition?by?nm?order?by?cj)?xx_tp,
'成績(jī)'?||?row_number()?over(partition?by?nm?order?by?cj)?cj_tp,
xx,
cj
from?test?a)
select?nm?"姓名",
max(decode(xx_tp,?'選修1',?xx,?null))?"選修1",
max(decode(cj_tp,?'成績(jī)1',?cj,?null))?"成績(jī)1",
max(decode(xx_tp,?'選修2',?xx,?null))?"選修2",
max(decode(cj_tp,?'成績(jī)2',?cj,?null))?"成績(jī)2"
from?t
group?by?nm
2、pivot
with?t?as
(select?nm,
'選修'?||?row_number()?over(partition?by?nm?order?by?cj)?xx_tp,
'成績(jī)'?||?row_number()?over(partition?by?nm?order?by?cj)?cj_tp,
xx,
cj
from?test?a)
select?nm?"姓名",?max(xx1)?"選修1",?max(cj1)?"成績(jī)1",?max(xx2)?"選修2",?max(cj2)?"成績(jī)2"
from?(select?*
from?t
pivot(max(xx)
for?xx_tp?in('選修1'?xx1,?'選修2'?xx2)))?a
pivot?(max(cj)?for?cj_tp?in('成績(jī)1'?cj1,?'成績(jī)2'?cj2))
group?by?nm
二、選修科目數(shù)量不確定
首先去?;tid=1609939extra=highlight=%B6%AF%CC%AC%D0%D0%D7%AA%C1%D0page=1 ? 復(fù)制動(dòng)態(tài)行轉(zhuǎn)列的代碼到sql窗口中運(yùn)行,然后執(zhí)行如下代碼:
with?t?as
(select?*
from?table(pivot('select?nm,
''成績(jī)''?||?row_number()?over(partition?by?nm?order?by?cj)?cj_tp,
cj
from?test?a'))),
t1?as
(select?*
from?table(pivot('select?nm,
''選修''?||?row_number()?over(partition?by?nm?order?by?cj)?cj_tp,
xx
from?test?a')))
select?*?from?t,?t1?where?t.nm?=?t1.nm
以上。
oracle數(shù)據(jù)多行不同列進(jìn)行合并顯示
select id ,listagg( name, ',' ) within group ( order by id ) as name from TABLE_NAME GROUP BY id;
單純的select a||b肯定是錯(cuò)的,你得有個(gè)相同的字段才能進(jìn)行合并啊
所以你得把兩個(gè)表做個(gè)子查詢加上一個(gè)行號(hào),然后用兩個(gè)行號(hào)做關(guān)聯(lián)才行
select name,
max(case when course = '語(yǔ)文' then course else null end) course1,
max(case when course = '語(yǔ)文' then score else null end) score1,
max(case when course = '數(shù)學(xué)' then course else null end) course2,
max(case when course = '數(shù)學(xué)' then score else null end) score2,
max(case when course = '英語(yǔ)' then course else null end) course3,
max(case when course = '英語(yǔ)' then score else null end) score3
from table
group by name