0

I am using postgres database for my application.

I have below data

    create table contacts (
        id int,
        contact_data jsonb
    );
    
    insert into contacts values
    (1,   '{
        "tags": [
            "MOCK_DATA (4)"
        ],
        "Email": "[email protected]",
        "reach": false,
        "Gender": "Male",
        "Interest": [
            "Dance"
        ],
        "Last Name": "Kynman",
        "First Name": "Nicko"
    }'),
    (2,   '{
        "ltv": "6",
        "City": "Bengaluru",
        "Email": "[email protected]",
        "State": "Karnataka",
        "Country": "India",
        "latitude": 12.9715987,
        "Last Name": "World",
        "longitude": 77.5945627,
        "First Name": "Helo"
    }'),
    (3,   '{
        "ltv": "11",
        "Email": "[email protected]",
        "reach": false,
        "Gender": "Female",
        "Country": "United States",
        "latitude": "37.09024",
        "Last Name": "Fortye",
        "longitude": "-95.712891",
        "First Name": "Sissie"
    }');
    Select * from contacts;
    
    +====+==================================================================================================================================+
    | id | contact_data                                                                                                                     |
    +====+==================================================================================================================================+
    | 1  | {"tags": ["MOCK_DATA (4)"], "Email": "[email protected]", "reach": false, "Gender": "Male", "Interest": ["Dance"], "Last... |
    +----+----------------------------------------------------------------------------------------------------------------------------------+
    | 2  | {"ltv": "6", "City": "Bengaluru", "Email": "[email protected]", "State": "Karnataka", "Country": "India", "latitude": 12.9715987, "L... |
    +----+----------------------------------------------------------------------------------------------------------------------------------+
    | 3  | {"ltv": "11", "Email": "[email protected]", "reach": false, "Gender": "Female", "Country": "United States", "latitude": "3... |
    +----+----------------------------------------------------------------------------------------------------------------------------------+

I am trying to fetch id who has ltv between 2 to 9. and it's giving 1 record. which is correct

    SELECT id from contacts where ( contact_data->> 'ltv' > '2' and contact_data->> 'ltv' < '9' );
    +====+
    | id |
    +====+
    | 2  |
    +----+

Issue:

Now i am trying between 2 to 12, which should give 2 records, but it's giving 0 data.

SELECT id from contacts where ( contact_data->> 'ltv' > '2' and contact_data->> 'ltv' < '12' );

Can anyone tell me what's the issue here. why I am not getting the value? here is sql-fiddle

2 Answers 2

1

In PostgreSQL ->> operator will return a text value. So you have too cast it in respective type.

Try below query:

SELECT id 
from contacts 
where ( 
    (contact_data->> 'ltv')::int > 3 and 
    (contact_data->> 'ltv')::int < 12 
);

DEMO Only thing you have to take care is, itv must contain integer value only, otherwise casting will throw error.

Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at the data type of ltv. They are strings and not numbers, so it is ordered lexicographically.

Cast it in the query to give it the meaning you want it to have.

EDIT: remove using number in the JSON, as retrieving a number field form JSON seems to not give a numeric value.

5 Comments

i tried with number but still it's not showing any data
What exactly did you try?
Try the casting after the JSON access, as this table might say you get always text if you retrieve something from a JSON: postgresql.org/docs/current/…
So at the time of insertion I inserted LTV as number. and tried below query SELECT id from contacts where ( contact_data->> 'ltv' > 2 and contact_data->> 'ltv' < 12 );
this will also now work. You should try like this SELECT id from contacts where ( (contact_data->> 'ltv')::int > 3 and (contact_data->> 'ltv')::int < 12 ); . No matter you are providing value 6 or "6". Only thing never store a non interger value in ltv

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.