您好,感謝您的觀看!
我正在嘗試從先前的查詢中進行復雜的選擇,但我很難找到解決方案,部分原因是我很難描述我所追求的內容或要搜索的內容。這是摩擦:
我有什么([table1]):
| 識別符號 | 月 | 第 1 項 | 第 2 項 |
|---|---|---|---|
| xyz-1 | 10 | 0 | 0 |
| xyz-2 | 10 | 0 | 0 |
| xyz-1 | 11 | 1 | 1 |
| xyz-2 | 11 | 1 | 1 |
如果可能的話,我想要什么:
| 識別符號 | 專案 1 - 10 | 專案 2 - 10 | 專案 1 - 11 | 專案 2 - 11 |
|---|---|---|---|---|
| xyz-1 | 0 | 0 | 1 | 1 |
| xyz-2 | 0 | 0 | 1 | 1 |
目標是我在一年中的每個月都有一組專案(上例僅顯示 10 月和 11 月)。我覺得 Group By 和 Join 解決方案是我所需要的,但是在花了一整天的時間之后我被困住了。
任何幫助表示贊賞!
更新 - 解決方案:
結合以下兩位貢獻者的建議,我能夠重寫生成上面起始表的原始查詢。
我一直在運行這個查詢:
TRANSFORM First([Points]) AS ItemPoints
SELECT identifier, month
FROM [source]
GROUP identifier, month
PIVOT name;
但這為月份創建了一個列,事后看來這是非常明顯的。
解決方案是以下查詢:
TRANSFORM First([source].Points) AS ItemPoints
SELECT [source].identifier
FROM [itemNames], [source]
GROUP BY [source].identifier
ORDER BY ScoreMonth & [itemNames].ItemId
PIVOT ScoreMonth & [itemNames].ItemId;
其中 [itemNames] 是回傳唯一專案名稱串列的查詢,即“專案 1”、“專案 2”位。
這導致了下表:
| 識別符號 | 10項1 | 10項2 | 11項1 | 11項2 |
|---|---|---|---|---|
| xyz-1 | 0 | 0 | 1 | 1 |
| xyz-2 | 0 | 0 | 1 | 1 |
我可以與之合作:)
uj5u.com熱心網友回復:
您嘗試執行的操作稱為交叉表查詢。但是,您的查詢有 2 列標題 Month 和 Item。月是看不見的。Access 和 Excel 只允許在交叉表查詢中使用 1 個列標題。所以我的解決方案是手動生成 2 列交叉表。因此,對于手動交叉表,我們需要進行交叉連接查詢以生成我們需要的所有行和列。然后我們使用計算欄位和 dlookup 函式將 Table1 中的正確值插入到即將到來的交叉表查詢的根查詢中的正確單元格中。
ItemNames 是所有專案名稱的表格,Months(這里是 2 個月),Identifiers 是所有識別符號。由于我們不能有 2 個同名的列,因此我們將使用 ItemMonth 作為解決方法。
Value: Nz(DLookUp("item1","Table1","identifier = '" & [identifier] & "' AND monthnumber = " & [MonthNumber]),0)
ItemMonth: [MonthNumber] & [ItemName]
'Table 1
ID identifier monthnumber item1 item2
3 xyz-1 10 0 0
4 xyz-2 10 0 0
5 xyz-1 11 1 1
6 xyz-2 11 1 1
'after cross join : query1 in picture
MonthNumber ItemName identifier Value ItemMonth
10 Item 1 xyz-1 0 10Item 1
11 Item 1 xyz-1 1 11Item 1
10 Item 2 xyz-1 0 10Item 2
11 Item 2 xyz-1 1 11Item 2
10 Item 1 xyz-2 0 10Item 1
11 Item 1 xyz-2 1 11Item 1
10 Item 2 xyz-2 0 10Item 2
11 Item 2 xyz-2 1 11Item 2
交叉表設定非常簡單。交叉表是一種總計查詢,因此請注意匯總函式:

'result of our cross-tab query but the column names are still wrong so we will fix that with a report
identifier 10Item 1 10Item 2 11Item 1 11Item 2
xyz-1 0 0 1 1
xyz-2 0 0 1 1
為了生成報告,我剛剛選擇了交叉表查詢并點擊了報告。然后進入設計模式并編輯所有列標簽。如果您必須經常執行整個程序或有很多專案,請使用 VBA 自動調整報告標簽。

旁白 1. 在 Access 中,如果交叉表查詢基于稍微復雜的查詢,則它們會出錯。如果您的交叉表基于查詢并且它不起作用,請嘗試將交叉表的查詢轉換為帶有生成表查詢的表。然后將交叉表建立在新表的基礎上。
Aside 2. I suggest Looking at the cross-tab as only a way to view your data. Every time you add or subtract an item to this data that results in adding or subtracting 12 columns to the cross-tab. that means you have to adjust any reports and forms based on a cross-tab every time the items change. Way to much work.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342500.html
下一篇:使用ByRef更新記錄集欄位
