Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE teachers (id SERIAL, first_name VARCHAR(60), last_name VARCHAR(60));
- CREATE TABLE training_modules (id SERIAL, course VARCHAR(60), teachers_id INTEGER[]);
- teacher_name id_count
- bob teacher 4
- sally lady 3
- jimbo jones 5
- SELECT tid, count(*) as id_count FROM training_modules tm, unnest(tm.teachers_id) as tid GROUP BY tid;
- SELECT t.id, concat(t.first_name, ' ', t.last_name) AS teacher_name, tm.id_count FROM (SELECT unnest(training_modules.teachers_id) AS id_count, count(*) FROM training_modules GROUP BY id_count) AS tm INNER JOIN teachers t ON tm.id_count = t.id;
- id | t_name | tc
- ----+-----------------+----
- 5 | Jimbo Jones | 5
- 4 | Frank McGee | 4
- 6 | Sara Sarason | 6
- 2 | Joshua Jesps | 2
- 1 | Larry Bucatin | 1
- 3 | Natalie Fatali | 3
- SELECT t.id, concat_ws(' ', t.first_name, t.last_name) AS teacher_name, tm.id_count
- FROM (
- SELECT unnest(tm.teachers_id) AS id, count(*) AS id_count
- FROM training_modules tm
- GROUP BY 1
- ) AS tm
- JOIN teachers t USING (id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement