Transform row into column and vice-versa using sql - oracle - oracle

I have this table:
create table history (
date_check DATE,
type VARCHAR2(30),
id_type NUMBER,
total NUMBER
)
Selecting.....
select * from history order by 1
DATE_CHECK TYPE ID_TYPE TOTAL
14/02/2016 abc 1 14
14/02/2016 abc33 1 14
14/02/2016 bbb 1 40
14/02/2016 bbb33 3 43
14/02/2016 ddd 2 61
14/02/2016 ddd33 2 62
15/02/2016 abc 1 33
15/02/2016 abc33 1 44
15/02/2016 bbb 1 55
15/02/2016 bbb33 3 66
15/02/2016 ddd 2 77
15/02/2016 ddd33 2 88
Type its always this 6 values:
abc
abc33
bbb
bbb33
ddd
ddd33
And I cross this data with "id_type" so there is a decode like this:
select type || decode(id_type, 1, '- new', 2, '- old', 3, '- xpto') as type from history order by 1
In the end I need something like this:
DATE_CHECK abc - new abc33 - old bbb - new bbb33 - old ....
14/02/2016 14 14 40 43
15/02/2016 33 44 55 66
What is the easiest way to do it? Using pivot?

try this:
with data as(
select date_check, type, total from (
select date_check, type || ' ' || decode(id_type, 1, '- new', 2, '- old', 3, '- xpto') as type, total from history
))
select * from data
pivot(
max(total) for type in ('abc - new', 'abc33 - new', 'bbb - new',
'bbb33 - xpto', 'ddd - old', 'ddd33 - old')
)
order by date_check;
And for the "vice versa" use UNPIVOT

You can reference multiple columns in a pivot statement to get your desired output. In your case you have a single analytic column (TOTAL) but multiple columns forming composite columns on which to perform the analytic function, you can use a pivot query like the following:
select *
from history
PIVOT ( max(TOTAL)
for (TYPE, ID_TYPE) in ( ('abc',1) abc_new
, ('abc',2) abc_old
, ('abc',3) abc_xpto
, ('abc33',1) abc33_new
, ('abc33',2) abc33_old
, ('abc33',3) abc33_xpto
, ('bbb',1) bbb_new
, ('bbb',2) bbb_old
, ('bbb',3) bbb_xpto
, ('bbb33',1) bbb33_new
, ('bbb33',2) bbb33_old
, ('bbb33',3) bbb33_xpto
, ('ddd',1) ddd_new
, ('ddd',2) ddd_old
, ('ddd',3) ddd_xpto
, ('ddd33',1) ddd33_new
, ('ddd33',2) ddd33_old
, ('ddd33',3) ddd33_xpto
)
)
You can adjust the output column headings to suite if desired by changing them similar to the following:
...
PIVOT ( max(TOTAL)
for (TYPE, ID_TYPE) in ( ('abc',1) "abc - new"
, ('abc',2) "abc - old"
, ('abc',3) "abc - xpto"
, ('abc33',1) "abc33 - new"
, ...

Related

Get Total count

I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32
Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!

How to redirect hive query output to text file with header and column name having space

I have hive able product with rating.
Id, productid, rating, ProdBarCode
42 96 5 881107178
168 151 5 884288058
110 307 4 886987260
58 144 4 884304936
62 21 3 879373460
279 832 3 881375854
237 514 4 879376641
I want to write a query find average product rating of product to pipe separated text file with header using hive -e"query" > output.txt
OUTPUT Format:-|Productid|average rating|
Solution:
hive -e " select C.value from (select 1 key, '|Productid|average rating|' value union all select 2 key , concat('|',concat_ws('|', Productid, averagerating),'|') value from (select CAST(A.productid AS STRING) AS Productid, CAST(A.averagerating AS STRING) AS averagerating from (select productid, avg(rating) averagerating from product group by productid sort by productid ) AS A where A.averagerating > 2) B sort by key) C " > output.txt
Is this query correct? Is there any other simple way to redirect the output in text file with header and column name having spaces (average rating)?
Any suggestion

HIVE : Replace string/ pattern in row if it exists else do nothing

I have a table A with id, name, age.
> id name age
> {20} Joan 12
> 3 James 12
> 12 Jill 12
> {54} Adam 12
> {10} Bill 12
I need to remove the {} surrounding 'id' field.
I tried this :
translate(regexp_extract(id, '([^{])([^}])', 2), '{', '')
which works but returned a null for values with NO {}.
id
3
12
Is there way I can get the output as ???
id
20
3
12
54
10
You could use the regexp_replace udf so as to remove the "{}" like :
select regexp_replace(id, '\\{|\\}','');
Please try the following select statement:
select regexp_replace(col1,'[{}]','') as replaced,col2,col3 from table_name;

oracle raw type iteration

I am working with a table that contains a raw(200) field. From my client application I manage to get the value and store it in a byte[] and so that I can loop over it and get all the samples.
My raw data would be like ...
2C2B2E2B2D2C2933283030332B2F2D302F2B272F312E2B2F2F28242A2F322E
... and from there I would like to go from hex to decimal values and get an array such as 44,43,46,43
However, I would like to do a similar thing in a procedure but I don't know how to iterate over a raw field or how to cast it to byte array.
I tried with UTL_RAW.CAST_TO_BINARY_INTEGER but that would only give me the first sample
Given this data ...
SQL> select col1
2 from t23
3 /
COL1
--------------------------------------------------------------------------------
32433242324532423244324332393333323833303330333332423246324433303246324232373246
33313245324232463246323832343241324633323245
SQL>
... a SELECT like this will produce the requisite output...
SQL> select regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
2 from t23
3 connect by level <= ceil(utl_raw.length(col1)/2)
4 /
REGEXP_SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(COL1),'([A-Z0-9]{2})',1,LEVEL)
--------------------------------------------------------------------------------
2C
2B
2E
2B
...
2B
2F
2F
28
24
2A
2F
32
2E
31 rows selected.
SQL>
Use TO_NUMBER with the 'XX' mask to convert the hex into decimal ...
SQL> select to_number(
2 regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
3 , 'XX')
4 from t23
5 connect by level <= ceil(utl_raw.length(col1)/2)
6 /
TO_NUMBER(REGEXP_SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(COL1),'([A-Z0-9]{2})',1,LEVEL),
--------------------------------------------------------------------------------
44
43
46
43
45
44
41
...
Finally, to populate an array, and populate it in PL/SQL with the bulk collection syntax:
create type int_nt as table of integer
/
declare
ints int_nt;
begin
select to_number(
regexp_substr(utl_raw.cast_to_varchar2(col1), '([A-Z0-9]{2})', 1, level)
, 'XX')
bulk collect into ints
from t23
connect by level <= ceil(utl_raw.length(col1)/2);
end;
/
Probably there's a more efficient way to solve this but I managed to get my result by using utl_raw.length and utl_raw.substr over my raw data and iterating with an standard plsql loop and converting each substring to decimal with utl_raw.cast_to_binary_integer

How can I get values from one table to another via similar values?

I have a table called excel that has 3 columns, name, id, and full_name. The name part is the only one I have and I need to fill id and full_name. The other table that contains the data is called tim_pismena and has 2 columns that I need, id and pismeno_name (the actual names are not important, but i'm writing them just for clarity). In pseudooracle code :) the select that gets me the values from the second table would be done something like this:
SELECT tp.id, tp.pismeno_name
FROM tim_pismena tp
WHERE upper(tp.pismeno_name) LIKE IN upper('%(SELECT name FROM excel)%')
and when used with an insert, the end result should be something like
name id full_name
Happy Joe 55 Very fun place Happy Joe, isn't it?
Use merge statement
1 MERGE
2 INTO excel tgt
3 USING tim_pismenae src
4 ON ( upper(src.naziv_pismena) LIKE '%'||upper(tgt.ime)||'%')
5 WHEN MATCHED
6 THEN
7 UPDATE
8 SET tgt.id = src.id
9 , tgt.full_name = src.naziv_pismena
10 WHEN NOT MATCHED
11 THEN
12 INSERT ( tgt.name
13 , tgt.id
14 , tgt.full_name )
15 VALUES ( src.naziv_pismena
16 , src.id
17 , src.naziv_pismena )
18 WHERE (1 <> 1);

Resources