There is a table of photos (photo_id, exam_id, photo) .
photo_id is the primary key. exam_id - foreign key, photo - binary data (in PostgreSQL, this is bytea )
I want to display the tablet using only SQL tools (exam_id, photo1, photo2, photo3) .
Made this sample: (exam_id, photo_id_1, photo_id_2, photo_id_3)
SELECT photos.exam_id, last(CASE WHEN num=1 THEN photos.exam_photo ELSE null END) id_1, max(CASE WHEN num=2 THEN photos.exam_photo_id ELSE null END) id_2, max(CASE WHEN num=3 THEN photos.exam_photo_id ELSE null END) id_3 FROM (SELECT exam_id, row_number() over(partition BY exam_id ORDER BY exam_photo_id) num, exam_photo_id, exam_photo FROM examsphotos ORDER BY exam_id) photos GROUP BY photos.exam_id; How to remake this script so that the sample was binary data, I can not think of. The forehead does not work, for bytea there are no suitable aggregating functions.