-1

以下のクエリが 2 つの集計関数 (合計) の差を適切に取得しないのはなぜですか?

SELECT 
    epa.status as Status,
    sum(equip_port_usage.total_nbr_ports) as Total,
    sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Used,
    sum(equip_port_usage.NBR_PORTS_ASSIGNED)
      - sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Difference
  FROM site_inst
  INNER JOIN site_attr_settings
     ON site_attr_settings.site_inst_id = site_inst.site_inst_id
  INNER JOIN epa on epa.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_inst ON equip_inst.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_port_usage
     ON equip_port_usage.equip_inst_id=equip_inst.equip_inst_id
WHERE site_inst.SITE_HUM_ID = 'CLEUS'
GROUP BY epa.status;

出力は以下のようになります

Ok        303876  10276     0
Faulty     19044    644     0
Reserved   19872    672     0

私はそれを期待しています

Ok        303876  10276  293600
Faulty     19044    644   18400
Reserved   19872    672   19200
4

2 に答える 2

2

コードでのコピーと貼り付けの失敗です。あなたは計算しています

sum(equip_port_usage.NBR_PORTS_ASSIGNED)
 - sum(equip_port_usage.NBR_PORTS_ASSIGNED)

これは常に 0 です。

試す:

SELECT 
    epa.status as Status,
    sum(equip_port_usage.total_nbr_ports) as Total,
    sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Used,
    sum(equip_port_usage.total_nbr_ports)
     - sum(equip_port_usage.NBR_PORTS_ASSIGNED) as Difference
  FROM site_inst
  INNER JOIN site_attr_settings
     ON site_attr_settings.site_inst_id = site_inst.site_inst_id
  INNER JOIN epa ON epa.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_inst ON equip_inst.site_inst_id=site_inst.site_inst_id
  INNER JOIN equip_port_usage
     ON equip_port_usage.equip_inst_id=equip_inst.equip_inst_id
 WHERE site_inst.SITE_HUM_ID = 'CLEUS'
 GROUP BY epa.status
于 2012-07-06T08:53:49.780 に答える
0

同じ列を 2 回合計し、2 つの結果を減算しているので、もちろん常に になります0

于 2012-07-06T08:53:18.093 に答える