2

我正在努力使用计算总和来解决我的 SQL 表的问题。

列的总和应该显示的所有内容都是一个空白框!

下面的代码:

TTITLE CENTER ==================== SKIP 1-
CENTER 'U  T O O L' skip 1-
CENTER ==================== SKIP 1 - 
LEFT 'Tool Report 1.03' SKIP 1 -
LEFT ============ SKIP 2-
RIGHT 'Page:' -
FORMAT 999 SQL.PNO SKIP 2 

set pagesize 50

column MEMBERNAME HEADING 'Member Name'  format a20


compute sum of TOTAL on Rental_ID
Break on RENTAL_ID
select Member.Member_ID, SUBSTR(Member.FName,0,10) || SUBSTR(' ',0,10) ||
SUBSTR(Member.SName,0,15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name, 
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty*Rental_Line.Price,'L9,999.99') TOTAL
from Rental_Line
INNER JOIN Rental
on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member
on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance
on Rental_Line.Tool_Instance_ID = Tool_Instance.Tool_Instance_ID
INNER JOIN Tool
on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';
4

1 回答 1

0

可能这对你有帮助,因为我知道你需要SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID)

select Member.Member_ID,
       SUBSTR(Member.FName, 0, 10) || SUBSTR(' ', 0, 10) ||
       SUBSTR(Member.SName, 0, 15) as MEMBERNAME,
       Rental.Rental_ID,
       Tool.Name,
       Rental_Line.Qty,
       Rental_Line.Price,
       TO_Char(Rental_Line.Qty * Rental_Line.Price, 'L9,999.99') TOTAL,
       SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID) TOTAL_QTY,
       SUM(Rental_Line.Qty * Rental_Line.Price) OVER (PARTITION BY Rental.Rental_ID) TOTAL_SUM
  from Rental_Line
 INNER JOIN Rental on Rental.Rental_ID = Rental_Line.Rental_ID
 INNER JOIN Member on Rental.Member_ID = Member.Member_ID
 INNER JOIN Tool_Instance on Rental_Line.Tool_Instance_ID =
                             Tool_Instance.Tool_Instance_ID
 INNER JOIN Tool on Tool_Instance.Tool_ID = Tool.Tool_ID
 where Rental.Rental_ID = '&Rental_ID';
于 2012-11-29T06:04:18.047 回答