当前位置: 动力学知识库 > 问答 > 编程问答 >

web - Join and group by in Derby SQL

问题描述:

I want to join 2 tables A and B, and on the result i want to group duplicate column and sum other column. and i used with :

SELECT VALUE, SUM(ARATING) FROM (SELECT A.QID, A.VALUE, B.ARating

FROM A JOIN B

ON A.QID = B.QID

AND B.Nickname='someuser')

GROUP BY VALUE;

And I get an error for some reason.

网友答案:

In MySQL, you need a table alias on the subquery:

SELECT VALUE, SUM(ARATING)
FROM (SELECT A.QID, A.VALUE, B.ARating  
      FROM A JOIN
           B
           ON A.QID = B.QID AND B.Nickname = 'someuser'
     ) ab
GROUP BY VALUE;

However, you don't need the subquery at all:

SELECT A.VALUE, SUM(B.ARATING)
FROM A JOIN
     B
     ON A.QID = B.QID AND B.Nickname = 'someuser'
GROUP BY A.VALUE;
分享给朋友:
您可能感兴趣的文章:
随机阅读: