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