When I run a query, these are the results presented to me:
id account_id score active item_id
5 78 9 true 4
6 78 1 true 4
7 78 9 true 6
8 78 5 true 7
9 78 5 true 8
10 78 5 true 8
I'd like the output to look like this by combining item_id's based on score:
id account_id score active item_id
* 78 10 true 4
7 78 9 true 6
8 78 5 true 7
* 78 10 true 8
My query that returns that info looks like this:
SELECT item.id, item.account_id, itemaudit.score, itemrevision.active, itemaudit.item_id
from item
left join itemrevision on item.id = itemrevision.id
join itemaudit on item.id = itemaudit.id
where itemrevision.active = true
;
The bit I'm missing is when 'item_id' is not distinct, combine/sum the value of 'score'. I'm not sure how to do this step.
The schema looks like this:
CREATE TABLE item
(id integer, account_id integer);
CREATE TABLE itemaudit
(id integer, item_id integer, score integer);
CREATE TABLE itemrevision
(id int, active boolean, item_id int);
INSERT INTO item
(id, account_id)
VALUES
(5, 78),
(6, 78),
(7, 78),
(8, 78),
(9, 78),
(10, 78)
;
INSERT INTO itemaudit
(id, item_id, score)
VALUES
(5, 4, 5),
(6, 4, 1),
(7, 6, 9),
(8, 7, 10),
(9, 8, 1),
(10, 8, 9)
;
INSERT INTO itemrevision
(id, active, item_id)
VALUES
(5, true, 4),
(6, true, 4),
(7, true, 6),
(8, true, 7),
(9, true, 7),
(10, true, 8)
;
CREATE TABLE ASfor you...