我有一个MS Access查询(使用SQL创建),用于计算设备状态表中日期列中非NULL值的出现次数。该表包含特定制造工厂的所有设备。查询还计算总设备,我们没有NULL值。共有12种不同的植物,每种植物都有自己相同的Access表。我需要创建一个统一的SQL查询,将每个计数的总和创建为所有工厂的主计数。
状态表的结构名为“_review_status”:
equip_number, text
review_a_analysis, date
review_b_analysis, date
review_c_analysis, date
review_d_analysis, date
review_e_analysis, date
review_f_analysis, date
review_g_analysis, date
工作查询的结果(在一个表上)看起来像这样。
a b c d e f g equip_count
17 31 0 94 13 12 44 1249
新结果应该与上面的结果完全相同,只是数字会更大,因为查询会查看所有12组表。
以下是工作单表查询:
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment
LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number =
dept1_review_status.dept1_equip_number;
dept1_equipment表上的连接用于获取每个部门中所有设备的完整计数。
非常感谢。
鲍勃
答案 0 :(得分:1)
考虑通过聚合查询运行所有12个组的联合查询,然后使用保存的联合查询运行另一个最终聚合查询:
<强> UNION 强>
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number = dept1_review_status.dept1_equip_number;
UNION ALL
SELECT
Count(dept2_review_status.review_a_analysis) AS a,
Count(dept2_review_status.review_b_analysis) AS b,
Count(dept2_review_status.review_c_analysis) AS c,
Count(dept2_review_status.review_d_analysis) AS d,
Count(dept2_review_status.review_e_analysis) AS e,
Count(dept2_review_status.review_f_analysis) AS f,
Count(dept2_review_status.review_g_analysis) AS g,
Count(dept2_equipment.dept2_equip_number) AS equip_count
FROM dept2_equipment LEFT JOIN dept2_review_status
ON dept2_equipment.dept2_equip_number = dept2_review_status.dept2_equip_number;
UNION ALL
SELECT
Count(dept3_review_status.review_a_analysis) AS a,
Count(dept3_review_status.review_b_analysis) AS b,
Count(dept3_review_status.review_c_analysis) AS c,
Count(dept3_review_status.review_d_analysis) AS d,
Count(dept3_review_status.review_e_analysis) AS e,
Count(dept3_review_status.review_f_analysis) AS f,
Count(dept3_review_status.review_g_analysis) AS g,
Count(dept3_equipment.dept3_equip_number) AS equip_count
FROM dept3_equipment LEFT JOIN dept3_review_status
ON dept3_equipment.dept3_equip_number = dept3_review_status.dept3_equip_number;
...other 9 tables...
<强> FINAL 强>
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM unionqry
或者,如果MS Access允许,则可以将所有内容合并到一个查询中:
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM (...above union query)... As unionqry