MS Access Union数据过滤器和条件

时间:2017-08-03 07:18:45

标签: sql ms-access

我一直在寻找一个联合两个表并计算其条目的解决方案,这就是我找到这段代码的原因: Access union & count

但是,我还需要对其进行过滤并添加标准。

我有什么:

表库存

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      

我能够编码(如果Item1有03-08-17和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

我需要这个,因为稍后我想制作一个图表并由团队过滤,如果可能的话,还要过滤一周。(显示第30周的作品,或者从第30周和第31周开始)

我的代码(没有Team专栏,但是几周显示我):

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和Team。在表Inventory中,我将列Type更改为Type_Inv。据我所知,目前我还没有使用任何受限制的SQL词。问题在于您的第二个查询,我收到了消息"参考字段" 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 个答案:

答案 0 :(得分: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