根据列值的SUM删除UNION结果集中的列

时间:2011-10-06 14:06:20

标签: mysql sql

我写了一个查询,我在这个问题的底部解释。它正确地获得了人们提出的一些问题的总分。所以,结果集看起来像这样:

            | David |  Bill  | Mary | James
Question 1  |   10  |   10   |  0   |   0
Question 2  |   10  |   20   |  0   |   0
Question 3  |   10  |   30   |  0   |   0
Question 4  |   0   |   20   |  0   |   1

我需要做的是,如何从结果集中删除任何人,他们对所有问题都没有分数。所以,玛丽'将从上面的结果中删除,留下:

            | David |  Bill  | James
Question 1  |   10  |   10   |   0
Question 2  |   10  |   20   |   0
Question 3  |   10  |   30   |   0
Question 4  |   0   |   20   |   1

以下是需要进一步开发的查询:

SELECT
    `questions`,
    SUM(`0`) AS `David`,
    SUM(`1`) AS `Bill`
FROM(
(SELECT
    ROUND(((SUM(`sm`.`ScorecardMark`) * `sc`.`ScoreCriteriaWeight`)/(COUNT(`sm`.`ScorecardMark`) * `sc`.`ScoreCriteriaWeight`))*100) AS `0`,
    0 AS `1`
FROM
    `tables`
WHERE
    `clauses`
GROUP BY
    `questions`)
UNION
(SELECT
    0 AS `0`,
    ROUND(((SUM(`sm`.`ScorecardMark`) * `sc`.`ScoreCriteriaWeight`)/(COUNT(`sm`.`ScorecardMark`) * `sc`.`ScoreCriteriaWeight`))*100) AS `1`
FROM
    `tables`
WHERE
    `clauses`
GROUP BY
    `questions`)
)  AS `tbltotals`
GROUP BY
    `questions`

1 个答案:

答案 0 :(得分:1)

我认为更好的方法是选择问题作为专栏,因为获得所需问题的列表应该比获得好于0的人员列表更容易。

然后,您可以对人员应用正常的行级别过滤。您可以将结果转置为在应用程序代码中切换行和列,以备不时之需。

相关问题