Error if coordinates in SDO_ORDINATE_ARRAY has decimals - sql

I´m trying to insert a geometry into a table using the Oracle SDO_GEOMETRY objects.
If I use the following coordinates without decimals, it works without issue:
DECLARE
ordinate_arr SDO_ORDINATE_ARRAY := SDO_ORDINATE_ARRAY(2,4, 4,3, 9,3, 13,5, 13,9, 11,13, 5,13, 2,11, 2,4, 7,5, 7,9, 9,9, 9,5, 7,5);
info_arr SDO_ELEM_INFO_ARRAY := SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1);
begin
update schema.table set geo_coord = (SDO_GEOMETRY(2003, 25832, NULL,
info_arr, ordinate_arr)) where id = 332564;
END;
However, if I use coordinates with decimals, it throws an ORA-06502 Error. Here is the code with decimals:
DECLARE
ordinate_arr SDO_ORDINATE_ARRAY := SDO_ORDINATE_ARRAY(2.5, 4.5, 4.5, 3.5, 9.5, 3.5, 13.5, 5.5, 13.5, 9.5, 11.5, 13.5, 5.5, 13.5, 2.5, 11.5, 2.5, 4.5, 7.5, 5.5, 7.5, 9.5, 9.5, 9.5, 9.5, 5.5, 7.5, 5.5);
info_arr SDO_ELEM_INFO_ARRAY := SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1);
begin
update schema.table set geo_coord = (SDO_GEOMETRY(2003, 25832, NULL,
info_arr, ordinate_arr)) where id = 123456;
END;
and here is the error that is raised:
DECLARE
ordinate_arr SDO_ORDINATE_ARRAY := SDO_ORDINATE_ARRAY(2.5, 4.5, 4.5, 3.5, 9.5, 3.5, 13.5, 5.5, 13.5, 9.5, 11.5, 13.5, 5.5, 13.5, 2.5, 11.5, 2.5, 4.5, 7.5, 5.5, 7.5, 9.5, 9.5, 9.5, 9.5, 5.5, 7.5, 5.5);
info_arr SDO_ELEM_INFO_ARRAY := SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1);
begin
update schema.table set geo_coord = (SDO_GEOMETRY(2003, 25832, NULL,
info_arr, ordinate_arr)) where id = 123456;
END;
Fehlerbericht -
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause: An arithmetic, numeric, string, conversion, or constraint error
occurred. For example, this error occurs if an attempt is made to
assign the value NULL to a variable declared NOT NULL, or if an
attempt is made to assign an integer larger than 99 to a variable
declared NUMBER(2).
*Action: Change the data, how it is manipulated, or how it is declared so
that values do not violate constraints.
If I, however, use a normal update query like this, including decimals, it works without errors:
update schema.table set geo_coord = (SDO_GEOMETRY(2003, 25832, NULL,
SDO_ELEM_INFO_ARRAY(1,1003,1, 19,2003,1), SDO_ORDINATE_ARRAY(2.5, 4.5, 4.5, 3.5, 9.5, 3.5, 13.5, 5.5, 13.5, 9.5, 11.5, 13.5, 5.5, 13.5, 2.5, 11.5, 2.5, 4.5, 7.5, 5.5, 7.5, 9.5, 9.5, 9.5, 9.5, 5.5, 7.5, 5.5)))
where id = 123456;
Any idea why is this happening?

Related

SQL OperationalError: near "%": syntax error when using inserting values into a table with "executemany"

I have been trying to insert values into a table with the following code:
top10_strong_beers.values.tolist() =
[['Surly Brewing Company', 'Abrasive Ale', 2020, 1],
['Modern Times Beer', 'Blazing World', 2020, 2],
['Sixpoint Craft Ales', 'Hi-Res', 2020, 3],
['Southern Star Brewing Company', 'Red Cockaded Ale', 2020, 4],
['Tallgrass Brewing Company', 'Ethos IPA', 2020, 5],
['Caldera Brewing Company', 'Hopportunity Knocks IPA', 2020, 6],
['Mike Hess Brewing Company', 'Habitus (2014)', 2020, 7],
['Oskar Blues Brewery', 'GUBNA Imperial IPA', 2020, 8],
['Renegade Brewing Company', 'Redacted Rye IPA', 2020, 9],
['Sockeye Brewing Company', 'Dagger Falls IPA', 2020, 10]]
query = """
INSERT INTO strong_beer_competition
(brewery,beer,year,ranking)
VALUES (%s,%s,%s,%s)
"""
cur.executemany(query, top10_strong_beers.values.tolist())
conn.commit()
I get the following error:
OperationalError Traceback (most recent call last)
<ipython-input-47-e3e92e2c1d6f> in <module>
11 """
12
---> 13 cur.executemany(query, top10_strong_beers.values.tolist())
14 conn.commit()
OperationalError: near "%": syntax error
I'm not sure how to fix this. I've been searching for why this error occurs, but it seems similar to other code I've looked up. Can anyone help? Anything that gets me closer to solving this is appreciated. Thank you.
Well, I accidentally found an answer when looking up a solution for another problem. I should be using "?" instead of "%s".
I hadn't realised that there were different versions of SQL, and I am apparently using SQLite3 that uses "?" and not "%s". Hope this helps other idiots like myself.

How eliminate rows in a dataframe with common values with other dataframe? Rstudio

I need to eliminate the rows in a dataframe that have at a column common values with the same column in a second dataframe
The columns the code have to take into account contain IDs of subjetcs, while the rest contain data refering to those subjects.
Example of dataframes (Rstudio)
df1<-data.frame(ID=c(13, 16, 25, 36, 25, 17, 50, 63, 61, 34, 65, 17), AnyData=round(runif(12, 1, 5)))
df2<-data.frame(ID=c(89, 57, 13, 17, 18, 21, 51, 50, 72, 84), AnyData=round(runif(10, 1, 5)))
I have tried two functions
df1<- filter(df1, ID!=df2[ID])
df1<- df1[-c(which(df1[ID]==df2[ID]))]
The result should be:
df1 <- data.frame(ID=c(16, 25, 36, 25, 63, 61, 34, 656), AnyData=(...)
AnyData depends on the values asigned with ruinf, so it will vary, but the value must be the same as in the original df1.
What you need is an anti_join():
library(dplyr)
df1 %>%
anti_join(df2, by = "ID")

Skip first column

Quite simple question I hope.Basically I want the same output without the first column.
import pandas as pd
ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils','Kings',
'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year':
[2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
df.loc[df['Team']=='Riders'].values.tolist()
Out [1]:
[['Riders', 1, 2014, 876],
['Riders', 2, 2015, 789],
['Riders', 2, 2016, 694],
['Riders', 2, 2017, 690]]
I want my output to be:
Out [1]:
[[1, 2014, 876],
[2, 2015, 789],
[ 2, 2016, 694],
[2, 2017, 690]]
You can do this,
df.loc[df['Team']=='Riders', ['Rank', 'Year', 'Points'] ].values.tolist()
Or if you want to select the columns without explicitly specifying column names,
columns = df.columns.values.tolist()[1:]
df.loc[df['Team']=='Riders', columns].values.tolist()
Use:
df.loc[df.Team == "Riders", df.columns[1:]].to_numpy().tolist()
to_numpy() is recommended instead of values according to pandas documentation. Both gives you numpy arrays so you can still use tolist().
You can select by positions all columns without first by DataFrame.iloc:
#pandas 0.24+
print (df.iloc[(df['Team']=='Riders').to_numpy(), 1:].to_numpy().tolist())
#oldier pandas versions
#print (df.iloc[(df['Team']=='Riders').values, 1:].values.tolist())
[[1, 2014, 876], [2, 2015, 789], [2, 2016, 694], [2, 2017, 690]]

SQL. BigQuery. Find values from an array corresponding to another column

I have a table with columns Date, UserID, EventID, Value, RangeOfValues.
The task is two determine a pair of values from the last columns between which is the value from the 4th columns. So, for example, if the user on the screenshot has value 326 in Value clmn it will be between 200 and 1000. I have a lot of users and need to extract such pairs for each of them. Can do this in python but have no ideas how to do this in bigquery (or even if it's possible).
Any advice would be appreciated!
The table looks like this
Yes, this is easily achievable using UNNEST() to turn the array into rows and then run simple sub-queries on them:
WITH test as (
SELECT * FROM UNNEST([
STRUCT(4 as value, [1, 3, 5, 7, 9, 100, 150, 40] as rangeOfValues)
,(15, [1, 3, 5, 7, 9, 100, 150, 40])
,(50, [1, 3, 5, 7, 9, 100, 150, 40])
,(160, [1, 3, 5, 7, 9, 100, 150, 40])
])
)
SELECT
value,
(SELECT MAX(r) FROM UNNEST(rangeOfValues) r WHERE r<value ) nextLowest,
(SELECT MIN(r) FROM UNNEST(rangeOfValues) r WHERE r>value ) nextBiggest
FROM test

Generate random sample of values in a range with replacements using numpy

np.random.uniform(low, high, size) does not give you replacement values.
Is there any way to generate sample values with replacements?
numpy.random.randint
Ex: draw 10 samples from 0 to 10, with replacement.
import numpy as np
np.random.randint(low=0, high=10, size=10)
# array([5, 0, 7, 8, 9, 8, 0, 6, 4, 8])
See tons of examples on this post: Numpy distributions and statistical funcitons: examples and reference
You can also use np.random.choice:
import numpy as np
np.random.choice(10,10,replace=True)
# array([5, 0, 7, 8, 9, 8, 0, 6, 4, 8])