当我有一个联接表时,如何查询总数

问题描述

| 哈o 我有一个连接表,说的是tableA和tableB。 tableA有一个称为Amount的列。 tableB有一列称为refID。当refID具有相同的值时,我想总计Amount列。我在查询中使用了SUM,但它抛出了一个错误
ORA-30483: window  functions are not allowed here
30483. 00000 -  \"window  functions are not allowed here\"
*Cause:    Window functions are allowed only in the SELECT list of a query.
           And,window function cannot be an argument to another window or group
           function.
这是我的查询供您参考:
select *
from (
       select SUM(A.Amount),B.refId,Rank() over (partition by B.refID order by B.id desc) as ranking
       from table A
       left outer join table B on A.refID = B.refID
)
where ranking=1;
我可以知道是否还有其他解决方案可以让我求和? 谢谢 @!     

解决方法

SELECT  *
FROM    (
        SELECT  A.Amount,B.refId,Rank() over (partition by A.refID order by B.id desc) as ranking,SUM(amount) OVER (PARTITION BY a.refId) AS asum
        FROM    tableA A
        LEFT JOIN
                tableB B
        ON      B.refID = A.refID
        )
WHERE   ranking = 1
    ,
select 
  SUM(A.Amount),B.refId
from table A
  left outer join table B on A.refID = B.refID
GROUP BY 
  B.refId
    ,
  Declare @T table(id int) 
    insert into @T values (1),(2)
  Declare @T1 table(Tid int,fkid int,Amount int)
    insert into @T1 values (1,1,200),(2,250),(3,2,100),(4,25)

    Select SUM(t1.Amount) as amount,t1.fkid as id from @T t
 left outer join @T1 t1 on t1.fkid = t.id group by t1.fkid
    ,
SELECT refid,sum(a.amount)
FROM table AS a LEFT table AS b USING (refid)
GROUP BY refid;
    ,我有点困惑。您发布的查询在任何地方都没有SUM函数,并对其自身执行了名为““ TABLE \”的表的自联接。我猜您实际上有两个表(我将它们称为TABLE_A和TABLE_B),在这种情况下,应执行以下操作:
SELECT a.REFID,SUM(a.AMOUNT)
  FROM TABLE_A a
  INNER JOIN TABLE_B b
    ON (b.REFID = a.REFID)
  GROUP BY a.REFID;
如果我理解您的问题,则仅当您拥有与TABLE_A.REFID匹配的TABLE_B.REFID时才需要结果,因此使用INNER JOIN是合适的。 分享并享受。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...