我可以请求您提供以下帮助吗?
我正在尝试计算结果中从一条记录到下一条记录的更改。如果我向您展示我当前的查询和结果,它可能会有所帮助......
SELECT A.AuditDate, COUNT(A.NickName) as [TAccounts],
SUM(IIF((A.CurrGBP > 100 OR A.CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits A
GROUP BY A.AuditDate;
查询给了我这些结果......
AuditDate D/M/Y TAccounts Funded
--------------------------------------------
30/12/2011 506 285
04/01/2012 514 287
05/01/2012 514 288
06/01/2012 516 288
09/01/2012 520 289
10/01/2012 522 289
11/01/2012 523 290
12/01/2012 524 290
13/01/2012 526 291
17/01/2012 531 292
18/01/2012 532 292
19/01/2012 533 293
20/01/2012 537 295
理想情况下,我想得到的结果与以下类似......
AuditDate D/M/Y TAccounts TChange Funded FChange
------------------------------------------------------------------------
30/12/2011 506 0 285 0
04/01/2012 514 8 287 2
05/01/2012 514 0 288 1
06/01/2012 516 2 288 0
09/01/2012 520 4 289 1
10/01/2012 522 2 289 0
11/01/2012 523 1 290 1
12/01/2012 524 1 290 0
13/01/2012 526 2 291 1
17/01/2012 531 5 292 1
18/01/2012 532 1 292 0
19/01/2012 533 1 293 1
20/01/2012 537 4 295 2
查看'17 / 01/2012'的行,'TChange'的值为5,因为'TAccounts'从之前的526增加到531.而'FChange'将基于'Funded'领域。我想要注意的事实是这个例子的前一行是'13 / 01/2012'。我的意思是,有些日子我没有数据(例如在周末)。
我想我需要使用SubQuery,但我真的很想弄清楚从哪里开始。你能告诉我如何得到我需要的结果吗?
我正在使用MS Access 2010
非常感谢你的时间。
约翰尼。
答案 0 :(得分:1)
这是你可以尝试的一种方法......
SELECT B.AuditDate,B.TAccounts,
B.TAccount -
(SELECT Count(NickName) FROM Audits WHERE AuditDate=B.PrevAuditDate) as TChange,
B.Funded -
(SELECT Count(*) FROM Audits WHERE AuditDate=B.PrevAuditDate AND (CurrGBP > 100 OR CurrUSD > 100)) as FChange
FROM (
SELECT A.AuditDate,
(SELECT Count(NickName) FROM Audits WHERE AuditDate=A.AuditDate) as TAccounts,
(SELECT Count(*) FROM Audits WHERE (CurrGBP > 100 OR CurrUSD > 100)) as Funded,
(SELECT Max(AuditDate) FROM Audits WHERE AuditDate<A.AuditDate) as PrevAuditDate
FROM
(SELECT DISTINCT AuditDate FROM Audits) AS A) AS B
我没有使用Group By
而是使用subquerys同时获得TAccounts和Funded,以及之前的审核日期,然后在主SELECT
语句中使用它来获得TAccounts和Funded再次,但这次是前一个日期,以便可以对他们进行任何所需的计算。
但我想这可能会很慢处理
答案 1 :(得分:0)
很遗憾MS在Access中从未使这类事情变得简单,你在报告中使用了多少行?
如果它低于65K,那么我建议将数据转储到Excel电子表格中,并使用简单的公式来计算行之间的差异。
答案 2 :(得分:0)
您可以尝试以下内容(sql未经测试,需要进行一些更改)
选择
A.AuditDate,
A.TAccounts,
A.TAccounts - B.TAccounts AS TChange,
A.Funded,
A.Funded - B.Funded AS FChange
这
( SELECT
ROW_NUMBER() OVER (ORDER BY AuditDate DESC) AS ROW,
AuditDate,
COUNT(NickName) as [TAccounts],
SUM(IIF((CurrGBP > 100 OR CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits
GROUP BY AuditDate
) A
INNER JOIN
( SELECT
ROW_NUMBER() OVER (ORDER BY AuditDate DESC) AS ROW,
AuditDate,
COUNT(NickName) as [TAccounts],
SUM(IIF((CurrGBP > 100 OR CurrUSD > 100), 1, 0)) as [Funded]
FROM Audits
GROUP BY AuditDate
) B ON B.ROW = A.ROW + 1