473,609 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
I have table which has a column of type json with name "demographi c". 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 1859

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

Similar topics

2
3637
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 to speed, I bought the Dietel book How To Program 5th ed. I am going through some examples trying to work them out, and have been doing pretty good, but came across one exercise that has me stumped. It's a card shuffling program that deals 5...
6
5199
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 Duration (int) - the duration in minutes of the work block. In another table called OvertimeRates I have information about rate multipliers and a column that tells me the TIME that the rate
1
11037
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 primary key consists of an OrderNo and DetailSeqNo?
4
6446
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 JDBC. Assume we have a table named "test". Under PSQL, we can input "\d test" to see the details about table "test" successufully. However, my main purpose is trying to get column name, column name of a table through JDBC.
4
16966
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 Error: Column name or number of supplied values does not match table definition. I'm using MS-SQL Server. My code is:
1
10741
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 values are comming 1,10,11,2,20.... etc like. but i need 1,2,3,10,22 like. please tell me the solution for this problem. thanks and regards,
7
2285
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 lastname, there will be a lot of firstnames
2
2060
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 results I want. Picture this: $BaseColor = BLACK $ColorOne = YELLOW $ColorTwo = DARKGREY What I need is a way to compare the colors so that it returns DARKGREY.
1
3756
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 this, i have added an Autoincrement Column to the DataTable so that i can use a filter on the Autoincrement Column to get the desired 20 records from the DataTable. I have used the following code Dim dc As New...
0
1155
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 values comes from another datatable: { "YMDH": "2009-03-07 00:00:00.000", "SELL_DURATION": 222.999995 }, { "YMDH": "2009-03-07 01:00:00.000", "SELL_DURATION": 75.816664 }, { "YMDH": "2009-03-07 02:00:00.000", "SELL_DURATION": 39.349995 }, { "YMDH":...
0
8145
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8556
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6068
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5526
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4037
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2541
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1690
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.