I have been learning regexp_replace to clean up my dataset and have come across a new problem.
I have a dataset that looks like this:
$14..89
30 0
40 -
56 e
37.5
0.25
16.8.9
I want the data set to look like this:
14.89
30
40
56
37.5
0.25
16.89
I have used the following codes:
SELECT CAST(REGEXP_REPLACE('30 0', '(\.)\.+|[^0-9.]', '\1\2\3') AS DECIMAL(16,2)) from dual;
the problem with the first code is, although it works on the other numbers, it turns 30 0 into 300, when it should be 30
SELECT CAST(REGEXP_REPLACE('.25', '(\.)\.+|[^0-9]|(\w+).*$', '\1\2\3') AS DECIMAL(16,2)) from dual;
SELECT CAST(REGEXP_REPLACE('.25 0', '(\.)\.+|[^0-9]|(^.*?)(\w+).*$', '\1\2\3') AS DECIMAL(16,2)) from dual;
the problem with the two codes at the top is, even though it turns 30 0 into 30 (which is what I want), it messes up the other numbers.
If someone could help me solve this issue so that I can get all the numbers to look the way it is in the second list, that would be great. Thank you
try this:
SELECT REGEXP_REPLACE(REGEXP_REPLACE('30 0', ' .*$'),'(\.)\.+|[^0-9.]', '\1\2\3') FROM dual
Related
how do I show in a pie chart the percentage of the total value. eg lets say I have total sick leave which is 30 days and I take 15 days, how in a pie I show 15/30
Generally speaking, to get a percentage value, you'd divide those two values and multiply them by 100 (and, possibly, round the result to 0, 1 or 2 decimals).
That would be - in your example - 50%, right?
SQL> select round(15 / 30 * 100, 2) from dual;
ROUND(15/30*100,2)
------------------
50
SQL>
Chart query expects 3 columns to be specified, e.g.
select null as link,
what as label,
number_of_days as value
from some_table
where some_condition
If you could provide test case so that we'd see what you really have, we'd be able to suggest something more.
Let me start by saying, I am very new to Hive, so I'm not sure what information folks will need to help me out. Please let me know what information would be useful. Also, while I'd usually create a small dataset to recreate the problem with, I think this problem has to do with the scale of my dataset, because I can't seem to recreate the problem on a smaller dataset. Let me know if you have suggestions to make this more easy to answer.
Okay now that's out of the way, here's my problem. I have a huge dataset, partitioned by month, with about 500 million rows per month. I have a column with an ID number in it (I'll call it idcol), and I want to closely examine a couple of examples where there's a high number of repeated IDs and a very low number. So, I used this:
SELECT idcol, COUNT(*) FROM table WHERE month = 7 GROUP BY idcol LIMIT 10;
And got:
000005185884381 13
000035323848000 24
000017027256315 531
000010121767109 54
000039844553332 3
000013731352481 309
000024387407996 3
000028461234451 67
000016564844672 1
000032933040806 17
So, I went to investigate the first idvar with a count of 3, with:
SELECT * FROM table WHERE month = 7 AND idcol = '000039844553332';
I expected to see just 3 rows, but ended up with 469 rows found! That was strange enough, but then I just happened to run the original line of code above but with LIMIT 5 instead and ended up with:
000005185884381 13
000017027256315 75
000010121767109 25
000013731352481 59
000024387407996 1
And, it may be hard to see because the idcol is so long, but idvar 000017027256315 ended up with a count of 531 when I did LIMIT 10 and just 75 when I did LIMIT 5.
What am I missing?! How can I get a correct count of just a small number of values so I can investigate further?!
BTW my first thought was to make the counting part a sub-query, but that didn't change a thing. I used:
SELECT * FROM (SELECT idcol, COUNT(*) FROM table WHERE month = 7 GROUP BY idcol) x LIMIT 10;
...same EXACT results
Most likely the counts are being computed from statistics.See here for the bug and the related discussion.
hive.compute.query.using.stats = FALSE
If this doesn't fix it try the ANALYZE command before running the count(*)
ANALYZE TABLE table_name PARTITION(month) COMPUTE STATISTICS;
I have some issues that I hope you can lend a helping hand towards.
So I have some data with opposite value, for example:
Amount Type ID
10000 10 25
-10000 10 25
20000 11 30
30000 12 49
-30000 12 49
Sorry for the ugly table.
But how can I delete the lines where the amount cancelled out? I would like the ones with 10000 and -10000 to be deleted. But I wont know the specific type and id number. (30000 and -30000 is the same issue).
Any ideas? I've been searching forever, but can only find how to remove a duplicate row, and not both rows.
Hope it makes sense :)
Update. Thanks for the solutions so far! :)
There can be more than 1:1 in the amount column, but those wouldn't identical Type and ID. For exampel could a 6th entry look like this:
Amount Type ID
10000 10 25
-10000 10 25
20000 11 30
30000 12 49
-30000 12 49
10000 31 42
And the last one should not be deleted :) Hope it makes sense now.
On the basis only of the limited information provided...
DELETE x
FROM my_table x
JOIN my_table y
ON y.id = x.id
AND y.type = x.type
AND y.amount = x.amount * -1;
I have a table in oracle database which may contain amounts >=$10M or <=$-10B.
99999999.99 chunks and also include remainder.
If the value is less than or equal to $-10B, I need to break into one or more 999999999.99 chunks and also include remainder.
Your question is somewhat unreadable, but unless you did not provide examples here is something for start, which may help you or someone with similar problem.
Let's say you have this data and you want to divide amounts into chunks not greater than 999:
id amount
-- ------
1 1500
2 800
3 2500
This query:
select id, amount,
case when level=floor(amount/999)+1 then mod(amount, 999) else 999 end chunk
from data
connect by level<=floor(amount/999)+1
and prior id = id and prior dbms_random.value is not null
...divides amounts, last row contains remainder. Output is:
ID AMOUNT CHUNK
------ ---------- ----------
1 1500 999
1 1500 501
2 800 800
3 2500 999
3 2500 999
3 2500 502
SQLFiddle demo
Edit: full query according to additional explanations:
select id, amount,
case
when amount>=0 and level=floor(amount/9999999.99)+1 then mod(amount, 9999999.99)
when amount>=0 then 9999999.99
when level=floor(-amount/999999999.99)+1 then -mod(-amount, 999999999.99)
else -999999999.99
end chunk
from data
connect by ((amount>=0 and level<=floor(amount/9999999.99)+1)
or (amount<0 and level<=floor(-amount/999999999.99)+1))
and prior id = id and prior dbms_random.value is not null
SQLFiddle
Please adjust numbers for positive and negative borders (9999999.99 and 999999999.99) according to your needs.
There are more possible solutions (recursive CTE query, PLSQL procedure, maybe others), this hierarchical query is one of them.
I am new to oracle and I have a problem.
I have a column named file_id.
When I do an order by it sorts strings such as
1
1
10
100
11
11
110
114
12
300
31
4200
B14
B170
B18
edit:
I would like it to sort this way.
1
1
10
11
11
12
31
100
300
4200
B14
B18
B170
The answer below works perfectly. Only other problem I ran into now..I have records that are blank. How could I make the blank records order at the end?
1
1
10
11
11
12
31
100
300
4200
BLANK
BLANK
BLANK
BLANK
BLANK
B14
B18
B170
Thank you for your help.
select column
from table
order by
regexp_substr(column, '^\D*') nulls first,
to_number(regexp_substr(column, '\d+'))
fiddle
This is an old question, but it was the first hit on google so I thought I'd share an alternative solution:
select column
from table
order by
LPAD(column, 10)
The LPAD function pads the left-side of the string with spaces so that the results will be sorted numerically. This works for non-numeric values, and null values will be sorted last. This works well if you know the maximum length of the strings to be sorted (you may need to adjust the second parameter to suit your needs).
Source: http://www.techonthenet.com/oracle/questions/sort1.php
EDIT:
I noticed that while my solution works well for my case, the output is slightly different from the accepted answer (http://www.sqlfiddle.com/#!4/d935b8/2/0):
1
1
10
11
11
12
31
100
110
114
300
A14
A18
4200
A170
(null)
(null)
4200 should come after 300. For my situation this is good enough, but this may not always be the case.
Based on the previous solution:
SELECT column
FROM table
ORDER BY LPAD(column, (SELECT MAX(LENGTH(column)) FROM table)) ASC
The advantage of this approach is that you don't need know the table column size.