0

I have a Trip table in PostgreSQL DB, there is a column called meta in the table.

A example of meta in one row looks like:

meta = {"runTime": 3922000, "distance": 85132, "duration": 4049000, "fuelUsed": 19.595927498516176}

To select the trip which has largest value divided by "distance" and "runTime", I run query:

select MAX(tp."meta"->>'distance'/tp."meta"->>'runTime') maxkph FROM "Trip" tp

but I get ERROR:

/* ERROR:  operator does not exist: unknown / jsonb LINE 1: MAX(tp."meta"->>'distance'/tp."meta"...

I also tried:

select MAX((tp."meta"->>'distance')/(tp."meta"->>'runTime')) maxkph FROM "Trip" tp

but get another ERROR:

/* ERROR:  operator does not exist: text / text LINE 1: ...MAX((tp."meta"->>'distance')/(tp."meta...

Could you please help me to solve this problem?

2 Answers 2

2

There is not operator div for jsonb values. You have to cast a values on both sizes to some numeric type first:

MAX( ((tp."meta"->>'distance')::numeric) / ((tp."meta"->>'runTime')::numeric) ) maxkph
Sign up to request clarification or add additional context in comments.

Comments

1

Try using parentheses:

MAX( (tp."meta"->>'distance') / (tp."meta"->>'runTime') ) as maxkph

Your second problem suggests that these values are stored as strings. So convert them:

MAX( (tp."meta"->>'distance')::numeric / (tp."meta"->>'runTime')::numeric ) as maxkph

1 Comment

Many thanks for check my question. I have tried your method, but get another error. I have modified my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.