2017-08-03 65 views
2

我一直在寻找一个解决方案,联合两个表,并计算其条目,这就是为什么我发现这段代码: Access union & count的MS Access联盟数据过滤器和标准

但是,由于它的工作原理,我还需要过滤它并添加一个标准。

我有什么:

表库存

Action_A Date_Action_A Action_B Date_Action_B Type 
Item1 Date   Item2 Date   A 
Item2 Date   Item5 Date   A 
Item3 Date   Item3 Date   B 
and so on, all combinations 

表型

Type Team 
A Team1 
B Team2  

我能代码(如果项目1具有从17年3月8日和条目03- 08-17我有两排):

Items Count_Action_A Count_Action_B Date_A Date_B 
Item1 1    1    Week30 Week31 
Item1 1    0    Week31 Week31 
and so on 

我需要下表和标准,只有在同一个星期添加的条目显示(关于同一个星期内一项项合并)

Items Count_Action_A Count_Action_B Date_A Date_B Team 
Item1 2    1    Week31 Week31 Team1 
Item2 0    2    Week31 Week31 Team2 

我需要这个,因为后来我想打一个图表和过滤器由一个团队,如果可能的话,还通过一个星期(从week30显示的条目,或从week30和31)过滤

我的代码(缺少团队柱,butit显示我周):

SELECT Items, Date_A, Date_B, COUNT(A_Type) AS Count_Action_A, COUNT(B_Type) AS Count_Action_B, DatePart("ww",[Date_A]) AS Week_A, DatePart("ww",[Date_B]) AS Week_B 
FROM (SELECT Action_A as Items, Date_Action_A as Date_A, Date_Action_B as Date_B, 1 AS A_Type, NULL AS B_Type FROM Inventory 
     UNION ALL 
     SELECT Action_B, Date_Action_B, Date_Action_A, NULL, 1 FROM Inventory) 
WHERE Items IS NOT NULL 
GROUP BY Items, Date_A, Date_B 
ORDER BY Items; 

What is in the table and what is displayed on the graph, it does not work as intended.

编辑

如此看来,我需要总是在一个给定的线的同一个星期都在Date_A和Date_B。

现在,我有这样的情况:

Items Count_Action_A Count_Action_B Week_A Week_B Team 
Item1 1    1    Week31 Week31 Team1 
Item1 1    1    Week31 Week32 Team1 

我需要Action_A和Action_B合并到同一个星期,像这样:

Items Count_Action_A Count_Action_B Week_A Week_B Team 
Item1 2    1    Week31 Week31 Team1 
Item1 0    1    Week32 Week32 Team1 

这可能吗?

EDIT2 分组过程:

Items Count_Action_A Count_Action_B Week_A Week_B 
Item1 1    1    Week31 Week31 - OK, stays 
Item1 1    1    Week30 Week31 - is transferred to: item1 1 0 Week30 Week30 and item1 0 1 Week31 Week31     

编辑3:

我已经改变了表类型Type_Tbl,现在有两列:Type_Typ和团队。在表格Inventory中,我将列类型更改为Type_Inv。所以据我所知,目前我没有使用任何有限的SQL语言。问题出在你的第二个查询中,我得到了消息“Reference to field”Team“可以引用FROM函数中列出的多个表”。

已修改的第一个查询(第二个查询中没有更改)。

SELECT Items, TheAction, Date_Action, Week, Action_Type, AOrB, Team 
FROM (SELECT Items, Action_A As TheAction, Date_Action_A As Date_Action, DatePart("ww",[Date_Action_A]) As Week, Type_Inv As Action_Type, "A" As AOrB From Inventory 
UNION ALL 
SELECT Items, Action_B As TheAction, Date_Action_B As Date_Action, DatePart("ww",[Date_Action_B]) As Week, Type_Inv As Action_Type, "B" As AOrB 
From Inventory) AS A INNER JOIN Type_Tbl ON Type_Tbl.[Type_Type]=A.Action_Type; 

回答

1

编辑答案: 我们将采取两步法。

第1步:规范化数据,加入团队,并避免使用SQL关键字作为列名。此查询保存为NormalizedData

SELECT Items, TheAction, Date_Action, Week, Action_Type, AOrB, Team 
FROM 
(SELECT Action_A AS Items, Action_A As TheAction, Date_Action_A As Date_Action, DatePart("ww",[Date_Action_A]) As Week, [Type] As Action_Type, "A" As AOrB 
From Inventory 
UNION ALL 
SELECT Action_B AS Items, Action_B As TheAction, Date_Action_B As Date_Action, DatePart("ww",[Date_Action_B]) As Week, [Type] As Action_Type, "B" As AOrB 
From Inventory) As A INNER JOIN [Type] ON ([Type].[Type] = A.Action_Type) 

第2步:正确非规范化和统计数据

SELECT A.Items, Count_Action_A, Count_Action_B, A.Week As Week_A, A.Week As Week_B, A.Team 
FROM 
(SELECT Items, Count(TheAction) As Count_Action_A, Week, Team FROM NormalizedData WHERE AOrB = "A" GROUP By Items, Team, Week) As A 
LEFT JOIN (SELECT Items, Count(TheAction) As Count_Action_B, Week, Team FROM NormalizedData WHERE AOrB = "B" GROUP By Items, Team, Week) AS B ON A.Items = B.Items AND A.Week = B.Week 
UNION 
SELECT B.Items, 0 As Count_Action_A, Count_Action_B, B.Week As Week_A, B.Week As Week_B, B.Team 
FROM 
(SELECT Items, Count(TheAction) As Count_Action_B, Week, Team FROM NormalizedData WHERE AOrB = "B" GROUP By Items, Team, Week) AS B 
LEFT JOIN (SELECT Items, Count(TheAction) As Count_Action_A, Week, Team FROM NormalizedData WHERE AOrB = "A" GROUP By Items, Team, Week) As A ON A.Items = B.Items AND A.Week = B.Week 
WHERE A.Items Is Null