sql—将查询分组为3列,并在apex oracle中显示另一列的总和

uxh89sit  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(408)

我想做个报告。根据我的要求,这并不难。我在此请求中指定日期和记录类型(正数或减数)。我希望此查询按contractorid、itemid和organization字段分组。从网上取款。

select
  ID, DOCNUMBER,DOCDATE,PRINTNUMBER,DRIVERID,CONTRACTORID,ITEMID,
  BRUTTO,TARE,NETTO,
  DS_ID,
  DAT_A,
  MILEAGE,
  ORGANIZATION,
  sum(NETTO)
from WAYBILL
where trunc(DOCDATE) between to_date(:P115_D1,'dd.mm.yyyy')
                         and to_date(:P115_D2,'dd.mm.yyyy')
and DIRECTIONID = :P115_DIRECTIONID 
group by 
  ID, DOCNUMBER,DOCDATE,PRINTNUMBER,DRIVERID,CONTRACTORID,ITEMID,
  BRUTTO,TARE,NETTO,
  DS_ID,
  DAT_A,
  MILEAGE,
  ORGANIZATION
lvmkulzt

lvmkulzt1#

似乎您希望显示完整的行,每个行都具有每个contractorid、itemid和organization的netto sum。您可以通过窗口功能实现这一点:

select
  id,  docnumber, docdate, printnumber, driverid, contractorid, itemid, 
  brutto, tare, netto, ds_id, dat_a, mileage, organization,
  sum(netto) over (partition by contractorid, itemid, organization) as netto_sum
from waybill
where trunc(docdate) between to_date(:p115_d1,'dd.mm.yyyy')
                         and to_date(:p115_d2,'dd.mm.yyyy')
and directionid = :p115_directionid 
order by id;

相关问题