我有两个表,table1包含quater和week值,table2包含数据,我想根据table1的值显示最后四个quater数据
例如
表1(只有单行)
Week Quater
W3 Q22015
表2
Quater Column1 ColumnN
Q12015 X11 Y11
Q12014 X1 Y1
Q22014 X2 Y3
Q32014 X3 Y4
Q42014 T1 Y5
Q42013 3 Y7
Q32013 5 7
Q22013 6 8
输出数据(因为table1具有Q22015,所以最后四个季度的数据将是
Q12015
Q42014
Q32014
Q22014
表3
Quater Column1 ColumnN
Q12015 X11 Y11
Q22014 X2 Y3
Q32014 X3 Y4
Q42014 T1 Y5
我也可以在Quater和Week上同时应用过滤器,如果我的表有周也像W1,W2,W3等我要显示最后四个季度的数据加上数据一周,这在表1中提到
由于
答案 0 :(得分:0)
您应该以最恰当的方式存储quarted的值,以便轻松选择 但是站在实际情况你可以操纵字符串并选择这种方式
select Quater , Column1 , ColumnN
from my_table
where concat(substr(quater, -4 ), substr(quater,2,1))<=
(select concat(substr(quater, -4 ), substr(quater,2,1) from table1)
and rownum <5
order concat(substr(quater, -4 ), substr(quater,2,1))
并获取与最后4个季度相关的所有结果
select Quater , Column1 , ColumnN
from my_table
where quater in (
select quater from my_table
where concat(substr(quater, -4 ), substr(quater,2,1)) <=
(select concat(substr(quater, -4 ), substr(quater,2,1) from table1)
and rownum <5
order concat(substr(quater, -4 ), substr(quater,2,1))
)
order concat(substr(quater, -4 ), substr(quater,2,1))