Impala: Dynamically extract JSON attributes into rows - json

Environment: Impala version 4.1
Scenario: I have a table that has an id column and a JSON column, like so:
id
json_col
111
[{"m_id":"896de41d","name":"DE"},{"m_id":"194a3028","name":"Free"}]
222
[{"m_id":"687c6baa","name":"Texti"},{"m_id":"194a3028","name":"Default"},{"m_id":"896de41d","name":"Parcel"}]
Is there a function that dynamically takes out m_id and name attrtibutes into rows per id?
I tried the GET_JSON_OBJECT function but I'm not sure if it has that ability.

Related

How to write select query in mysql with dynamic table name and dynamic column name

Above is the table structure of database. For each ‘form_id’ in ‘ap_forms ‘ there is a dynamic table with dynamic column names like ap_form_{form_id}.
Columns in this table will created dynamically by element_id and option_id combination. (eg: for element_id=1 and option_id=2 then column name= element_1_2).
I want the query result from these tables and this will looks like
I tried with dynamic query and I think we can’t create dynamic query with dynamic table name and dynamic column name. Please help me to get the above query result.

How to generate incremental unique id on MySQL with existing values?

I have a table with data which has 3 rows:
"id" is primary key and auto increment.
order_no is NOT unique index because there may be many company_ids with the same order_no.
I can generate and show user next order_no with this query:
select LPAD(count(order_no)+1, 4, '0') from customers_orders where company_id=28
which gives me "0004". It's OK. But the user can also enter id manually and the data is like this somethimes:
Now it doesn't start with 0001 and the generate query gives me "0003". But I already have "0003" in there. The query should return like "0005".
If I get the latest order_no and add it 1 (for example max(order_no)+1), it won't work. Because the data can be also like this:
If the data is like this; the query have to generate "0003".
I think I can make this with while loop in PHP, but i want to do this with MySQL if it is possible. Thank you.

Postgres JSON querying - match all pairs, ignore order

I have a Postgres table that contains a jsonb column, the data in which is arbitrarily deep.
id | jsonb_data
---|----------------------
1 | '{"a":1}'
2 | '{"a":1,"b":2}'
3 | '{"a":1,"b":2,"c":{"d":4}}'
Given a JSON object in my WHERE clause, I want to find the rows that contain objects that contain the same data and no more, but in any order. Including, preferably, nested objects.
SELECT * FROM table
WHERE json_match_ignore_order(jsonb_data, '{"b":2,"a":1}');
id | jsonb_data
---|-----------
2 | '{"a":1,"b":2}'
This would essentially work identically to the following Ruby code, but I'd really like to do it in the database if possible.
table.select { |row| row.jsonb_data_as_a_hash == {b: 2, a: 1} }
How can I do this?
With jsonb type you can use equal sign even for values with nested objects.
Thus the following will also work:
create table jsonb_table(
id serial primary key,
jsonb_data jsonb
);
insert into jsonb_table(jsonb_data)
values
('{"a":1}'),
('{"a":{"c":5},"b":2}'),
('{"a":{"c":5},"b":2,"c":{"d":4}}');
select * from jsonb_table
where jsonb_data = '{"b":2,"a":{"c":5}}'::jsonb;
You will get rows with objects containing same keys with same values recursively (in this case only the second row).

sql get data from multiple tables and add the result

My MySQL db has a table called catmaster. It contains various "categories", each with a unique id like CAT12. Each "category" is made up of underlying "keywords" and has its own table with 1 column called key_id. So the table CAT12 might have 4 records (keywords) like the following: KEY1 KEY2 KEY3 KEY4. Each of these keywords also has its own table with the same names. Each of these tables has 2 columns, 1 with a date and the other with an integer value, call it inventory.
Is there a way I can write a query that pulls the SUM of the inventory for the underlying keywords (however many there might be) in a given category for a given date?
or is it best to just have my application loop through the records and pull the value for each keyword then sum it itself?
THANKS!

Is that possible making sure same ID wont appear twice in 2 tables (Database)

Is that possible making sure same ID wont appear twice in 2 tables?
For example i've got this colum: call id
and I want to make sure it'll count it in both tables
for ex: calls_to_x and calls_to_y
I want to enter both tables data and to make sure the call id colum will increase from both table for ex:
calls_to_x:
Call id: 1 parameters: .... time: 01:00:00
Call id: 3 parameters: .... time: 01:30:00
calls_to_y:
Call id: 2 parameters: .... time: 01:10:00
Call id: 4 parameters: .... time: 01:34:00
The answer depends on what RDBMS you're using.
For MySQL:
If you only need unique values (and not sequential), you can use something like an UUID to uniquely identify each row.
If you do need the values to be sequential, either merge the tables to one single table by having a field indicating the type of content or use a third table to keep track of used ids with an id field and auto_increment for that column. A second column can keep track of which table used the id if needed. You can INSERT INTO that table in an SQL function and return the value of LAST_INSERT_ID() to get the next id for your other tables.
For most other RDBMSes
Use a sequence (CREATE SEQUENCE).
Another option would be to have a table that is AllCalls that has a callid and at recipient then include this id as a foreign key in either of the two tables. Only the fields that are different could be in the calls_to_x and calls_to_y table things that are generally applicable to any call could all be in the AllCalls table.