计算复杂查询的结果

时间:2011-05-18 23:16:35

标签: mysql

以下查询输出以下结果:

+------------+------------+
| testID     | testStatus|
+------------+------------+
|          1 | PASS       |
|          2 | PASS       |
|          3 | SKIP       |
|          4 | PASS       |
+------------+------------+

如何更改查询以向我提供通行证计数,跳过计数,失败计数和通过率,跳过%,失败%

    SELECT orsm.testID, orsm.testStatus
    FROM mdro orsm, mdr mr, cmt cm,
    mmrprt mrt, mmtt mtt, mmuq mui, mmgt mgt, mmft mft
    WHERE mr.cmtID = cm.ID 
    AND orsm.ParentID = mr.ID 
    AND cm.IdentID = mui.ID 
    AND cm.DEVID = 2 
    AND cm.TestoID = mrt.ID 
    AND cm.SterID = mtt.ID 
    AND cm.FDID = mft.ID 
    AND cm.GID = mgt.ID 
    AND mui.ID="2" 
    AND mrt.Name="cameron" 
    AND mtt.Name="synthesis" 
    AND mft.Name="dupocdna" 
    AND mgt.Name="cce"

3 个答案:

答案 0 :(得分:2)

我相信这样的事情应该有效,除非我误解了一些事情:

SELECT 
SUM(IF(orsm.testStatus="PASS",1,0)) as pass_count,
SUM(IF(orsm.testStatus="SKIP",1,0)) as skip_count,
SUM(IF(orsm.testStatus="SKIP",1,0)) / COUNT(*) as skip_pct,   
SUM(IF(orsm.testStatus="PASS",1,0)) / COUNT(*) as pass_pct,
SUM(IF(orsm.testStatus="FAIL",1,0)) / COUNT(*) as fail_pct

FROM mdro orsm, mdr mr, cmt cm,
mmrprt mrt, mmtt mtt, mmuq mui, mmgt mgt, mmft mft
WHERE mr.cmtID = cm.ID 
AND orsm.ParentID = mr.ID 
AND cm.IdentID = mui.ID 
... ALL THE REST OF YOUR ANDS ...

答案 1 :(得分:2)

首先,您可以使用GROUP BY子句获得所需内容:

SELECT orsm.testStatus, count(orsm.testID)
FROM ...
WHERE ...
GROUP BY orsm.testStatus

除此之外,你应该使用JOIN而不是一个巨大的WHERE子句,在不同表的列之间具有相等性。这既提高了查询性能又提高了易读性:

SELECT orsm.testID, orsm.testStatus
FROM cmt cm
    JOIN mdr mr ON mr.cmtID = cm.ID 
        JOIN mdro orsm ON orsm.ParentID = mr.ID 
    JOIN mmrprt mrt ON mrt.ID = cm.TestoID
    JOIN mmtt mtt ON mtt.ID = cm.SterID
    JOIN mmuq mui ON mui.ID = cm.IdentID
    JOIN mmgt mgt ON mgt.ID = cm.GID
    JOIN mmft mft ON mft.ID = cm.FDID
WHERE
    cm.DEVID = 2 
    AND mui.ID="2" 
    AND mrt.Name="cameron" 
    AND mtt.Name="synthesis" 
    AND mft.Name="dupocdna" 
    AND mgt.Name="cce"

答案 2 :(得分:1)

当你想要计算百分比时,这有点棘手:

SELECT orsm.testStatus, COUNT(*) AS count, COUNT(*) / matches.total * 100 AS percentage
FROM cmt cm
  JOIN mdr mr ON mr.cmtID = cm.ID 
  JOIN mdro orsm ON orsm.ParentID = mr.ID 
  JOIN mmrprt mrt ON mrt.ID = cm.TestoID
  JOIN mmtt mtt ON mtt.ID = cm.SterID
  JOIN mmuq mui ON mui.ID = cm.IdentID
  JOIN mmgt mgt ON mgt.ID = cm.GID,
  JOIN mmft mft ON mft.ID = cm.FDID,
  (
    SELECT COUNT(*) AS total
    FROM cmt cm
      JOIN mdr mr ON mr.cmtID = cm.ID 
      JOIN mdro orsm ON orsm.ParentID = mr.ID 
      JOIN mmrprt mrt ON mrt.ID = cm.TestoID
      JOIN mmtt mtt ON mtt.ID = cm.SterID
      JOIN mmuq mui ON mui.ID = cm.IdentID
      JOIN mmgt mgt ON mgt.ID = cm.GID,
      JOIN mmft mft ON mft.ID = cm.FDID,
    WHERE
      cm.DEVID = 2 
      AND mui.ID="2" 
      AND mrt.Name="cameron" 
      AND mtt.Name="synthesis" 
      AND mft.Name="dupocdna" 
      AND mgt.Name="cce"
  ) matches
WHERE
  cm.DEVID = 2 
  AND mui.ID="2" 
  AND mrt.Name="cameron" 
  AND mtt.Name="synthesis" 
  AND mft.Name="dupocdna" 
  AND mgt.Name="cce"