将3个查询合并为一个

时间:2017-04-23 14:24:40

标签: sql vba ms-access transform

我有3个查询:

  • 第一个计算每个Qtr的总计和
  • 另外两个计算每个Qtr的总计,其中收据字段为[是]或[否]。

现在我有(例子):

 qtrTotals
 Year / Total / TotalQtr1 / TotalQtr2 / etc...

 qtrTotalsReceiptYes
 Year / TotalYes / TotalYesQtr1 / TotalYesQtr2 / etc...

 qtrTotalsReceiptNo
 Year / TotalNo / TotalNoQtr1 / TotalNoQtr2 / etc...

是否可以进行一次查询?

qtrTotals
Year / Total / TotalYes / TotalNo / TotalQtr1 / TotalYesQtr1 / TotalNoQtr1 / etc...

MySQL:

所有

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

是:

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

没有

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

谢谢。

PS。

tblExpences(正确的费用)有[Date] / [Price] / [ReceiptYesNo]。

tblExpences

 10/3/2017  /  200  /  Yes
 10/3/2017  /  200  /  No
 11/3/2017  /  100  /  Yes
 14/3/2017  /  100  /  No
 15/4/2017  /  200  /  Yes
 10/5/2017  /  200  /  No

3 个答案:

答案 0 :(得分:0)

TLDR;以下是如何使其发挥作用的一般形式。

Select *
From (Query1) as Q1
Join (Query2) as Q2 On 1
Join (Query3) as Q3 On 1;

它应该有效,因为3个查询只使用聚合函数,因此返回单行。因此,始终连接这些列将导致每列连接并形成一列多列。

答案 1 :(得分:0)

在我看来,你的查询和你说的他们给出的输出并不相同。看起来你必须做些事情将你的月份合并到几个季度。

无论如何,与第二次和第三次查询的风格类似:

TRANSFORM IIf(Sum([Price]) Is Null,0,Sum([Price])) AS SumPrice
SELECT Year([ExpensesDate]) AS [Year]
FROM tblExpenses, Numbers
WHERE (((Numbers.Number) Between 1 And 4))
GROUP BY Year([ExpensesDate])
PIVOT IIf([Number]=1,"TotalMonth"& [ReceiptYesNo] & Month([ExpensesDate]),IIf([Number]=2,"TotalMonth" & Month([ExpensesDate]),IIf([Number]=3,"TotalPerYear" & [ReceiptYesNo],"TotalPerYear"))) in ("TotalPerYear","TotalPerYearYes","TotalPerYearNo","TotalMonth1","TotalMonthYes1","TotalMonthNo1","TotalMonth2","TotalMonthYes2","TotalMonthNo2","TotalMonth3","TotalMonthYes3","TotalMonthNo3","TotalMonth4","TotalMonthYes4","TotalMonthNo4","TotalMonth5","TotalMonthYes5","TotalMonthNo5","TotalMonth6","TotalMonthYes6","TotalMonthNo6","TotalMonth7","TotalMonthYes7","TotalMonthNo7","TotalMonth8","TotalMonthYes8","TotalMonthNo8","TotalMonth9","TotalMonthYes9","TotalMonthNo9","TotalMonth10","TotalMonthYes10","TotalMonthNo10","TotalMonth11","TotalMonthYes11","TotalMonthNo11","TotalMonth12","TotalMonthYes12","TotalMonthNo12");

我发现我必须将其粘贴到新查询中,因为它会记住列标题的旧排序顺序。

这取决于名为Numbers的新表。这只需要一个名为Number的字段,并且应该填充(至少)四行数据,其中包含数字:1,2,3和4.像这样:

Table: Numbers

Number
------
1
2
3
4

NB :我更正了“费用”的拼写,这意味着您需要调整数据或查询,以便他们同意。

答案 2 :(得分:0)

好的,另一种方法。首先,我假设您的三个查询保存为:qryTotalTotalqryTotalYesqryTotalNo

SELECT qryTotalTotal.Year,
qryTotalTotal.TotalPricePerYear AS TotalPerYear,
qryTotalYes.TotalPricePerYear AS TotalPerYearYes,
qryTotalNo.TotalPricePerYear AS TotalPerYearNo,
qryTotalYes.[1]+qryTotalNo.[1] as TotalMonth1,
qryTotalYes.[1] as TotalMonthYes1,
qryTotalNo.[1] as TotalMonthNo1,
qryTotalYes.[2]+qryTotalNo.[2] as TotalMonth2,
qryTotalYes.[2] as TotalMonthYes2,
qryTotalNo.[2] as TotalMonthNo2,
qryTotalYes.[3]+qryTotalNo.[3] as TotalMonth3,
qryTotalYes.[3] as TotalMonthYes3,
qryTotalNo.[3] as TotalMonthNo3,
qryTotalYes.[4]+qryTotalNo.[4] as TotalMonth4,
qryTotalYes.[4] as TotalMonthYes4,
qryTotalNo.[4] as TotalMonthNo4,
qryTotalYes.[5]+qryTotalNo.[5] as TotalMonth5,
qryTotalYes.[5] as TotalMonthYes5,
qryTotalNo.[5] as TotalMonthNo5,
qryTotalYes.[6]+qryTotalNo.[6] as TotalMonth6,
qryTotalYes.[6] as TotalMonthYes6,
qryTotalNo.[6] as TotalMonthNo6,
qryTotalYes.[7]+qryTotalNo.[7] as TotalMonth7,
qryTotalYes.[7] as TotalMonthYes7,
qryTotalNo.[7] as TotalMonthNo7,
qryTotalYes.[8]+qryTotalNo.[8] as TotalMonth8,
qryTotalYes.[8] as TotalMonthYes8,
qryTotalNo.[8] as TotalMonthNo8,
qryTotalYes.[9]+qryTotalNo.[9] as TotalMonth9,
qryTotalYes.[9] as TotalMonthYes9,
qryTotalNo.[9] as TotalMonthNo9,
qryTotalYes.[10]+qryTotalNo.[10] as TotalMonth10,
qryTotalYes.[10] as TotalMonthYes10,
qryTotalNo.[10] as TotalMonthNo10,
qryTotalYes.[11]+qryTotalNo.[11] as TotalMonth11,
qryTotalYes.[11] as TotalMonthYes11,
qryTotalNo.[11] as TotalMonthNo11,
qryTotalYes.[12]+qryTotalNo.[12] as TotalMonth12,
qryTotalYes.[12] as TotalMonthYes12,
qryTotalNo.[12] as TotalMonthNo12
FROM (qryTotalTotal LEFT JOIN qryTotalYes ON qryTotalTotal.Year = qryTotalYes.Year)
LEFT JOIN qryTotalNo ON qryTotalTotal.Year = qryTotalNo.Year;