Solved

Operation between hexadecimal data Oracle

Posted on 2014-04-22
9
441 Views
Last Modified: 2014-04-30
HI experts, I need resolve the next situation:

I have the next twelve rows in a table A_5MIN_TST1 (the data to be compared are hexa, but Examples works with decimal values):
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

Doing a select I must analyze each bit of the tlq_inst column (hexadecimal data) and to decide:
if some value of tlq_inst is
= 8
or
= 32
or
= 128
then write = 8
when tlq_inst doesn't is 8, 32, 128 then write the first value of tlq_inst, over the range.
I have tried with this query:
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
;

Open in new window

And I saw:
SALIDA
Q
Q
Q
Q
OK
Q
Q
Q
Q
Q
Q
Q

Open in new window

Resuming, on these 12 values, I need to do:

Get 'Q' if the comparison condition with mask, is met.

Get the first value of tlq_inst, when the comparison with the mask, is NOT true.

If possible, do the same but inside where

With this query I managed to get 12 values, but I need to get only one.

Could you help me to resolve this problem?

Thanks all in advanced
Regards
0
Comment
Question by:carlino70
  • 5
  • 2
  • 2
9 Comments
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
do select TOP 1
0
 

Author Comment

by:carlino70
Comment Utility
the sentence "select top" does not exist in Oracle.
0
 
LVL 15

Expert Comment

by:Ess Kay
Comment Utility
sorry, forgot about that.

If you want just a first selected row you can:

select fname from MyTbl where rownum = 1
you can also use analytic functions to order and take the top x

select max(fname) over (rank() order by some_factor) from MyTbl
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
Comment Utility
With this query I managed to get 12 values, but I need to get only one.
So you're supposed to get just one row as result?! What exactly do you expect as result?!

@esskayb2d: Don't get me wrong + I really don't want to be too offensice, BUT please -if you do not exactly know what the asker wants AND obviously do not have or just have half knowledge about Oracle (RDBMS), please stick to your topics, where you are truly powerful (and I mean this since I viewed your profile + your answered threads)...
If you want just a first selected row you can:

select fname from MyTbl where rownum = 1

Yes, you'd get just one row. But within a table holding billions of rows, which row is returned??!! Without an "order by" (e.g.) this is totally undetermined!

you can also use analytic functions to order and take the top x

select max(fname) over (rank() order by some_factor) from MyTbl
Sorry, but this is far too vague ;-)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:carlino70
Comment Utility
"   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')  "

Open in new window


I have 12 rows.

If there is any value is in mask, then I must write :  'Q' (in hexa: 0000000000000008, in decimal: 8)

If any value doesn't is in mask, then I must write the first value of "tlq_inst"

Both cases, I need just 1 value

note: I'm using 'Q' and 'OK', just for testing
0
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 500 total points
Comment Utility
I suppose you have to use analytic window functions for this, like:
select DECODE(POWER(2, BITAND(tlq_inst, 168)), 1, 'OK', 'Q') salida,
       case DECODE(POWER(2, BITAND(tlq_inst, 168)), 1, 'OK', 'Q')
        when
        'OK' then to_char(first_value(tlq_inst) ignore nulls over (partition by null order by null))
        else
        'Q'
       end testit,
       A_5MIN_TST1.*
  from A_5MIN_TST1
 where utctime >= TO_DATE('01/08/2013 01:00:00', 'dd/mm/yyyy hh24:mi:ss')
   and utctime < TO_DATE('01/08/2013 02:00:00', 'dd/mm/yyyy hh24:mi:ss');

Open in new window


Whereas you should adjust the "partition by" and "order by" within the window function to suit your needs/table specs...

As you might have seen, I also changed your "where utctime >= TO_DATE('01/08/2013 01:00:01', 'dd/mm/yyyy hh24:mi:ss')" to "where utctime >= TO_DATE('01/08/2013 01:00:00', 'dd/mm/yyyy hh24:mi:ss')": don't miss any data ;-)
0
 

Author Comment

by:carlino70
Comment Utility
Good, I'll try this.

Thanks for the recomendation, I going put the time in correct way
0
 

Author Comment

by:carlino70
Comment Utility
With a little change, the solution was:
select 
  case when max(bitand(tlq_inst, 168)) = 0 then 
    max(tlq_inst) keep (dense_rank first order by utctime)
  else
    8
  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;

Open in new window

Thanks  Alexander Eßer for your help, was very important.
Regards
0
 

Author Closing Comment

by:carlino70
Comment Utility
Excellent!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now