MySQL:获取计数和平均值

时间:2017-04-28 22:59:50

标签: mysql sql mysql-workbench

select 
COUNT(pd.property_id) AS `Beginning Total File Count`,
COUNT(pd.recv_dt) as `average days in inventory`,
AVG(pd.status = 'P') as `average days in pre-marketing`,
AVG(pd.status NOT IN('I','C')) as `average days onMarket`,
AVG(pd.status ='U') as `average days UnderContract`,
SUM(pd.status = 'O') as `Total FilesOccupied Status`,
SUM(pd.status = 'O') / COUNT(pd.property_id) as `percentage of Occupied / 
total file count`
from resnet.property_Details pd

我想要

  1. 开始总文件数
  2. 库存的平均天数
  3. 上市前的平均天数
  4. 平均待售天数
  5. 合同平均天数
  6. 处于已使用状态的文件总数
  7. 占用/总文件数的百分比
  8. 不确定我的查询是否写得正确,请帮助:)

    enter image description here

1 个答案:

答案 0 :(得分:1)

好的,这是一个非常疯狂的猜测,你是在这个请求之后:

select 
COUNT(distinct pd.property_id) AS `Beginning Total File Count`,
COUNT(pd.recv_dt) as `average days in inventory`,
AVG(IF(pd.status = 'P', 1,0)) as `average days in pre-marketing`,
AVG(IF(pd.status NOT IN('I','C'), 1,0)) as `average days onMarket`,
AVG(IF(pd.status ='U', 1,0)) as `average days UnderContract`,
SUM(IF(pd.status = 'O', 1,0)) as `Total FilesOccupied Status`,
SUM(IF(pd.status = 'O', 1,0)) / COUNT(pd.property_id) as `percentage of Occupied / 
total file count`
from resnet.property_Details pd

但是如果你没有使用AVG()语句,那么使用group by似乎很奇怪...(它只是一个条件总和,或者你需要按某种方式分组)