MS Access объединяет 3 запроса в 1 - PullRequest
0 голосов
/ 12 мая 2018

У меня есть следующая таблица:

 tblFinances
 -------------------------------
   -FinancesID      => AutoNumber
   -FinancesDate    => Date
   -Price           => Currency
   -ReceiptYesNo    => Yes/No

и следующие 3 запроса:

qtrFinancesAll
    TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
    SELECT Format([FinancesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
    FROM tblFinances
    GROUP BY Format([FinancesDate],"yyyy")
    ORDER BY Format([FinancesDate],"yyyy") DESC 
    PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

qtrFinancesReceiptNo
    TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
    SELECT Format([FinancesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
    FROM tblFinances
    WHERE (((tblFinances.ReceiptYesNo)="No"))
    GROUP BY Format([FinancesDate],"yyyy")
    ORDER BY Format([FinancesDate],"yyyy") DESC 
    PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

qtrFinancesReceiptYes
    TRANSFORM IIf(Sum([price]) Is Null,0,Sum([price])) AS SumPrice
    SELECT Format([FinancesDate],"yyyy") AS [Year], IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalPricePerYear
    FROM tblFinances
    WHERE (((tblFinances.ReceiptYesNo)="Yes"))
    GROUP BY Format([FinancesDate],"yyyy")
    ORDER BY Format([FinancesDate],"yyyy") DESC 
    PIVOT Month([FinancesDate]) In (1,2,3,4,5,6,7,8,9,10,11,12);

Могу ли я иметь один запрос вместо 3?

  • qtrFinancesAll => Показать все цены за месяц.
  • qtrFinancesReceiptNo => Показать цены за месяц, где квитанция №.
  • qtrFinancesReceiptYes => Показать цены за месяц, где квитанция да.

Заранее спасибо.

ОБНОВЛЕНО:

Желаемый вывод должен выглядеть следующим образом:

Year  Receipt  TotalPrice   1    2    3    4    5    6    7    8    9    10    11    12
------------------------------------------------------------------------------------------
2018              1200     100   0   500  300   0   100   0    0    0   100   100     0
2018    Yes        800      0    0   400  100   0   100   0    0    0   100   100     0
2018    No         400     100   0   100  200   0    0    0    0    0     0    0      0
.
.

Ответы [ 2 ]

0 голосов
/ 12 мая 2018

Вы можете выбрать данные несколько раз, используя запрос 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);
0 голосов
/ 12 мая 2018

Да.Вы можете изменить критерии Сумма для двух полей Да-Нет:

IIf(Sum([SumPrice]) Is Null,0,Sum([SumPrice])) AS TotalAllPricePerYear,
IIf(Sum([SumPrice]) Is Null Or ReceiptYesNo="No",0,Sum([SumPrice])) AS TotalYesPricePerYear,
IIf(Sum([SumPrice]) Is Null Or ReceiptYesNo="Yes",0,Sum([SumPrice])) AS TotalNoPricePerYear
...