Extract jsonb fields as rows - postgresql

I have a table
users
name: varchar(20)
data:jsonb
Records look something like this
adam, {"car": "chevvy", "fruit": "apple"}
john, {"car": "toyota", "fruit": "orange"}
I want to extract all the fields like this
name. |.type |. value
adam. car chevrolet
adam. fruit apple
john. car toyota
john. car orange

For your example you can do:
SELECT name, d.key AS type, d.value
FROM users u,
JSONB_EACH_TEXT(u.data) AS d
;
output:
name | type | value
------+-------+--------
adam | car | chevvy
adam | fruit | apple
john | car | toyota
john | fruit | orange
(4 rows)
There are good explanations here PostgreSQL - jsonb_each

Related

How can i compare 2 tables in postgresql?

i have a table named hotel with 2 columns named : hotel_name , hotel_price
hotel_name | hotel_price
hotel1 | 5
hotel2 | 20
hotel3 | 100
hotel4 | 50
and another table named city that contains the column : city_name , average_prices
city_name | average_prices
paris | 20
london | 30
rome | 75
madrid | 100
I want to find which hotel has a price that's more expensive than average prices in the cities.For example i want in the end to find something like this:
hotel_name | city_name
hotel3 | paris --hotel3 is more expensive than the average price in paris
hotel3 | london --hotel3 is more expensive than the average price in london etc.
hotel3 | rome
hotel4 | paris
hotel4 | london
(I found the hotels that are more expensive than the average prices of the cities)
Any help would be valuable thank you .
A simple join is all that is needed. Typically tables are joined on a defined relationship (PK/FK) but there is nothing requiring that. See fiddle.
select h.hotel_name, c.city_name
from hotels h
join cities c
on h.hotel_price > c.average_prices;
However, while you can get the desired results, it's pretty meaningless. You cannot tell whether a particular hotel is even in a given city.

Is it possible to get column value with subquery based on column's key?

I have the following table:
id | fruit | parent |
----------------------------
id_1 | apple | |
id_2 | | id_3 |
id_3 | pineapple | |
id_4 | plum | id_5 |
id_5 | plum | |
Is it possible with a subquery/lateral join to get the following output:
id_1 apple
id_2 pineapple
id_4 plum
So if the fruit is null, get the parent's fruit. Tried to get this with subquery, collected the linked parents to get the fruit values from that but in that case those were in pair with their ids, not with the "kids" ids. So something like this:
id_1 apple
id_3 pineapple
id_4 plum
If this is only a single level up, you can do it like this:
select id,
coalesce(fruit, (select t2.fruit
from the_table t2
where t2.id = t1.parent
limit 1)) as fruit
from the_table t1
Online example
You can do something like this:
EDIT, sorry, missed that it was postgres. This works
SELECT t1.id, (CASE WHEN t1.parent is NULL THEN t1.fruit ELSE t2.fruit END) as fruit
FROM fruits as t1
LEFT JOIN fruits as t2 ON t1.parent = t2.id;

Tableau calculated field summing up the values

I have a table like this
----------------------------------------------
ID Name Value |
---------------------------------------------|
1 Bob 4 |
2 Mary 3 |
3 Bob 5 |
4 Jane 3 |
5 Jane 1 |
----------------------------------------------
Is there any ways to do out a calculated field where if the name is "Bob" , it'll sum up all the values that have the name "Bob"?
Thanks in advance!
If Name = “Bob” then Value end

How to find out the keywords in two hadoop tables with Spark?

I have two tables in HDFS. One table (Table-1) has some keywords as you can see below. Another table (Table-2) has a text column. Every row could have more than one keyword in Table-1. I need to find out all the matched keywords in Table-1 for the text column in Table-2, and output the keyword list for every row in Table-2.
Example :
Table-1:
ID | Name | Age | City | Gender
---------------------------------
111 | Micheal | 19 | NY | male
222 | George | 23 | CA | male
333 | Linda | 22 | LA | female
Table-2:
Text_Description
------------------------------------------------------------------------
1-Linda and my cousin left the house.
2-Michael who is 19 year old, and George are going to rock concert in CA.
3-Shopping card is ready at the NY for male persons.
Output:
1- Linda
2- Micheal, 19, George, CA
3- NY, male

How to decompose the relation into 5NF?

The example provided in the Fifth Normal Form has ACP(Agent, Company, Product) relation with the following data:
-----------------------------
| AGENT | COMPANY | PRODUCT |
|-------+---------+---------|
| Smith | Ford | car |
| Smith | Ford | truck |
| Smith | GM | car |
| Smith | GM | bus |
| Jones | Ford | car |
-----------------------------
Rule applied is if an agent sells a certain product, and he represents a company making that product, then he sells that product for that company.
The relation is decomposed into 3 relations according to constratint 3D: AC(agent, company), AP(agent, product) and CP(company, product). Hence the join dependency is *{(agent, company), (agent, product),(company, product)}. According to the definition of 5NF, a table R is in fifth normal form (5NF) or project-join normal form (PJ/NF) if and only if every join dependency in R is implied by the keys of R. But none of the projections contain a key, since the key is {agent, company, product} itself.
Another example provided in the book by C.J. Date contains a similar relation shipments(suppier_number, part_number, project_number) and the relation is decomposed similarly stating constratint 3D. However, the definition of 5NF doesn't state about the constratint 3D.
So, I have a few questions on the scenario presented above:
What is constratint 3D?
What if the relation has one more attribute "region" making ACPR(agent, company, product, region)?