是否可以根据 SQL- PostgreSQL 中的 CHILD table 订购 parent table?
Is it possible to ORDER a parent table based on a CHILD table in SQL- PostgreSQL?
甚至可以通过 Child table 订购 Parent Table 吗?
假设我有 person_ID(来自 parent table),person_ID 会在 child table 中获得很多选票。这么多票可以在childtable中换一person_id在parenttable。选票可以是+1 或-1。因此,如果 person_ID=001 收到 5 x(+1 票)和 2 x(-1 票)。 person_ID=001 的总分是 +3。假设 person_ID=002 有 3 x (-1) 票。 Person_ID=002 的得分为 -3。 Person_ID=003 得分为 0。
现在假设我想按 child table 的总分排序 Parent table。
我知道如何对 Child table 求和,但现在我需要根据 child 的结果对 parent table 求和table。
SELECT
parent_id2,
SUM (votes) AS total
FROM childtbl GROUP BY parent_id2
ORDER BY total DESC
我希望查询结果如下所示
Person_ID001 | +3
Person_ID003 | 0
Person_ID002 | -3
这里是link到SQLFiddle。 http://www.sqlfiddle.com/#!17/ea694/6
这里是代码,如果它搞砸了
CREATE TABLE parenttbl
(
parent_id integer,
test character(25),
test2 character(25),
PRIMARY KEY (parent_id)
);
INSERT INTO parenttbl (parent_id, test, test2) VALUES (1,'adam','test'), (2,'steven','test'), (3, 'bob','test');
CREATE TABLE childtbl
(
child_id integer,
parent_id2 integer,
votes integer,
PRIMARY KEY (child_id),
FOREIGN KEY (parent_id2)
REFERENCES parenttbl (parent_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
INSERT INTO childtbl (child_id, parent_id2, votes) VALUES (1,1,1), (2,1,1), (3, 1, 1),(4,1,-1), (5,2,1), (6, 2, 1),(7,3,-1), (8,3,-1), (9, 3, -1),(10, 1, 1),(11,3,1),(12,3,1),(13,3,1)
谢谢大家!
如果我没听错,你只需要left join
、聚合和排序:
SELECT p.parent_id, COALESCE(SUM(c.votes), 0) total_votes
FROM parenttbl p
LEFT JOIN childtbl c ON c.parent_id2 = p.parent_id
GROUP BY p.parent_id
ORDER BY total_votes DESC
left join
允许 parents 没有投票,在这种情况下 COALESCE(SUM(c.votes), 0)
给出 0
.
您可能还想 pre-aggregate child table,因此您不需要在外部查询中聚合 - 这使得 return parent table 中的更多列(而在前一个查询中,您需要将每一列添加到 group by
子句):
SELECT p.*, COALESCE(c.votes, 0) total_votes
FROM parenttbl p
LEFT JOIN (
SELECT parent_id2, SUM(votes) votes FROM childtbl GROUP BY parent_id2
) c ON c.parent_id2 = p.parent_id
ORDER BY total_votes DESC
甚至可以通过 Child table 订购 Parent Table 吗? 假设我有 person_ID(来自 parent table),person_ID 会在 child table 中获得很多选票。这么多票可以在childtable中换一person_id在parenttable。选票可以是+1 或-1。因此,如果 person_ID=001 收到 5 x(+1 票)和 2 x(-1 票)。 person_ID=001 的总分是 +3。假设 person_ID=002 有 3 x (-1) 票。 Person_ID=002 的得分为 -3。 Person_ID=003 得分为 0。
现在假设我想按 child table 的总分排序 Parent table。
我知道如何对 Child table 求和,但现在我需要根据 child 的结果对 parent table 求和table。
SELECT
parent_id2,
SUM (votes) AS total
FROM childtbl GROUP BY parent_id2
ORDER BY total DESC
我希望查询结果如下所示
Person_ID001 | +3
Person_ID003 | 0
Person_ID002 | -3
这里是link到SQLFiddle。 http://www.sqlfiddle.com/#!17/ea694/6
这里是代码,如果它搞砸了
CREATE TABLE parenttbl
(
parent_id integer,
test character(25),
test2 character(25),
PRIMARY KEY (parent_id)
);
INSERT INTO parenttbl (parent_id, test, test2) VALUES (1,'adam','test'), (2,'steven','test'), (3, 'bob','test');
CREATE TABLE childtbl
(
child_id integer,
parent_id2 integer,
votes integer,
PRIMARY KEY (child_id),
FOREIGN KEY (parent_id2)
REFERENCES parenttbl (parent_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
);
INSERT INTO childtbl (child_id, parent_id2, votes) VALUES (1,1,1), (2,1,1), (3, 1, 1),(4,1,-1), (5,2,1), (6, 2, 1),(7,3,-1), (8,3,-1), (9, 3, -1),(10, 1, 1),(11,3,1),(12,3,1),(13,3,1)
谢谢大家!
如果我没听错,你只需要left join
、聚合和排序:
SELECT p.parent_id, COALESCE(SUM(c.votes), 0) total_votes
FROM parenttbl p
LEFT JOIN childtbl c ON c.parent_id2 = p.parent_id
GROUP BY p.parent_id
ORDER BY total_votes DESC
left join
允许 parents 没有投票,在这种情况下 COALESCE(SUM(c.votes), 0)
给出 0
.
您可能还想 pre-aggregate child table,因此您不需要在外部查询中聚合 - 这使得 return parent table 中的更多列(而在前一个查询中,您需要将每一列添加到 group by
子句):
SELECT p.*, COALESCE(c.votes, 0) total_votes
FROM parenttbl p
LEFT JOIN (
SELECT parent_id2, SUM(votes) votes FROM childtbl GROUP BY parent_id2
) c ON c.parent_id2 = p.parent_id
ORDER BY total_votes DESC