INSERT INTO for JSON array - json

I'm getting text files where some columns are populated with a JSON array. I import the file into a staging table. I want to use INSERT INTO to get the file into another table with the JSON array column parsed into two columns.
I need one column named 'military_focus' and another named 'health_insurance_focus' that will be populated with 'true' or 'false'. Using this SELECT statement presents the data as I need it.
SELECT
[network_id]
[network_name],
[network_type],
[service_type_ids],
JSON_VALUE(focus,'$.military_focus') AS military_focus,
JSON_VALUE(focus,'$.health_insurance_focus') AS health_insurance_focus,
[created_at],
[updated_at],
[LoadDt],
[FileNM]
FROM
[Med_Stage].[Provider].[networks]
I'm trying to use that with an INSERT INTO to get it into another table with the appropriate columns. I get an error that the SELECT values do not match the number of INSERT columns since I'm going from one 'focus' column in the Staging table to two columns in the destination table.
INSERT INTO [Med].[Provider].[networks]
(
[network_id],
[network_name],
[network_type],
[service_type_ids],
[military_focus],
[health_insurance_focus],
[created_at],
[updated_at],
[LoadDt],
[FileNM]
)
SELECT
[network_id]
[network_name],
[network_type],
[service_type_ids],
JSON_VALUE(focus,'$.military_focus') AS military_focus,
JSON_VALUE(focus,'$.health_insurance_focus') AS health_insurance_focus,
[created_at],
[updated_at],
[LoadDt],
[FileNM]
FROM
[Med_Stage].[Provider].[networks]

Yes, the fiddle is sufficent, being able to test immediately highlights the issue.
You're just missing a comma after the first column
SELECT
[network_id], /* <-- missing comma*/
[network_name],
[network_type],
[service_type_ids],
JSON_VALUE(focus,'$.military_focus') AS military_focus,
JSON_VALUE(focus,'$.health_insurance_focus') AS health_insurance_focus,
[created_at],
[updated_at],
[LoadDt],
[FileNM]
FROM
[Med_Stage].[Provider].[networks]

Related

Can SqlAlchemy's array_agg function accept more than one column?

I want to return arrays with data from the entire row (so all columns), not just a single column. I can do this with a raw sql statement in Postgresql,
SELECT
array_agg(users.*)
FROM users
WHERE
l_name LIKE 'Br%'
GROUP BY f_name;
but when I try to do it with SqlAlchemy, I'm getting
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'InstrumentedAttribute'
For example, when I execute this query, it works fine
query: Query[User] = session.query(array_agg(self.user.f_name))
But with this I get arrays of rows with only one column value in them (in this example, the first name of a user) whereas I want the entire row (all columns for a user).
I've tried explicitly listing multiple columns, but to no avail. For example I've tried this:
query: Query[User] = session.query(array_agg((self.user.f_name, self.user.l_name))))
But it doesn't work. I get the above error message.
You could use Python feature unpack for create
example = [func.array_agg(column) for column in self.example.__table__.columns]
query = self.dbsession.query(*attach)
And after join results

Extract character from db to another db with all items

I tried to copy the data related to the guid of a character to another db (same account) but it always appears without any item
I am use deleted/insert
This from inventori db for example:
DELETE FROM `character_inventory` WHERE `item`=item;
INSERT INTO `character_inventory` VALUES (guid, bag, slot, item);
Ever export/import the character appear without items: no equiment and no inventory
psd: I executed the query with the server turned off and on with the same result
You can use this query to import the table content from another database.
INSERT INTO db1.`character_inventory` SELECT * FROM db2.`character_inventory` WHERE guid=XXX;
You need also to copy the item_instance table probably, so use:
INSERT INTO db1.`item_instance` SELECT * FROM db2.`item_instance`;

Moving data from one mySQL Table field to another Table field where each table has a matching id

I need to move all the data from table FCSTMR field CSTYPE to table customers2 field customers_group_pricing.
The value stored in FCSTMR CS_ENCSUNIQUE matches that stored in customers2 customers_id.
I tried using the following mysql, but it didn't move any data at all.
UPDATE customers2
SET `customers_group_pricing` = (
SELECT `CSTYPE`
FROM FCSTMR
WHERE CS_ENCSUNIQUE = customers2.customers_id);
Where did i go wrong with this? I'm assuming something to do with the WHERE statement.
Have a look this question's last comment. I think it is similar to yours.

Insert a set specified as a comma separated string into a table

Concept
3 Tables:
Events (INT EventRid, Title, Desc, ....)
Participants (INT ParticipantRid, FirstName, LastName,Address....)
ParticipantEventMap(INT refEventRid,INT refParticipantRid)
Application (without significant re-write) will attempt to submit the data about the event (1 field per Event table field PLUS a field 'Participants' which is a comma separated list of ParticipantRids). The fields in the events table are easy to add/update but I seek a means of submitting a query which will do something along the lines of:
INSERT INTO ParticipantEventMap (refEventRid,refParticipantRid)
VALUES (10003211,(Participants));
Of course this is totally invalid SQL syntax, the idea being that it would expand (10003211,(Participants)) into (10003211,ParticipantRid[1]),(10003211,ParticipantRid[2]),...
Is there a way do this as an SQL query, or am I required to perform all mangling on the PHP side before submitting separate queries?
INSERT IGNORE INTO ParticipantEventMap (
SELECT
1002324 as refProgramEventRid,
ParticipantRid as refParticipantRid
FROM Participants where ParticipantRid in (1,2,3,4));
Thus by replacing 1002324 with {EventID} and 1,2,3,4 with {Participants} in the PHP prepared statement I get the desired result! For those interested the app I'm trying to make this work in is DHTMLx Scheduler module.

Update MySQL without specifying column names

I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.