I have two tables in postgresql.
the first (product) has sku json row ([149461190])
the second (item) has an ordinary sku column
How can I join them on sku? I tried this, but it didn't work.
select * from product ps
, jsonb_to_recordset(ps.sku -> 'ps_sku') as (sku text)
join item v using sku
Exception I get
org.apache.hadoop.hive.ql.parse.ParseException:line 5:39 cannot
recognize input near 'jsonb_array_elements_text' '(' 'p' in
joinSourcePart
I understood that product is a table and this table has a field by name sku and by type JSON, and JSON has a key by name ps_sku
If the ps_sku key value is an integer type then:
select * from product pr
inner join item itm on itm.sku = (pr.sku->'ps_sku')::integer
If the ps_sku key value is a text type then:
select * from product pr
inner join item itm on itm.sku = (pr.sku->'ps_sku')::text
pr.sku->'ps_sku' this command extract value of ps_sku key from JSON string (from sku field)
Related
Write a query to return the list of employee IDs with incorrectly spelled departments, from the Emp Master table. correct data is in Dept_Master with no foreign key.
SELECT * FROM Emp_Master as orig
LEFT OUTER JOIN Dept_Master as correct
ON SOUNDEX(orig.Department) = SOUNDEX(correct.Department_Name)
WHERE orig.Department NOT IN (SELECT Department_Name FROM Dept_Master)
tried this but getting NULL value for string 'marketing' and 'makeing' as soundex drops vowels only as both have same soundex string value.
how can I solve it?
select distinct orig.Emp_ID,(orig.department) from Emp_Master as orig,Dept_Master as correct
where orig.Department not in (select Department_Name from Dept_Master);
I have two tables in postgresql.
the first (product) has sku json row ([149461190])
the second (item) has an ordinary sku column
How can I join them on sku?
I tried this, but it didn't work.
cannot recognize input near 'jsonb_to_recordset' '(' 'ps' in joinSourcePart
select * from product ps
, jsonb_to_recordset(ps.sku -> 'ps_sku') as (sku text)
join item v using sku
I hope this query help you, You can see data structure and sample data in dbfiddle
select
*
from
product p
cross join jsonb_array_elements_text(p.sku -> 'ps_sku') as j(sku)
inner join item i on i.sku = j.sku :: numeric
I have a table in MySQL with data in a column in the following format:
[{"type_id":1,"price":50},{"type_id":3,"price":60}]
I need to find out price of the item based on its id. For example, I need to find out price of an item with type_id = 3
I have tried:
select JSON_EXTRACT(JSONColumn, '$[*].price') as prices, JSON_EXTRACT(JSONColumn, '$[*].type_id') as quantities from Items where ItemId = 123
and json_contains(JSONColumn, '{"type_id" : 3}')
This is not working. Can someone specify the correct way of querying json data?
SELECT test.id, jsontable.price
FROM test
CROSS JOIN JSON_TABLE (test.val,
'$[*]' COLUMNS (type_id INT PATH '$.type_id',
price INT PATH '$.price')) jsontable
WHERE jsontable.type_id = 3;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=baa0a24a4bbf10ba30202c7156720018
I have two database tables:
First table is named FIELDS and has following two columns:
FIELD_ID whose data-type is int
FIELD_NAME whose data-type is varchar
Second table is named SHIPPINGS and has following two columns:
FIELD_ID whose data-type is int
SHIPPING_ID whose data-type is int
I need to retrieve all field names, and mark those which have certain SHIPPING_ID set in table SHIPPINGS.
Example:
If I will choose SHIPPING_ID = 3, I'd like to receive the following structure:
s_firstname true
b_lastname false
s_lastname false
How do I do this? I have tried the following SQL query:
SELECT pf.field_name, ps.shipping_id, IF(ps.shipping_id = 3, "true", "false") AS status
FROM `fields` AS pf LEFT JOIN `shippings` AS ps
ON ps.shipping_id = 3
But it returns true for all fields.
What did I do wrong?
You can use aggregation:
select f.*, (s.field_id is not null) as has_3
from fields f left join
shippings s
on f.field_id = s.field_id and s.shipping_id = 3;
Note: This interprets the results as wanting a boolean value. If you want a string, then use a case expression:
select f.*,
(case when s.field_id is not null then 'true' else 'false end) as has_3
I want to write an MySQL Query where I replace a JSON Array with Data from another Table.
I have got two Tables, "Reserved" and "Seats". Reserved contains one column "Seats", an JSON Array referring to the ID of the Table "Seats". Table Seats also contains a column "Name". I now want to basically replace the IDs in the JSON Data of the Seats column of the Reserved Table, with the name of the corresponding IDs stored in the Seats Table.
Is there a way to do this in an Mysql Query. I do not know how to pack a query result in a JSON Format and return it as a column
I already tried to utilize JSON_EXTRACT somehow : see test below.
SELECT * FROM `seats` WHERE ID = JSON_EXTRACT('["276", "277", "278"]','$.*')
Basically I want a Query like this:
SELECT *,
JSONCreate(SELECT name from `seats` WHERE seats.id IN JSON_EXTRACT(reserved.seats)) as name
FROM `reserved`
WHERE 1
You can use one of the following solutions.
solution using JSON_SEARCH and JSON_ARRAYAGG
SELECT r.seats, JSON_ARRAYAGG(s.name)
FROM reserved r JOIN seats s ON JSON_SEARCH(r.seats, 'one', CONVERT(s.id, CHAR(10))) IS NOT NULL
GROUP BY r.seats
solution using ... MEMBER OF () and JSON_ARRAYAGG
SELECT r.seats, JSON_ARRAYAGG(s.name)
FROM reserved r INNER JOIN seats s ON CONVERT(s.id, CHAR) MEMBER OF(r.seats)
GROUP BY r.seats
solution using JSON_CONTAINS and JSON_ARRAYAGG
SELECT r.seats, JSON_ARRAYAGG(s.name)
FROM reserved r INNER JOIN seats s ON JSON_CONTAINS(r.seats, JSON_QUOTE(CONVERT(s.id, CHAR))) = 1
GROUP BY r.seats
You can also use JSON_TABLE to solve this:
SELECT JSON_ARRAYAGG(IFNULL(s.name, ''))
FROM reserved r, JSON_TABLE(
r.seats,
"$[*]" COLUMNS (
id CHAR(50) PATH "$"
)
) AS rr LEFT JOIN seats s ON rr.id = s.id
GROUP BY r.seats
Note: You can use INNER JOIN to remove the empty values. Instead of GROUP BY r.seats you should use a id column.
demo on dbfiddle.uk