一个简单的select语句编写,在线等待,解决马上给分...
select S_ID,S_STATUS,P_KIND,BUS_STRING from EHT
id 状态1 状态2 功能串(50位)
1 1 1 00000000100000000000100000000000000000000000000000
现在具体需求如下:
S_STATUS:如果是1 10 11 状态统一为1
否则为0
P_KIND:如果是1 状态为1
如果是0 5 状态为0
问题一:功能串为50位,把刚才的放至49,50位上去
例如
id 状态1 状态2 功能串(50位)
1 1 1 00000000100000000000100000000000000000000000000000
就变为
id 状态1 状态2 功能串(50位)
1 1 1 00000000100000000000100000000000000000000000000011
问题二:或者把刚才的补至51,52位上去,功能串由50位变为52委
id 状态1 状态2 功能串(52位)
1 1 1 0000000010000000000010000000000000000000000000000011
问题一40分,问题二60分
------解决方案--------------------问题一:
select S_ID, decode(S_STATUS,1,1,decode(S_STATUS,10,1,decode(S_STATUS,11,1,0))) as S_STATUS,
decode(P_KIND,1,1,decode(P_KIND,0,0,decode(P_KIND,5,0,P_KIND))) as P_KIND,
case when decode(S_STATUS,1,1,decode(S_STATUS,10,1,decode(S_STATUS,11,1,0))) = 1
then substr(BUS_STRING,0,48) || '11 '
else BUS_STRING
end case as BUS_STRING
from EHT;
------解决方案--------------------1. SELECT substr(BUS_STRING,1, 48)||decode(s_status, 1, 1, 10, 1, 11, 1, 0)||decode(p_kind, 1, 1, 0) FROM EHT
------解决方案--------------------sql> create table EHT(s_id number,s_status number, p_kind number, bus_string varchar2(50));
SQL> DESC EHT;
Name Type Nullable Default Comments
---------- ------------ -------- ------- --------
S_ID NUMBER Y
S_STATUS NUMBER Y
P_KIND NUMBER Y
BUS_STRING VARCHAR2(50) Y
SQL>
SQL> INSERT INTO EHT SELECT 1,1,1, '00000000100000000000100000000000000000000000000000 ' FROM DUAL;
1 row inserted
SQL> INSERT INTO EHT SELECT 2,10,5, '00000000100000000000100000000000000000000000000000 ' FROM DUAL;
1 row inserted
SQL> SELECT * FROM EHT;
S_ID S_STATUS P_KIND BUS_STRING
---------- ---------- ---------- --------------------------------------------------
1 1 1 00000000100000000000100000000000000000000000000000
2 10 5 00000000100000000000100000000000000000000000000000