SQL Query,需要显示一个列条件的计数'忽略'其他条件

时间:2012-08-08 09:50:55

标签: mysql sql

我有以下查询:

<?php
$BM = $_POST['BranchManager'];
$AverageScore = ("ROUND(AVG(Score),1)AS 'Avg <br/> Score'");
$AverageAutofails = ("ROUND(AVG(Autofails),1)AS 'Autofails <br/> per Check'");
$CA003 = ( "SELECT COUNT(*) FROM Data_Table WHERE CA003Result = 'No'");

echo SQLResultTable("SELECT  BranchManager, COUNT(SalesExec)AS 'Total<br/> Checks', $AverageScore, SUM(Autofails) AS 'Total <br/>AutoFails', $AverageAutofails, $CA003 FROM Data_Table WHERE BranchManager = '$BM'");
?>

正如您所看到的,它会增加问题,分数,平均值等。

我需要做的是计算问题3(CA003)已被回答的次数。这与其他列无关。上面我试图做一个嵌套查询,但有些东西不起作用。如何将其插入到那里,而不是限制主查询的结果?

干杯!

所以我现在有这个工作:

$BM = $_POST['BranchManager'];
$AverageScore = ("ROUND(AVG(Score),1)AS 'Avg <br/> Score'");
$AverageAutofails = ("ROUND(AVG(Autofails),1)AS 'Autofails <br/> per Check'");
$CA003 = "(SELECT COUNT(*) FROM Data_Table WHERE CA003Result = 'No' AND BranchManager = '$BM')   AS 'CA003'";
$CA004 = "(SELECT COUNT(*) FROM Data_Table WHERE CA004Result = 'No' AND BranchManager = '$BM')   AS 'CA004'";
$CA006= "(SELECT COUNT(*) FROM Data_Table WHERE CA006Result = 'No' AND BranchManager = '$BM')   AS 'CA006'";
$CA010 = "(SELECT COUNT(*) FROM Data_Table WHERE CA010Result = 'No' AND BranchManager = '$BM')   AS 'CA010'";
$CA017 = "(SELECT COUNT(*) FROM Data_Table WHERE CA017Result = 'No' AND BranchManager = '$BM')   AS 'CA017'";
$CA022 = "(SELECT COUNT(*) FROM Data_Table WHERE CA022Result = 'No' AND BranchManager = '$BM')   AS 'CA022'";
$CA027 = "(SELECT COUNT(*) FROM Data_Table WHERE CA027Result = 'No' AND BranchManager = '$BM')   AS 'CA027'";



echo SQLResultTable("SELECT  BranchManager, COUNT(SalesExec)AS 'Total<br/> Checks', $AverageScore, SUM(Autofails) AS 'Total <br/>AutoFails', $AverageAutofails, $CA003, $CA004, $CA006, $CA010, $CA017, $CA022, $CA027 FROM Data_Table WHERE BranchManager = '$BM'");
?>

完美显示团队(分支)结果。

目前在下一张表中我有:

echo SQLResultTable("SELECT SalesExec, COUNT(SalesExec)AS 'Total<br/> Checks', $AverageScore, SUM(Autofails) AS 'Total <br/>AutoFails', $AverageAutofails,   FROM Data_Table WHERE BranchManager = '$BM' GROUP BY SalesExec");}

还需要为每个CA00问题显示无计数。

第一个查询适用于基于BranchManager字段的Branch Totals,但是对于第二个查询,我需要它来计算每行显示的SalesExec的每个CA00问题的No。

我真的不知道如何更好地解释它!

1 个答案:

答案 0 :(得分:2)

您只需要在子查询周围添加()即可。最终的查询看起来像......

SELECT
  BranchManager,
  COUNT(SalesExec)                                             AS 'Total Checks',
  ROUND(AVG(Score),1)                                          AS 'Avg Score',
  SUM(Autofails)                                               AS 'Total AutoFails',
  ROUND(AVG(Autofails),1)                                      AS 'Autofails per Check',
  (SELECT COUNT(*) FROM Data_Table WHERE CA003Result = 'No')   AS 'Total No'
FROM
  Data_Table
WHERE
  BranchManager = '$BM'
GROUP BY
  BranchManager


编辑:根据您的评论和修改...

第一组结果......

SELECT
  BranchManager,
  COUNT(SalesExec)                                     AS 'Total Checks',
  ROUND(AVG(Score),1)                                  AS 'Avg Score',
  SUM(Autofails)                                       AS 'Total AutoFails',
  ROUND(AVG(Autofails),1)                              AS 'Autofails per Check',
  SUM(CASE WHEN CA001Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA001 No',
  SUM(CASE WHEN CA002Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA002 No',
  SUM(CASE WHEN CA003Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA003 No',
  SUM(CASE WHEN CA004Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA004 No',
  SUM(CASE WHEN CA005Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA005 No',
  SUM(CASE WHEN CA006Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA006 No'
FROM
  Data_Table
WHERE
  BranchManager = '$BM'
GROUP BY
  BranchManager

第二个结果集......

SELECT
  SalesExec,
  COUNT(SalesExec)                                     AS 'Total Checks',
  ROUND(AVG(Score),1)                                  AS 'Avg Score',
  SUM(Autofails)                                       AS 'Total AutoFails',
  ROUND(AVG(Autofails),1)                              AS 'Autofails per Check',
  SUM(CASE WHEN CA001Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA001 No',
  SUM(CASE WHEN CA002Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA002 No',
  SUM(CASE WHEN CA003Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA003 No',
  SUM(CASE WHEN CA004Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA004 No',
  SUM(CASE WHEN CA005Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA005 No',
  SUM(CASE WHEN CA006Result = 'No' THEN 1 ELSE 0 END)  AS 'Total CA006 No'
FROM
  Data_Table
WHERE
  BranchManager = '$BM'
GROUP BY
  SalesExec