Postgresql-jsonb_pretty & Datestyle

Source: Internet
Author: User
Tags postgresql

1.

SHOW Datestyle;

Datestyle
-----------
ISO, MDY
(1 row)

INSERT into Container VALUES (' 13/01/2010 ');
Error:date/time field value out of range: "13/01/2010"
Hint:perhaps you need a different "datestyle" setting.

SET Datestyle = "ISO, DMY";
SET

INSERT into Container VALUES (' 13/01/2010 ');
INSERT 0 1

SET Datestyle = default;
SET

------------------------------------------------

2.


--http://www.silota.com/docs/recipes/sql-postgres-json-data-types.html
--https://www.postgresql.org/docs/9.3/static/functions-json.html
--https://stackoverflow.com/questions/26877241/ query-combinations-with-nested-array-of-records-in-json-datatype/26880705#26880705
--45091839

DROP TABLE IF EXISTS reports;

CREATE TABLE reports (rep_id int primary key, data JSON);

TRUNCATE TABLE reports;

INSERT into reports (rep_id, data)
VALUES
(1, ' {"Objects": [{"album": 1, "src": "Fooa.png", "POS": "Top"}, {"album": 2, "src": "Barb.png", "POS": "Top"}], "background ":" Background.png "}")
, (2, ' {"Objects": [{"album": 1, "src": "Fooa.png", "POS": "Top"}, {"album": 2, "src": "Barc.png", "POS": "Top"}], "Backgroun D ":" Bacakground.png "}")
, (3, ' {"Objects": [{"album": 1, "src": "Fooa.png", "POS": "Middle"},{"album": 2, "src": "Barb.png", "POS": "Middle"}], " Background ":" Background.png "}")
, (4, ' {"Objects": [{"album": 1, "src": "Fooa.png", "POS": "Top"}, {"album": 3, "src": "Barb.png", "POS": "Top"}], "Backgroun D ":" Backgrounda.png "}")
;

/*1. Example 1 */
/*
SELECT Array_agg (r.rep_id) as IDs, COUNT (*) as CT
From reports R
, Json_array_elements (r.data-> ' objects ') o

where o->> ' pos ' in (' Top ', ' fooc.png ')

GROUP by r.data->> ' background '
, O->> ' album '
, o->> ' SCR '
ORDER by Count (*) DESC
LIMIT 3;
*/

/* 2. Example 2 is what we wanted.*/

SELECT DISTINCT on (rep_id) r.rep_id as Id2,r.data,r.data-> ' objects ' objects
From reports R
, Json_array_elements (r.data-> ' objects ') o

where o->> ' pos ' in (' Top ', ' middle2222 ')
ORDER by r.rep_id DESC

/* 3. Format output JSON String */

/*
Select Jsonb_pretty (' {' name ': "Alice", "Agent": {"bot": True}} ':: JSONB);
--Returns the following
{
"Name": "Alice",
"Agent": {
"Bot": true
}
}
*/

/* 4. Update
Update sales Set info = info | | ' {' Country ': ' Canada '} ';

Use the | | operator to concatenate existing data with new data.
The operator would either update or insert the key to the existing document.

*/

Postgresql-jsonb_pretty & Datestyle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.