简体   繁体   English

Oracle DB 查询以获取记录计数并随后计算平均值

[英]Oracle DB query to get records count and calculate average afterward

Could anyone please help me regarding that Oracle query.任何人都可以帮助我了解该 Oracle 查询。 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感谢高级。从下面的查询返回一些记录我想计算平均值意味着 SUM(COST+DURATION)/No.of 行返回形式该查询但在查询下面基于 SITE_ID 做分区我不想要任何分区,想要的是什么查询返回(所有持续时间的总和+所有成本的总和/行返回数)但没有任何分区是我可以做到的任何方式

select * from ( SELECT ROW_NUMBER() OVER (ORDER BY 1) RN, select * from (SELECT ROW_NUMBER() OVER (ORDER BY 1) RN,
SITE_ID, SITE_ID,
CASEID, WOID, DURATION, CASEID,WOID,持续时间,
COST,成本,
(SUM(COST+DURATION) OVER(PARTITION BY SITE_ID))/COUNT as TotalCost from (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 WHERE (1=1) ) 其中 rn 介于 0 和 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 .根据您修改后的问题,我认为您想要的是每个站点的 COST 和 DURATION 的总和,加上从table1table2的条目数得出的这些数字的平均值。 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.这里有一个 SQL Fiddle 演示来证明这个查询运行成功。


Your question appears to reference other tables, site and emp , which I have ignored because you don't provide join conditions for them.您的问题似乎引用了其他表、 siteemp ,我忽略了它们,因为您没有为它们提供连接条件。 Likewise I have ignored the top-n filter;同样,我忽略了 top-n 过滤器; you can add it back it if you want.如果你愿意,你可以把它加回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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