简体   繁体   中英

Oracle DB query to get records count and calculate average afterward

Could anyone please help me regarding that Oracle query. Here is my required output. 在此处输入图片说明 Thanks in advanced.from below query return some record i want to Calculate Average means SUM(COST+DURATION)/No.of row return form that query but below query doing partition based on SITE_ID i dont want any partition,What is want is whatever query return (Sum of all Duration+sum of all Cost/No. of row returns)but without any partition is that any way i can do that

select * from ( SELECT ROW_NUMBER() OVER (ORDER BY 1) RN,
SITE_ID,
CASEID, WOID, DURATION,
COST,
(SUM(COST+DURATION) OVER(PARTITION BY SITE_ID))/COUNT as TotalCost from

     (
      SELECT  COUNT(*) OVER () as COUNT,
      SITE_ID,  
      CASEID,
      WOID,
      ROUND(table1.duration/3600,2) AS DURATION,
      ROUND(table2.duration/3600),2) AS COST
       FROM table1  ) AVG

WHERE (1=1) ) where rn between 0 and 1000

On the basis of your revised question what I think you want is the total of COST and DURATION for each SITE, plus an average of these figures derived from the number of entries in table1 and table2 . This is what this query does:

select site_id
       , duration
       , cost
       , (duration+cost)/no_of as avg_total_cost
from (
    select
      table1.site_id
      , count(*) as no_of
      , sum(round(table1.duration/3600,2)) as duration
      , sum(round((table1.labor_rate * table2.duration)/3600,2)) as cost
    from table1
    inner join table2
        on table1.id = table2.id
    group by table1.site_id 
)
/

Here is a SQL Fiddle demo to prove that this query runs successfully.


Your question appears to reference other tables, site and emp , which I have ignored because you don't provide join conditions for them. Likewise I have ignored the top-n filter; you can add it back it if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM