BITAND SQL - 十六进制数据之间的操作Oracle

时间:2014-04-22 19:32:56

标签: sql oracle function hex

我需要解决以下问题:

我在表A_5MIN_TST1中有以下12行(要比较的数据是hexa,但示例使用十进制值):

UTCTIME|TLQ_INST
01/08/2013 01:05:00 a.m.|32
01/08/2013 01:10:00 a.m.|128
01/08/2013 01:15:00 a.m.|8
01/08/2013 01:20:00 a.m.|32
01/08/2013 01:25:00 a.m.|1
01/08/2013 01:30:00 a.m.|10
01/08/2013 01:35:00 a.m.|100
01/08/2013 01:40:00 a.m.|1000
01/08/2013 01:45:00 a.m.|2000
01/08/2013 01:50:00 a.m.|3000
01/08/2013 01:55:00 a.m.|4000

做一个选择我必须分析tlq_inst列的每一位(十六进制数据)并决定:

如果tlq_inst的某个值是 = 8 要么 = 32 要么 = 128 然后写= 8。 当tlq_inst不是8,32,128时,则在该范围内写入tlq_inst的第一个值。

我尝试过这个查询:

SELECT DECODE(POWER(2,BITAND(tlq_inst, 168)), 1, 'OK','Q') salida
FROM A_5MIN_TST1
WHERE utctime >= TO_DATE ('01/08/2013 01:00:01','dd/mm/yyyy hh24:mi:ss')
AND utctime < TO_DATE ('01/08/2013 02:00:00','dd/mm/yyyy hh24:mi:ss')
AND POINTNUMBER = 330062;

我收到了这些结果:

SALIDA
Q
Q
Q
Q
OK
Q
Q
Q
Q
Q
Q
Q

恢复,在这12个值上,我需要做:

  • 获取问题&#39;如果满足与掩码的比较条件。
  • 当与掩码进行比较时,获取tlq_inst的第一个值为true。

如果可能的话,在

的内部做同样的事情

通过这个查询,我设法获得了12个值,但我只需要一个。

你能帮我解决这个问题吗?

CREATE TABLE A_5MIN_TST1
(
  UTCTIME      DATE                             NOT NULL,
  POINTNUMBER  INTEGER                          NOT NULL,
  SITEID       INTEGER,
  VALOR_INST   FLOAT(126),
  TLQ_INST     INTEGER,
  VALOR_PROM   FLOAT(126),
  TLQ_PROM     INTEGER,
  VALOR_MAX    FLOAT(126),
  TLQ_MAX      INTEGER,
  UTCTIME_MAX  DATE,
  VALOR_MIN    FLOAT(126),
  TLQ_MIN      INTEGER,
  UTCTIME_MIN  DATE
)
TABLESPACE USERS
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
           )
LOGGING 
NOCOMPRESS 
NOCACHE
NOPARALLEL
MONITORING;


ALTER TABLE A_5MIN_TST1 ADD (
  PRIMARY KEY
 (UTCTIME, POINTNUMBER)
    USING INDEX 
    TABLESPACE USERS
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          64K
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
               ));


SET DEFINE OFF;
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:05:00', 'MM/DD/YYYY HH24:MI:SS'), 32);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:10:00', 'MM/DD/YYYY HH24:MI:SS'), 128);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:15:00', 'MM/DD/YYYY HH24:MI:SS'), 8);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:20:00', 'MM/DD/YYYY HH24:MI:SS'), 32);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:25:00', 'MM/DD/YYYY HH24:MI:SS'), 1);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:30:00', 'MM/DD/YYYY HH24:MI:SS'), 10);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:35:00', 'MM/DD/YYYY HH24:MI:SS'), 100);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:40:00', 'MM/DD/YYYY HH24:MI:SS'), 1000);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:45:00', 'MM/DD/YYYY HH24:MI:SS'), 2000);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:50:00', 'MM/DD/YYYY HH24:MI:SS'), 3000);
Insert into A_5MIN_TST1
   (UTCTIME, TLQ_INST)
 Values
   (TO_DATE('08/01/2013 01:55:00', 'MM/DD/YYYY HH24:MI:SS'), 4000);
COMMIT;

1 个答案:

答案 0 :(得分:0)

这是一个语句,当至少一个记录与位掩码匹配时给你Q,否则给出最早的TLQ_INST。它使用KEEP DENSE_RANK。它通过utctime对记录进行排序,获取最早的记录并返回该记录的tlq_inst。如果有更多记录具有相同的最早时间,则返回这些记录的最大tlq_inst。

select 
  case when max(bitand(tlq_inst, 168)) = 0 then 
    max(tlq_inst) keep (dense_rank first order by utctime)
  else
    'Q'
  end as result
from a_5min_tst1
where utctime >= to_date ('01/08/2013 01:00:01','dd/mm/yyyy hh24:mi:ss')
and utctime < to_date ('01/08/2013 02:00:00','dd/mm/yyyy hh24:mi:ss')
and pointnumber = 330062;