2013-01-10 133 views
1

我正在放在一起交叉表,我想要一個按月分組的多個列值(所有數字)的報告。這是我使用的SQL。我知道這不會帶來預期的結果。每個「timex」列中都有一個不同的數字。我想要一個查詢,它將返回按月分組的列的總和。MS Access交叉表查詢 - 按月報告的列總數

TRANSFORM Sum(tblTimeTracking.time1+ tblTimeTracking.time2+ tblTimeTracking.time3+ tblTimeTracking.time4+ tblTimeTracking.time5+ tblTimeTracking.time6+ tblTimeTracking.time7+ tblTimeTracking.time8+ tblTimeTracking.time9+ tblTimeTracking.time10+ tblTimeTracking.time11+ tblTimeTracking.time12+ tblTimeTracking.time13+ tblTimeTracking.time14+ tblTimeTracking.time15+ tblTimeTracking.time16+ tblTimeTracking.time17+ tblTimeTracking.time18+ tblTimeTracking.time19+ tblTimeTracking.time20+ tblTimeTracking.time21+ tblTimeTracking.time22) AS Total SELECT tbl_vlookup.Manager AS Manager FROM tbl_vlookup INNER JOIN tblTimeTracking ON tbl_vlookup.[Associate Name] = tblTimeTracking.Associate GROUP BY tbl_vlookup.Manager PIVOT Format([Day],"yyyy-mm");

Associate Day   Time 1 Time 2 Time 3 Time 4 Time 5 Time 6 Time 7 
John Smith 12/1/9999  1   0  0  5.5  1  0.25 0.25 
+0

你是說你有一個22個時間表的列表?如果是這樣,爲什你需要用UNION查詢來壓扁它。 – Fionnuala

+0

我這樣做。他們都是獨一無二的。這是一個勞動力跟蹤器。時間1將是休息時間,時間2 =文件等...我想找到按月排序的所有列的總數 – Chris568524

+0

請發佈樣本數據。 – Fionnuala

回答

1

事情是這樣的:

TRANSFORM Sum(q.Time1) AS SumOfTime1 
SELECT q.Associate, q.Day 
FROM (SELECT t.Associate, t.Day, t.Time1,"Time1" As TimeType 
FROM tbl t 
UNION ALL 
SELECT t.Associate, t.Day, t.Time2,"Time2" As TimeType 
FROM tbl t 
UNION ALL 
SELECT t.Associate, t.Day, t.Time3,"Time3" As TimeType 
FROM tbl t) AS q 
GROUP BY q.Associate, q.Day 
PIVOT q.TimeType; 

正如我所說,你需要拼合表。它似乎只是不同類型的數據:)