From b65ac299ed43006f3c0d6ff303e1c1525a6060ca Mon Sep 17 00:00:00 2001 From: Junwei Zhao Date: Tue, 25 Feb 2020 13:55:55 +1100 Subject: [PATCH] Add some db queries --- categories/database.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/categories/database.md b/categories/database.md index 79505de..6e32a09 100644 --- a/categories/database.md +++ b/categories/database.md @@ -75,7 +75,24 @@ CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass'; GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser; ``` -## Postgres trigger example +### Queries + +#### Query with json field + +```sql +select count(*), arguments ->> 'SomeProp', arguments -> 'FirstLevel' ->> 'SecondLevel' from jobs group by arguments ->> 'SomeProp', arguments -> 'FirstLevel' ->> 'SecondLevel' +``` +### Group by + +```sql +select job_ids from ( + select count(*) as c, string_agg(cast(job_id as varchar),',') as job_ids, args ->> 'ResourceID' args -> 'Measurement' ->> 'Property' + from jobs + group by args ->> 'ResourceID', args -> 'Measurement' ->> 'Property' +) as f1 where c > 1; +``` + +### Postgres trigger example Giving we have a table called users, and the columns are: ```bash