0

我有以下查询:

<?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) 被回答 NO 的次数。这与其他列无关,上面我尝试进行嵌套查询,但有些东西不起作用。如何将其插入其中,而不限制主查询的结果?

干杯!

所以我现在有这个工作:

$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 问题的 No Counts。

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

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

4

1 回答 1

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
于 2012-08-08T10:07:03.747 回答