Access Query创建1而不是3

时间:2017-06-09 18:28:51

标签: sql ms-access merge

我从一个表中得到以下3个查询。不同的是每个查询中的标准([ReceiptYesNo])。

Query No1 - NO Criteria:
TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
SELECT Format([ExpensesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
FROM tblExpenses
GROUP BY Format([ExpensesDate],"yyyy")
ORDER BY Format([ExpensesDate],"yyyy") DESC 
PIVOT Month([ExpensesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

Query No2 - Criteria [ReceiptYesNo] = YES:
TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
SELECT Format([ExpensesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
FROM tblExpenses
WHERE (((tblExpenses.ReceiptYesNo)="YES"))   <=== See Here ===
GROUP BY Format([ExpensesDate],"yyyy")
ORDER BY Format([ExpensesDate],"yyyy") DESC 
PIVOT Month([ExpensesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

Query No3 - Criteria [ReceiptYesNo] = NO:
TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
SELECT Format([ExpensesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
FROM tblExpenses
WHERE (((tblExpenses.ReceiptYesNo)="NO"))   <=== See Here ===
GROUP BY Format([ExpensesDate],"yyyy")
ORDER BY Format([ExpensesDate],"yyyy") DESC 
PIVOT Month([ExpensesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

是否可以创建一个包含3?

的查询

谢谢。

1 个答案:

答案 0 :(得分:1)

在每个之间使用UNION - 只要列相同

https://www.w3schools.com/sql/sql_union.asp