我會發布我的查詢開始......SQL - UNION查詢中總
SELECT [copyright status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
GROUP BY [copyright status]
UNION
SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE [lw status] = 'In Reserve'
GROUP BY [lw status]
UNION
SELECT [lw status],
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
WHERE [lw status] = 'Published'
GROUP BY [lw status];
(希望這是易於閱讀)
它的工作,我打算將它,不過,我想喜歡爲查詢添加更多功能。
在第一個SELECT
查詢之後,我想添加一個額外的查詢,其中總計每個三個總和(關鍵信息,方法,研究)。我試圖加入的語法如下:
<Previous Query>
UNION
SELECT,
sum(IIF(layer='key info',1,0)) AS [Key Info],
sum(IIF(layer='approaches',1,0)) AS [Approaches],
sum(IIF(layer='research',1,0)) AS [Research]
FROM resources
UNION
<Next Query>
然而,當我嘗試運行它,我得到它讀取「不列的聯合查詢的查詢所選的兩個表中的數字錯誤比賽。」
我不確定我是否對此過於抱負。
此外,是否有更有效的方式來格式化初始查詢?
如果有區別,圖層,版權狀態和狀態值將存儲在單獨的表中,並通過表格設計模式中的組合框繪製到資源表中。我正在使用Access 2003.
如果需要更多信息,請告訴我。
謝謝。
這樣一個簡單的解決方案,謝謝! – Squadinho
@Squadinho:列名由「union」的第一部分設置,連續的部分不能更改它。你可以用'SELECT [版權狀態]作爲Total開始你的查詢,' – Andomar