In my DB, there are two tables
EventType
ID (Primary key)
Name
ActivityType
ID (Primary key)
Name
EventTypeID (foreign key)
ParentActivityTypeID (Relation with self ID)
I have tried with the following query to aggregate the json
SELECT coalesce(json_build_object(
'EventTypeID', ev."ID",
'EventTypeName', ev."Name",
'ActivityType', json_agg(json_build_object('ID',ac."ID",'Name',ac."Name",'ParentActivityType',json_agg(select * from "Activity" where ))
), '{}'::json) AS item
FROM "EventType" as ev
JOIN "ActivityType" as ac ON ev."ID" = ac."EventTypeID"
GROUP BY ev."ID"
expected JSON output
[{
"EventTypeID": 2,
"EventTypeName": "On-Site Care",
"ActivityType": [
{
"ID": 1,
"Name": "Measurement",
"EventTypeID": 2,
"ParentActivityTypeID": null,
"SubActivityType": [
{
"ID": 17,
"Name": "abc",
"EventTypeID": 2,
"ParentActivityTypeID": 1
}
]
},
{
"ID": 2,
"Name": "Medication",
"EventTypeID": 2,
"ParentActivityTypeID": null
},
{
"ID": 3,
"Name": "Wellness check",
"EventTypeID": 2,
"ParentActivityTypeID": null
},
{
"ID": 4,
"Name": "Other",
"EventTypeID": 2,
"ParentActivityTypeID": null
}
]
},
{
"EventTypeID": 3,
"EventTypeName": "Care Call",
"ActivityType": [
{
"ID": 1,
"Name": "Measurement",
"EventTypeID": 3,
"ParentActivityTypeID": null,
"SubActivityType": [
{
"ID": 17,
"Name": "abc",
"EventTypeID": 3,
"ParentActivityTypeID": 1
}
]
},
{
"ID": 2,
"Name": "Medication",
"EventTypeID": 3,
"ParentActivityTypeID": null
},
{
"ID": 3,
"Name": "Wellness check",
"EventTypeID": 3,
"ParentActivityTypeID": null
},
{
"ID": 4,
"Name": "Other",
"EventTypeID": 3,
"ParentActivityTypeID": null
}
]
}
]
You can join self table as parent described as below.
SELECT coalesce(json_build_object(
'EventTypeID', ev."ID",
'EventTypeName', ev."Name",
'ActivityType', json_agg(json_build_object('ID',ac."ID",'Name',ac."Name",'ParentActivityType',json_agg(parent.*))
), '{}'::json) AS item
FROM "EventType" as ev
LEFT JOIN "EventType" as parent ON ev."ParentActivityTypeID" = parent."ID"
JOIN "ActivityType" as ac ON ev."ID" = ac."EventTypeID"
GROUP BY ev."ID"
Related
I have following table:
Id
HieararchyId
Name
1
/
MHz
2
/1/
Finance
3
/1/
IT
4
/1/3/
Software Development
5
/1/3/
QA
6
/1/2/
Accountant
and I want to get data for my TreeView like:
[
{
"Id": 1,
"HierarchyId": "/"
"Name":"MHz",
"Children":[
{
"Id": 3,
"HierarchyId": "/1/"
"Name":"IT",
"Children":[
{
"Id": 4,
"HierarchyId": "/1/3/"
"Name":"Software Development",
"Children":[]
}
{
"Id": 5,
"HierarchyId": "/1/3/"
"Name":"QA",
"Children":[]
}
]
},
{
"Id": 2,
"HierarchyId": "/1/"
"Name":"Finance",
"Children":[
{
"Id": 6,
"HierarchyId": "/1/2/"
"Name":"Accountant",
"Children":[]
}
]
}
]
}
]
I tried with this: How to generate hierarchical JSON data
Got error: No more lock classes available from transaction.
Or is it better creating recursive function and filling the Tree on client side?
Update
CREATE FUNCTION dbo.GetJson (#departmentId hierarchyid)
RETURNS nvarchar(max)
AS BEGIN
RETURN (
SELECT
Id,
HierarchyId,
Name,
children = JSON_QUERY(dbo.GetJson(HierarchyId))
FROM Departments p
WHERE p.HierarchyId.IsDescendantOf(#departmentId ) = 1
FOR JSON PATH
);
END;
The best way I have found to do this is to use a recursive function.
You can self-join all nodes and check if they are a child using GetAncestor
CREATE FUNCTION dbo.GetJson (#parentID hierarchyid)
RETURNS nvarchar(max)
AS BEGIN
RETURN (
SELECT
t.Id,
t.HierarchyId,
t.Name,
children = JSON_QUERY(dbo.GetJson(t.HierarchyId))
FROM Tree t
WHERE t.HierarchyId <> #parentID
AND t.HierarchyId.GetAncestor(1) = #parentID
FOR JSON PATH
);
END;
go
SELECT dbo.GetJson(hierarchyid::GetRoot());
db<>fiddle
Result
[
{
"Id": 1,
"HierarchyId": "/1/",
"Name": "MHz",
"children": [
{
"Id": 2,
"HierarchyId": "/1/2/",
"Name": "Finance",
"children": [
{
"Id": 6,
"HierarchyId": "/1/2/6/",
"Name": "Accountant"
}
]
},
{
"Id": 3,
"HierarchyId": "/1/3/",
"Name": "IT",
"children": [
{
"Id": 4,
"HierarchyId": "/1/3/4/",
"Name": "Software Development"
},
{
"Id": 5,
"HierarchyId": "/1/3/5/",
"Name": "QA"
}
]
}
]
}
]
I have just started my journey with Laravel and PHP.
I have this route:
Route::get('/admin/applications/staff-role', function () {
$staff = StaffRole::with('applications')->where('id', '=', '1')->get();
return $staff;
});
which is the result of hasManyThrough query and returns this result:
[
{
"id": 1,
"staff_role_label": "Staff 1",
"staff_role_description": "Staff Role 1",
"staff_role_abbreviation": "SR1",
"deleted_at": null,
"applications": [
{
"id": 1,
"vacancy_id": 1,
"hours_per_week": "9",
"contract_type": "Full-Time",
"desired_shift_option": "Weekends",
"name": "John",
"surname": "Doe",
"mobile_phone_number": "07911 123456",
"email": "test1#test.com",
"application_status": 1,
"is_staff": 1,
"staff_status": 0,
"deleted_at": null,
"laravel_through_key": 1
},
{
"id": 2,
"vacancy_id": 1,
"hours_per_week": "9",
"contract_type": "Full-Time",
"desired_shift_option": "Weekends",
"name": "Jane",
"surname": "Doe",
"mobile_phone_number": "07911 123456",
"email": "test2#test.com",
"application_status": 1,
"is_staff": 1,
"staff_status": 0,
"deleted_at": null,
"laravel_through_key": 1
}
]
}
]
So far, so good, but how can I count how many applications I have in the array?
Thanks.
First things first:
I'm using PostgreSQL 11.6, compiled by Visual C++ build 1800, 64-bit. :)
Im trying to create a JSON object directly from the database.
My desired result is
{
"1": [],
"2": [],
"3": []
}
Imagine my tables like:
MyIdTable
_id_|__key__
1 test1
2 test2
3 test3
MyKeyValueTable
__id__|__fkidmyidtable__|__value__
1 1 test
2 1 test1
3 2 test2
4 2 test3
Now I create a query
select
json_build_object(
a.id,
json_agg(
b.*
)
)
from "MyIdTable" a
inner join "MyKeyValueTable" b on a.id = b.fkidmyidtable group by a.id
This will get me as result, multiple rows with the desired result:
row 1: {
"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }]
}
row 2: {
"2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }]
}
After this I can use json_agg() to create almost my desired result. The issue is that it will create
[ { "json_build_object": {"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }]}, "json_build_object": { "2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }] }]
I would like to know if its possible to write a query to merge my created object into one json object like:
{
"1": [{ "id": 1, "fkidmyidtable": 1, "value": "test" }, { "id": 2, "fkidmyidtable": 1, "value": "test1" }],
"2": [{ "id": 3, "fkidmyidtable": 2, "value": "test2" }, { "id": 4, "fkidmyidtable": 2, "value": "test3" }]
}
Thank you very much in advance for taking the time to read :)!
If I followed you correctly, you can add another level of aggregation and use json_object_agg():
select json_object_agg(id, js) res
from (
select a.id, json_agg(b.*) js
from "MyIdTable" a
inner join "MyKeyValueTable" b on a.id = b.fkidmyidtable
group by a.id
) t
type Item struct {
TopicId int `json:"topic_id"`
Topic *Topic `json:"topic,omitempty"`
BotId int `json:"bot_id"`
URL string `gorm:"varchar(250);unique" json:"url"`
Title string `gorm:"varchar(250)" json:"title"`
}
type Topic struct {
Title string `gorm:"varchar(250)" json:"title"`
Items []*Item `json:"items,omitempty"`
}
Here is the two models. I want to query the Topics with each have 5 latest items.
Without the limit for items, I could do this by,db.Model(&Topic{}).Preload("Items").
When I try to add some limit conditions to items:
db.Model(&Topic{}).Preload("Items", func(db *gorm.DB) *gorm.DB {
return db.Order("title DESC").Limit(5)
})
It will return 5 items in total, not 5 items for each topic.
Actual result:
"records": [
{
"id": 4,
"created_on": "2019-08-11T10:28:54.910022Z",
"title": "Topic 1",
},
{
"id": 5,
"created_on": "2019-08-11T10:29:26.952614Z",
"title": "Programming",
},
{
"id": 6,
"created_on": "2019-08-11T10:34:16.040229Z",
"title": "Topic 3",
"items": [
{
"id": 1,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title One",
},
......
{
"id": 5,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title five",
}
]
Expected results:
"records": [
{
"id": 4,
"created_on": "2019-08-11T10:28:54.910022Z",
"title": "Topic 1",
},
{
"id": 5,
"created_on": "2019-08-11T10:29:26.952614Z",
"title": "Programming",
"items": [
{
"id": 6,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 5,
"title": "Title six",
},
......
{
"id": 10,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 5,
"title": "Title ten",
}]
},
{
"id": 6,
"created_on": "2019-08-11T10:34:16.040229Z",
"title": "Topic 3",
"items": [
{
"id": 1,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title One",
},
......
{
"id": 5,
"created_on": "2019-08-27T14:23:17.766055Z",
"topic_id": 6,
"title": "Title five",
}
]
The actual sql it generated is SELECT * FROM "item" WHERE "topic_id" IN (6,4,5) DESC LIMIT 5
It's obvious not the results I want, so how should I get the expected result with gorm?
For postgresql only.
type Topic struct {
Title string `gorm:"varchar(250);PRIMARY KEY" json:"title"`
// assume the foreign key between two tables are both Title.
Items []*Item `gorm:"foreignkey:Title;association_foreignkey:Title" json:"items,omitempty"`
}
var topics []Topic
db.Model(&Topic{}).Preload("Items", func(tx *gorm.DB) *gorm.DB {
return tx.Joins(`JOIN LATERAL (
SELECT i.url FROM items i WHERE i.title = items.title ORDER BY i.topic_id DESC LIMIT 5
) AS foo ON foo.url = items.url`)
}).Find(&topics)
You could use lateral join to limit the rows for each different value. After retrieving the rows of topics, gorm then send the following query to get the related rows from items:
SELECT "items".*
FROM "items"
JOIN LATERAL
(SELECT i.url
FROM items i
WHERE i.title = items.title
ORDER BY i.topic_id DESC
LIMIT 5) AS foo ON foo.url = items.url
WHERE ("title" IN (?))
Consider the following sql schema:
Where Table_2 and Table_1 have a many to many relation
now I'm trying to create an elastic search river that will pull in all the data from table_2 but I want the rows from table_1 as well and not just the id's.
Here is what I believe will be my sql:
select t2.*, t1.Name from [Table_2] t2
join [Table_3] t3 on t2.ID = t3.table_2
join [Table_1] t1 on t1.ID = t3.table_1
Now after doing this I have noticed that I get duplicate rows IE for each relationship in Table_3 I will get one row, I understand why this is but what I want is one entry for Table_2 that has an entry for table one.
This is what I'm getting in elastic now
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test_relation",
"_type": "relation",
"_id": "AUpUGlvaRCP4Gzd2p3K4",
"_score": 1,
"_source": {
"Name": [
"table_2test",
"Test1"
],
"ID": 1
}
},
{
"_index": "test_relation",
"_type": "relation",
"_id": "AUpUGlvaRCP4Gzd2p3K5",
"_score": 1,
"_source": {
"Name": [
"table_2test",
"Test2"
],
"ID": 1
}
}
]
}
}
But instead I want it to look like:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_relation",
"_type": "relation",
"_id": "AUpUGlvaRCP4Gzd2p3K4",
"_score": 1,
"_source": {
"Name": [
"table_2test",
],
Table_1 :[
{"Name": "Test1", "ID": "1"},
{"Name": "Test2", "ID": "2"}
]
"ID": 1
}
}
]
}
}
I was hoping to get away with using an elasticsearch river for sql but I'm not sure if it allows for this kind of query.