2017-08-03 20 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