473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

[POSTGRESQL] - Get key name from json column after comparing keys values?

I have table which has a column of type json with name "demographic". I want to compare keys by their values predefined by me and the result of that should be the name of the key and not the value which i will save to another column's rows.

The json columns looks like a this:
Expand|Select|Wrap|Line Numbers
  1. {"he" : {"he_vakiy" : 29384, "he_naiset" : 14803, "he_miehet" : 14581, "he_kika" : 1485, "he_0_2" : 850, "he_3_6"  } }
I want to compare f.ex "he_naiset" and "he_miehet" and the one that has higher value should return the name of the "key" ;



Ive tried something like this:

Expand|Select|Wrap|Line Numbers
  1. select greatest(demographics->'he'->>'he_miehet',demographics->'he'->>'he_naiset') as greatest from demographics;

Which will compare the values and return the higest values. But i dont know how i can get the name of the key.

I also tried something like this from :

Expand|Select|Wrap|Line Numbers
  1. with cte as
  2. (
  3.    select *
  4.         , genre.key as genre
  5.         , row_number() over (partition by id order by value desc) as ord
  6.      from base.maakunta
  7.     cross
  8.      join lateral json_each_text(base.maakunta.demographic->'he') genre
  9.  
  10. )
  11. select  genre, value,ord
  12.   from cte
  13. where ord = 1
  14. ;
But this one compares all the keys inside the json .
Mar 20 '22 #1
0 1842

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chaz | last post by:
Hello, I hope someone can help me out. I am going to be taking the third step in a programming class soon(I took the previous two a while ago at a different school) and in an effort to get back up...
6
by: teddysnips | last post by:
I have a table called WorkItem. It models a chunk of work done during a working day. It has two columns that I'm interested in: Start (smalldatetime) - the TIME the work block is begun...
1
by: BStorm | last post by:
Does anyone know the best way to update a DataTable row column using specific values for a multipart primary key? For example, updating an OrderAmt column in an OrderDetail table where the...
4
by: Ying Lu | last post by:
Hello, Under mysql, we have "desc tablename" to get the detail information about a table. My question is about to get column name, and column type for a specific table under PostgreSQL through...
4
by: prasath03 | last post by:
Dear All, Can anybody help me about my coding, i'm inserting the image file to database using jsp. When i execute the code, the following error is occurred: java.sql.SQLException: Insert...
1
by: rompicherla | last post by:
Hai every body, i am using postgres 7.3 version, For a varchar (string) data column, in this column i filled the numbers (1,2,3 ...10,11,..... etc) alone. i sorted this column, but sorting...
7
nev
by: nev | last post by:
i declared 2 datatables: dim dtt1 as datatable = ds.datatable1 dim dtt2 as datatable = ds.datatable2 dtt1 has firstname, lastname columns dtt2 has firstname column when i filter dtt1 by...
2
MarkoKlacar
by: MarkoKlacar | last post by:
Does anyone know a good script for comparing RBG values? The values are hexadecimal and I need to find a color that is closest to the one I'm comparing. I've tried a lot of stuff but I can't get the...
1
rizwan6feb
by: rizwan6feb | last post by:
I have a DataTable with thousands of records, i want to show these records on per page basis ( i.e a DataGridView showing first 20 records and next button to show next 20 records ...) To achieve...
0
by: Radha Shyam | last post by:
My question is how can I combine two sets of JSON for example one set comes from one datatable like this: "ID": 1, "SHORT_NAME": "B", "CARRIER_NAME": "Carrier A" Another one with multiple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.