There are a bunch of tables in my database named courses, topics, subtopics, assessments.
One course can have multiple topics. One topic can have multiple subtopics. One topic can have multiple assessments.
There is a query I'm trying to implement in which I need a response in a specific format. For which aggregation has been used. I'm getting the desired result except for assessments.
The format I want is an array of courses. For each course, there is an array of topics. For each topic, there is an array of subtopics. This much I'm able to achieve. Now, I want for each topic an array of assessments just like subtopics which I'm unable to achieve.
The Code:
select courses.course_id as id, courses.course_name as name, courses.course_duration as
duration, courses.course_description as description,
jsonb_agg(
jsonb_build_object(
'id', topics.topic_id,
'name', topics.topic_name,
'duration', topics.topic_duration,
'sequence_no', topics.sequence_no,
'description', topics.topic_description,
'subtopics', subtopic.subtopics,
'assessment_id', assessments.id
)
order by topics.topic_id
) topics
from courses
left join topics on courses.course_id = topics.course_id
cross join lateral (
select jsonb_agg(
jsonb_build_object(
'id', subtopics.subtopic_id,
'name', subtopics.subtopic_name,
'assignment_id', subtopics.assignment_id,
'homework_id', subtopics.homework_id,
'on_free_trial', subtopics.on_free_trial
)
order by subtopics.subtopic_id
) subtopics
from subtopics
where subtopics.topic_id = topics.topic_id
) subtopic
left join assessments on assessments.topic_id = topics.topic_id
cross join lateral (
select jsonb_agg(
jsonb_build_object(
'id', assessments.id
)
order by assessments.id
) assessments
from assessments
where assessments.topic_id = topics.topic_id
) assessment
group by courses.course_id
order by courses.course_id
The output is as follows:
{
"Response": [
{
"id": 1,
"name": "Data Structures and Algorithms",
"duration": 32,
"description": "A data structure is a named location that can be used to store and organize data. And, an algorithm is a collection of steps to solve a particular problem. Learning data structures",
"topics": [
{
"id": 1,
"name": "ARRAYS AND VECTORS",
"duration": 7,
"subtopics": [
{
"id": 1,
"name": "Pre-Computations in Array",
"homework_id": 1,
"assignment_id": 1,
"on_free_trial": null
},
{
"id": 2,
"name": "Pre-Computations",
"homework_id": 1,
"assignment_id": 2,
"on_free_trial": null
}
],
"description": "Static and Dynamic Arrays, Pre-Computations, Array Rotations, Sliding Window Technique, Bucketization Technique etc.",
"sequence_no": 1,
"assessment_id": 2
},
{
"id": 1,
"name": "ARRAYS AND VECTORS",
"duration": 7,
"subtopics": [
{
"id": 1,
"name": "Pre-Computations in Array",
"homework_id": 1,
"assignment_id": 1,
"on_free_trial": null
},
{
"id": 2,
"name": "Pre-Computations",
"homework_id": 1,
"assignment_id": 2,
"on_free_trial": null
}
],
"description": "Static and Dynamic Arrays, Pre-Computations, Array Rotations, Sliding Window Technique, Bucketization Technique etc.",
"sequence_no": 1,
"assessment_id": 1
},
{
"id": 2,
"name": "MATHS",
"duration": 7,
"subtopics": null,
"description": "Modular arithmetic, Combinatorics, Euclid’s Algorithm for GCD, Primality Testing : Sieve of Eratosthenes, Fast Factorization, Catalan Numbers",
"sequence_no": 2,
"assessment_id": null
}
]
},
{
"id": 2,
"name": "OPERATING SYSTEMS",
"duration": 30,
"description": "An exhaustive collection of all the must-know concepts of Operating Systems",
"topics": [
{
"id": 100,
"name": "Threads and Processes",
"duration": 4,
"subtopics": [
{
"id": 5,
"name": "Basics of Threads and Processes",
"homework_id": 100,
"assignment_id": 100,
"on_free_trial": null
},
{
"id": 6,
"name": "Inter Process Communication",
"homework_id": null,
"assignment_id": null,
"on_free_trial": null
}
],
"description": "Difference b/w Threads and Processes, PCB",
"sequence_no": 1,
"assessment_id": null
},
{
"id": 101,
"name": "Memory Management",
"duration": 3,
"subtopics": [
{
"id": 7,
"name": "Logical And Physical Memory",
"homework_id": 101,
"assignment_id": 101,
"on_free_trial": null
}
],
"description": "Logical and Physical Memory, Paging, Fragmentation",
"sequence_no": 2,
"assessment_id": null
}
]
},
{
"id": 3,
"name": "Python",
"duration": 4,
"description": "Basics of python language.",
"topics": [
{
"id": 3,
"name": "Python list",
"duration": 1,
"subtopics": null,
"description": "Basics of python.",
"sequence_no": 1,
"assessment_id": null
}
]
}
]
}
As one can see the topic is getting repeated because of assessment_id. I want it to be structured in form of an array Or array of objects as subtopics are.
You forgot to use the assessments in the json_build_object as you did with the subtopics:
select courses.course_id as id, courses.course_name as name, courses.course_duration as
duration, courses.course_description as description,
jsonb_agg(
jsonb_build_object(
'id', topics.topic_id,
'name', topics.topic_name,
'duration', topics.topic_duration,
'sequence_no', topics.sequence_no,
'description', topics.topic_description,
'subtopics', subtopic.subtopics,
'assessments', assessments.assessments
)
order by topics.topic_id
) topics
...
Related
I have the following table with one row. I have tried to query this table to return authors under the age of 40 and have been unable to do so.
CREATE TABLE bookstuff (
data JSON
);
insert into bookstuff(data)
VALUES('
{
"the_books": {
"publishers": [
{
"name": "Dunder Mifflin",
"address": "Scranton, PA",
"country": "USA",
"CEO": "David Wallace"
},
{
"name": "Sabre",
"address": "Tallahassee, FL",
"country": "USA",
"CEO": "Jo Bennett"
},
{
"name": "Michael Scott Paper company",
"address": "Scranton, PA",
"country": "USA",
"CEO": "Michael Gary Scott"
},
{
"name": "Vance Refrigeration",
"address": "Scranton, PA",
"country": "USA",
"CEO": "Bob Vance"
}
],
"authors": [
{
"id": 1,
"name": "Michael Scott",
"age": 45,
"country": "USA",
"agentname": "Jan Levinson",
"books_written": "book1"
},
{
"id": 2,
"name": "Jim Halpert",
"age": 35,
"country": "USA",
"agentname": "Pam Beesly",
"books_written": "book3"
},
{
"id": 3,
"name": "Dwight Schrute",
"age": 40,
"country": "USA",
"agentname": "Angela Martin",
"books_written": "book2"
},
{
"id": 4,
"name": "Pam Halpert",
"age": 35,
"country": "USA",
"agentname": "Angela Martin",
"books_written": "book4"
}
],
"books": [
{
"isbn": "book1",
"title": "The Dundies",
"price": 10.99,
"year": 2005,
"publishername": "Dunder Mifflin"
},
{
"isbn": "book2",
"title": "Bears, Beets, Battlestar Galactica",
"price": 8.99,
"year": 2006,
"publishername": "Dunder Mifflin"
},
{
"isbn": "book3",
"title": "The Sabre Store",
"price": 12.99,
"year": 2007,
"publishername": "Sabre"
},
{
"isbn": "book4",
"title": "Branch Wars",
"price": 14.99,
"year": 2015,
"publishername": "Sabre"
}
]
}
}');
I have tried the following query to get the author's age
SELECT data->'the_books'->'authors'
FROM bookstuff
WHERE (data->'the_books'->'authors'->>'age')::integer > 40;
I expect it to return two values 'Jim halpert' and 'pam halpert' but instead I get no result back, not even null.
I have also tried this query, just to see if i could get anything back at all from the table and still no results:
SELECT data->'the_books'->'authors'
FROM bookstuff
where (data->'the_books'->'authors'->'name')::jsonb ? 'Michael Scott';
I'm new to postgresql, is there a different way I should be going about this?
Using json_array_elements:
select (v -> 'name')#>>'{}' from bookstuff b
cross join json_array_elements(b.data -> 'the_books' -> 'authors') v
where ((v -> 'age')#>>'{}')::int < 40
See fiddle
Another option, slightly more verbose:
select distinct(author->>'name') as author_name from
(select json_array_elements(b.data->'the_books'->'authors') author from bookstuff b) author
where (author->>'age')::int < 40
The distinct might be unnecessary if you really just have one database row and no duplicates in the authors array of that row.
Three considerations of why your final solution doesn't work
where filters out rows - this happens before the 'select'. the row contains everything in this case.
'?' predicate matches an array containing your choosen value "Does the key/element string exist within the JSON value?" You don't have a simple array here array->'key' doesn't pick that attribute into a new array
your select projection isn't called however it was it would contain the whole array (remember where doesn't transform just filters out rows)
This is my Apartment integrated with services
"id": 26,
"user_id": 1,
"title": "SAN MARINO",
"slug": "san-marino",
"rooms": 1,
"bathrooms": 1,
"beds": 1,
"squared_meters": 12,
"address": "San Marino, Carpi",
"latitude": "44.80924",
"longitude": "10.91565",
"image": "apartment_image/BMRQZSXLdWviqDwmHgqLzrmzG1hJzJGOq7DujnRB.jpg",
"is_visible": 1,
"floor": 1,
"price": 120,
"description": "2",
"created_at": "2022-02-21T21:41:53.000000Z",
"updated_at": "2022-02-21T21:41:53.000000Z",
"services": [
{
"id": 2,
"name": "Posto Macchina",
"slug": "posto-macchina",
"created_at": "2022-02-21T08:59:53.000000Z",
"updated_at": "2022-02-21T08:59:53.000000Z",
"pivot": {
"apartment_id": 26,
"service_id": 2
}
}
]
}
I use to filter my collection( $apartment_list = ApartmentResource::collection(Apartment::with(['services'])->get());)
using multiple WHERE, like this: $apartment_list = $apartment_list->where('rooms', '>=', $rooms);
How can i filter my Apartment refering to the pivot column service_id or services[id]?
There is filter method for the collection. You may create a callback for a single apartament and filter in any way you want:
$apartment_list->filter(function ($apartament) {
// ...
});
you can use try with this:
$serviceId = value_of_service_id;
$apartment_list = ApartmentResource::collection(Apartment:: whereHas('services', function ($q) use ($serviceId) {
$q->where('service_id', $serviceId);
})->get());
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.