【问题标题】:MS ACCESS SQL grouping and counting zerosMS ACCESS SQL 分组和计数零
【发布时间】:2023-03-28 01:20:01
【问题描述】:

在为以下内容创建 SQL 语句方面需要帮助:

基本上,我们有主管出去对员工进行测试。这些测试在线记录并导出到访问数据库。

测试按类别分组。因此,例如测试 # 的 101、102 和 103 被分组到类别 1 测试 # 的 112、113 和 114 被分组到类别 2,依此类推。

要求每个主管在每个类别中做一定的数量。

有一个表格包含有关每个测试编号及其所属类别的信息,另一个表格包含有关类别的详细信息。

我需要一份报告来显示每个类别的主管每个月完成了多少次测试,但如果特定月份没有输入任何测试,我需要显示 0 计数。

我能够使用 vba 获得计数,方法是循环遍历每个类别,然后循环遍历每个测试编号,然后计算完成的每个测试编号,但每个主管需要将近 30 秒(对于超过 500 位主管来说太长了)。

我希望我的解释是正确的,我希望你能有一个简单的解决方案。

需要创建的示例报告

Supervisor: Harvey Sample ( 0084568 )

01/20     Category 1:  5 Tests
01/20     Category 2:  0 Tests
01/20     Category 3:  8 Tests
01/20     Category 4:  2 Tests
01/20     Category 5:  4 Tests

02/20     Category 1:  2 Tests
02/20     Category 2:  6 Tests
02/20     Category 3:  3 Tests
02/20     Category 4:  0 Tests
02/20     Category 5:  5 Tests

已完成的测试存储在此表中

SPARTN_Livefeed1
Sheet ID         Date          Test Number          Supervisor ID
OB-1234          1/1/2020         101                 806855
OB-5678          1/1/2020         101                 806855
OB-9877          1/1/2020         112                 806855
OB-5644          1/1/2020         123                 806855
OB-5644          1/1/2020         123                 806855
REF_TestCatalog_tbl
ID          TestId          TestDesc          TestCategory          StartDate          EdnDate
1             101            Stopping              1                 12/1/2019
2             112            Delay                 2                 12/1/2019
3             123            Documents             3                 12/1/2019
4             134            Radio                 4                 12/1/2019




REF_TestCategory_tbl
ID        CategoryID          CategoryDesc          StartDate          EndDate   
1             1               Signals               12/1/2019
2             2               Speed                 12/1/2019
3             3               Equipment             12/1/2019
4             4               Operation             12/1/2019



Expected Results
CategoryID          TestofficerID          Actuals          ComplianceMonth
1                   806855                 2                 1/1/2020
2                   806855                 1                 1/1/2020
3                   806855                 3                 1/1/2020
4                   806855                 0                 1/1/2020

【问题讨论】:

  • 样本数据作为测试真的很有帮助。

标签: sql ms-access


【解决方案1】:
declare @supervisor varchar[50];
set @supervisor ='Harvey Sample'

select date, 'Category 1', (select count(*) from supervisor_tests sc where st.date = sc.date and sc.category = 'Category 1' and sc.supervisor = @supervisor) 
from supervisor_tests st where supervisor = @supervisor
union
select date, 'Category 2', (select count(*) from supervisor_tests sc where st.date = sc.date and sc.category = 'Category 2' and sc.supervisor = @supervisor) 
from supervisor_tests st where supervisor = @supervisor
order by date

为每个类别添加更多联合。如果您希望它是每月汇总,也可以做一个日期范围。

【讨论】:

  • 只需加入表(或者可能制作一个临时表)并更改字段名称,它应该仍然可以正常工作
猜你喜欢
  • 1970-01-01
  • 2020-04-06
  • 2018-10-30
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多