Вы можете выбрать данные несколько раз, используя запрос UNION
, а затем повернуть его так:
TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
SELECT Format([FinancesDate],"yyyy") AS [Year], Receipt, IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
FROM (
SELECT [FinancesDate], Nz(SumPrice, 0) AS SumPrice, 'Both' As Receipt
FROM tblFinances
UNION ALL
SELECT [FinancesDate], Nz(SumPrice, 0) AS SumPrice, ReceiptYesNo As Receipt
FROM tblFinances
) As t
GROUP BY Format([FinancesDate],"yyyy"), Receipt
ORDER BY Format([FinancesDate],"yyyy")
PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);
Альтернативным, возможно, немного более быстрым, но, несомненно, более странным способом, будет использование CROSS JOIN
вместо центра для дублирования данных:
TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
SELECT Format([FinancesDate],"yyyy") AS [Year], IIF(Bool, ReceiptYesNo, Null) As Receipt, IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
tblFinances, (SELECT DISTINCT Abs(ID Mod 2) * -1 As Bool FROM MSysObjects)
GROUP BY Format([FinancesDate],"yyyy"), IIF(Bool, ReceiptYesNo, Null)
ORDER BY Format([FinancesDate],"yyyy")
PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);
Обновлено:
Окончательный код
TRANSFORM IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPrice
SELECT Format([FinancesDate],"yyyy") AS [Year], Receipt, IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
FROM (
SELECT [FinancesDate], Nz(Price, 0) AS SumPrice, 'Both' As Receipt
FROM tblFinances
UNION ALL
SELECT [FinancesDate], Nz(Price, 0) AS SumPrice, ReceiptYesNo As Receipt
FROM tblFinances
) As t
GROUP BY Format([FinancesDate],"yyyy"), Receipt
ORDER BY Format([FinancesDate],"yyyy")
PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);