oracle 交叉報表固定列實現方法
需求:
統計某些天內每個型別的數量,
原資料 mctype 型別:dth,med,oth
tday時間劃分:t1,t2,t3,t4~t10
原始data
如果dthind=1 那麼這條記錄型別就是dth型別,其他類推
iddthind
medind
othind
1600672301
01600673001
01600673501
01600674410
01600675710
01600677210
01600700901
0 使用交叉報表後show 出的data
mctype
t1t2
t3t4~t10
dth100
2med00
13但是clients 需求是不管是否有data,都要show 出全部型別,全部日期
期望結果是
mctype
t1t2
t3t4~t10
dth100
2med00
13oth000
0 原**貼上**
select mctype,
count(decode(tdate,'t1','t1',null)) as t1,
count(decode(tdate,'t2','t2',null)) as t2,
count(decode(tdate,'t3','t3',null)) as t3,
count(decode(tdate,'t4~t10','t4',null)) as
"t4~t10"
from (
select
case
when sc.dthind=1
then
'dth'
when sc.medind=1
then
'med'
when sc.othind=1
then
'oth'
else
null
endas mctype,
case
when sc.crtdate >=(select bizdate from sysinfo) and sc.crtdate<=(select bizdate+1
from sysinfo) then
't0'
when sc.crtdate >=(select bizdate-1
from sysinfo) and sc.crtdate<=(select bizdate from sysinfo) then
't1'
when sc.crtdate >=(select bizdate-2
from sysinfo) and sc.crtdate<=(select bizdate-1
from sysinfo) then
't2'
when sc.crtdate >=(select bizdate-3
from sysinfo) and sc.crtdate<=(select bizdate-2
from sysinfo) then
't3'
when sc.crtdate >=(select bizdate-10
from sysinfo) and sc.crtdate<=(select bizdate-3
from sysinfo) then
't4~t10'
else
null
endas tdate
from cccbsc sc where sc.clmnum in ('16006772','16007009','16006744','16006757','16006723','16006730','16006735')
and crtdate >=(select bizdate - 10
from sysinfo)
and crtdate < (select bizdate from sysinfo)) group
by mctype
實現方法是
union all 所有的行和列一遍,這裡是3*4,也就是union all 12條記錄,然後所有統計的數量
減一條
select mctype,
count(decode(tdate,'t1','t1',null))-1
as t1,
count(decode(tdate,'t2','t2',null))-1
as t2,
count(decode(tdate,'t3','t3',null))-1
as t3,
count(decode(tdate,'t4~t10','t4',null))-1
as"t4~t10"
from (
select
case
when sc.dthind=1
then
'dth'
when sc.medind=1
then
'med'
when sc.othind=1
then
'oth'
else
null
endas mctype,
case
when sc.crtdate >=(select bizdate from sysinfo) and sc.crtdate<=(select bizdate+1
from sysinfo) then
't0'
when sc.crtdate >=(select bizdate-1
from sysinfo) and sc.crtdate<=(select bizdate from sysinfo) then
't1'
when sc.crtdate >=(select bizdate-2
from sysinfo) and sc.crtdate<=(select bizdate-1
from sysinfo) then
't2'
when sc.crtdate >=(select bizdate-3
from sysinfo) and sc.crtdate<=(select bizdate-2
from sysinfo) then
't3'
when sc.crtdate >=(select bizdate-10
from sysinfo) and sc.crtdate<=(select bizdate-3
from sysinfo) then
't4~t10'
else
null
endas tdate
from cccbsc sc where sc.clmnum in ('16006772','16007009','16006744','16006757','16006723','16006730','16006735')
and crtdate >=(select bizdate - 10
from sysinfo)
and crtdate < (select bizdate from sysinfo)
union
allselect
'dth'
as mctype,'t1'
as tdate from dual
union
allselect
'dth'
as mctype,'t2'
as tdate from dual
union
allselect
'dth'
as mctype,'t3'
as tdate from dual
union
allselect
'dth'
as mctype,'t4~t10'
as tdate from dual
union
allselect
'med'
as mctype,'t1'
as tdate from dual
union
allselect
'med'
as mctype,'t2'
as tdate from dual
union
allselect
'med'
as mctype,'t3'
as tdate from dual
union
allselect
'med'
as mctype,'t4~t10'
as tdate from dual
union
allselect
'oth'
as mctype,'t1'
as tdate from dual
union
allselect
'oth'
as mctype,'t2'
as tdate from dual
union
allselect
'oth'
as mctype,'t3'
as tdate from dual
union
allselect
'oth'
as mctype,'t4~t10'
as tdate from dual) group
by mctype
;
原理其實就是group by 的時候讓每個行和列都有值,然後show的時候再把加多的減去就可以了。
如果有什麼其他簡單的方法可以固定行和列也可以告訴我,謝謝
select語句實現交叉報表
1.交叉報表是什麼?呵呵,看看下面 乙個的問題就知道了 請教大家乙個問題 sql select 查詢的 表id name kechen fengshu 1 張三 數學 56 2 張三 語文 67 3 張三 化學 87 4 李四 語文 24 5 王五 化學 54 通過select 語句得到 下面的結果...
ORACLE表查詢結果轉置(固定列)
為乙個可編輯grid,對應這樣的grid資料庫有兩種設計方式,方式1 將資料庫表中的每乙個列和grid中的每乙個列對應 方式2 可編輯部份每乙個單元格做乙個資料庫表的行,既採用縱表的方式儲存資料,當查詢時把縱表結果轉置90度既可。兩種方式對比 方式1 和grid中對應一致,對於資料庫設計來說比較直觀...
CSS樣式實現表頭和列固定
效果圖 第一行和第一列固定 css實現表頭與列固定 title script src script style main td,th thtable td first child,th first child thead tr th th first child style head body di...