I have a table of data type VARIANT that holds a parquet file.
I have a relational table of the format
> CREATE TABLE IF NOT EXISTS covid_data_relational
> (
> id int identity(1,1),
> date_dt DATE,
> state string,
> value int,
> population_percent float,
> change_from_prior_day int,
> seven_day_change_percent float
> );
Inserting data into this table populates records only for columns "date" and "state". Any column that has the format cases.* is not populated.
The query is as follows:
insert into covid_data_relational(date_dt, state, value, population_percent, change_from_prior_day, seven_day_change_percent)
select covid_data_raw:date::date as date_dt,
covid_data_raw:state::string as state,
covid_data_raw:cases.value::int as value,
covid_data_raw:cases.calculated.population_percent::float as population_percent,
covid_data_raw:cases.calculated.change_from_prior_day::int as change_from_prior_day,
covid_data_raw:cases.calculated.seven_day_change_percent::float as seven_day_change_percent
from covid_data_parquet;
Any help is appreciated! Thanks in advance
It would be better to post sample data as text rather than an image. People can more easily copy and paste the sample to test. So this isn't tested, but it should work.
Take a close look at the cases.calculated.change_from_prior_day key. It is not a nested key in the grandparent path cases.*. It is a hard-coded key formed by a long string with dots.
To extract that, you'll need to specify that the key is the whole string including the dots:
insert into covid_data_relational(date_dt, state, value, population_percent, change_from_prior_day, seven_day_change_percent)
select covid_data_raw:date::date as date_dt,
covid_data_raw:state::string as state,
covid_data_raw:"cases.value"::int as value,
covid_data_raw:"cases.calculated.population_percent"::float as population_percent,
covid_data_raw:"cases.calculated.change_from_prior_day"::int as change_from_prior_day,
covid_data_raw:"cases.calculated.seven_day_change_percent"::float as seven_day_change_percent
from covid_data_parquet;
Related
i want match string in json string that like:
"ids":[44,53,1,3,12,45]
i want run query in sqlite send only one digit as id and match one of the above id in sql statement
i write this regex "ids":[\[] for matching start of key
but i don't have any idea to match middle id and escape starting id
example:
i have calc_method table like this:
CREATE TABLE "calc_method" (
"calc_method_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"calc_method_name" TEXT NOT NULL,
"calc_method_value" TEXT NOT NULL
);
in calc_method_value column i store calcMethod class which convert to json using Gson
class calcMethod{
var memberCafeIds:ArrayList<Long>,
var memberBarIds:ArrayList<Long>
}
after i convert calcMethod to json i have output like below and this value store in calc_method_value column:
{"memberCafeIds":[1,2,14,5,44],"memberBarIds":[23,1,5,78]}
now i want select row that match to my regex pattern like if calc_method_value column have memberBarIds with id 1
SELECT * FROM calc_method WHERE calc_method_value REGEXP '"memberCafeIds":\[[:paramId]'
:paramId is method parameter
Regards, a programmer struggle with regex
In Sqlite, use JSON1 functions to work with JSON, not regular expressions. In particular, json_each() to turn the JSON array into a table you can query:
sqlite> CREATE TABLE ex(json);
sqlite> INSERT INTO ex VALUES ('{"ids":[44,53,1,3,12,45]}');
sqlite> SELECT * FROM ex WHERE 1 IN (SELECT value FROM json_each(ex.json, '$.ids'));
json
-------------------------
{"ids":[44,53,1,3,12,45]}
sqlite> SELECT * FROM ex WHERE 50 IN (SELECT value FROM json_each(ex.json, '$.ids'));
sqlite>
I will explain what I have done so far and ask my question at the end.
First I created my external & internal variables (note that #EDelivery is a table type that included the columns FoodID and Quantity):
#EDelivery DeliveryTblType readonly
Declare #NumberOfMinutes smallint, #FoodWeight int, #WeightCapacity int,
#DroneID int, #TimeOfWeek varchar
I then read this in, giving me the total weight of all items ordered and read in the weight capacity from the DronesTbl:
select #FoodWeight = SUM(FoodWeight * Quantity)
from #EDelivery as del
inner join Foods on del.FoodID = Foods.FoodID
select #WeightCapacity = Drones.WeightCapacity
from dbo.Drones
My question is how do I select a specific drone to compare its weightcapacity to the food total weight
You need to set the variable #WeightCapacity according to the #droneid value. Right now, you are setting this variable to last drone in your drones table.
If you change your second query to what's below then it should work.
select #WeightCapacity =
Drones.WeightCapacity
from dbo.Drones
where DroneID =#droneID
I have a table with a JSON text field:
create table breaches(breach_id int, detail text);
insert into breaches values
( 1,'[{"breachedState": null},
{"breachedState": "PROCESS_APPLICATION",}]')
I'm trying to use MSSQL's in build JSON parsing functions to test whether ANY object in a JSON array has a matching member value.
If the detail field was a single JSON object, I could use:
select * from breaches
where JSON_VALUE(detail,'$.breachedState') = 'PROCESS_APPLICATION'
but it's an Array, and I want to know if ANY Object has breachedState = 'PROCESS_APPLICATION'
Is this possible using MSSQL's JSON functions?
You can use function OPENJSON to check each object, try this query:
select * from breaches
where exists
(
select *
from
OPENJSON (detail) d
where JSON_VALUE(value,'$.breachedState') = 'PROCESS_APPLICATION'
)
Btw, there is an extra "," in your insert query, it should be:
insert into breaches values
( 1,'[{"breachedState": null},
{"breachedState": "PROCESS_APPLICATION"}]')
I am trying to load following data in hive table
data which i am loading
I used following table defination
CREATE EXTERNAL TABLE IF NOT EXISTS YOUTUBE_DATA (
VIDEO_ID STRING,
UPLOADER STRING,
INTERVAL INT,
CATEGORY STRING,
VIDEO_LEN INT,
VIEW_NO INT,
RATING FLOAT,
NO_COMMENTS INT,
RELATED_VIDEOS ARRAY
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
location '/five';
LOAD DATA INPATH '/DATA/YOUTUBEDATA' OVERWRITE INTO TABLE YOUTUBE_DATA;
on running the query select * from youtube_data limit 10;
following was output.
output of query
can anyone please help with mistake which I am doing and with solution ??
I am using sqlite3 python modules and th following code returns the error
InterfaceError: Error binding parameter 0 - probably unsupported type
Note I have already tried with normal (non unicode) strings and the result is the same
# get database cursor
cur = dbConnection.cursor()
# create table for prices
cur.execute( """
create table if not exists
prices( time text,
mid integer,
exid text,
selid integer,
priceone real,
sometext text,
price2 real,
primary key (time, mid, exid, selid, priceone)
foreign key (time, mid, exid, selid) references selection(time, mid, exid,selid) )""" )
#insert price
tuple = (u'20120228153239788135', 104982590, 1, 4764315, 1.99, u'B', 0.07)
cur.execute( "insert into prices values (?,?,?,?,?,?,?)", tuple)
This code works fine for me.
However, have you changed your table schema at all? Because you add the
create if not exists
it is likely you changed something but the DB (file) wasn't updated since you have this.
Also, you are passing in an int for exid even though the type is text. It will automatically convert it but still shouldn't do it.