我想创建一个返回表结果的查询,如下所示:
列标题是我的某个表格中的字段,下面的数字是该字段的是/否条目计数。
我遇到的问题是在行的开头显示是/否。我希望它能够以更好的方式组织数据创建图表。
现在我只有查询独立显示2行(一个查询没有结果,一个查询是结果)
这是原始表格:
答案 0 :(得分:7)
改变一点Tim's solution,这是我肯定的工作回应。我创建了一个名为Table1
的测试表,其中包含与屏幕截图中的OP完全相同的字段。
SELECT 'yes' As Outcome,
SUM(IIF([Identified?] = -1, 1, 0)) As Identified,
SUM(IIF([DetailPlanned?] = -1, 1,0)) AS planned,
SUM(IIF([Realized?] = -1, 1,0)) AS realized
From Table1
UNION ALL
SELECT 'no' As Outcome,
SUM(IIF([Identified?] = 0, 1, 0)) As Identified,
SUM(IIF([DetailPlanned?] = 0, 1,0)) AS planned,
SUM(IIF([Realized?] = 0, 1,0)) AS realized
From Table1
答案 1 :(得分:1)
这是一个通用的SQL查询,希望可以适应Access:
SELECT
'yes' AS outcome,
SUM(IIF(identified = 'yes', 1, 0)) AS identified,
SUM(IIF(planned = 'yes', 1, 0)) AS planned,
SUM(IIF(realized = 'yes', 1, 0)) AS realized
FROM yourTable
UNION ALL
SELECT
'no',
SUM(IIF(identified = 'no', 1, 0)),
SUM(IIF(planned = 'no', 1, 0)),
SUM(IIF(realized = 'no', 1, 0))
FROM yourTable;
我不知道实际存储在复选框后面的列中的值。你可能不得不用其他东西替换是/否。
这段代码肯定有效,只需更新表名,它就可以使用你拥有的字段:
SELECT
'yes' As Outcome,
SUM(IIF([identified] = -1, 1, 0)) As Identified,
SUM(IIF([planned] = -1, 1,0)) AS planned,
SUM(IIF([realized] = -1, 1,0)) AS realized
From yourTable
UNION ALL
SELECT
'no' As Outcome,
SUM(IIF([identified] = 0, 1, 0)) As Identified,
SUM(IIF([planned] = 0, 1,0)) AS planned,
SUM(IIF([realized] = 0, 1,0)) AS realized
From yourTable;