|ID |AN |CRN |CRT |SD |ED
+-------------------------------------------------------------------
|1 |a |x |R |01-Aug-10| 09-Sep-10
|1 |a |y |R |10-Sep-10| 08-Dec-10
|1 |a |z |R |20-Dec-10|
|1 |a |z |S |01-Aug-10| 31-Jan-10
|1 |a |v |S |01-Feb-10|
|2 |b |d |S |01-Aug-10| 29-Aug-10
|2 |b |d |S |31-Aug-10|
这是我从上表中寻找的东西。我希望得到sd和ed之间的区别。查询应该看看ID是否相同,如果它是相同的那么它应该看起来CRT是相同的,如果它们是相同的那么它应该显示差异,否则它应该为0,所以查询需要检查每一行..现在如果帐户不同,则查询应默认显示为0。
所以结果应该显示类似的东西
|ID |AN |CRN |CRT |SD |ED |Difference
+-------------------------------------------------------------------
|1 |a |x |R |01-Aug-10| 09-Sep-10 |0 *(since this is 1st entry in respect to the CRT within the same ID)*
|1 |a |y |R |10-Sep-10| 08-Dec-10 |1 *(10sept(SD)-9sept(ED))*
|1 |a |z |R |20-Dec-10| |12
|1 |a |z |S |01-Aug-10| 31-Jan-10 |0 *(since this is 1st entry in respect to the CRT within the same ID)*
|1 |a |v |S |01-Feb-10| |1 *(1Feb(SD)-31Jan(ED))*
|2 |b |d |S |01-Aug-10| 29-Aug-10 |0 *(since this is a new ID)*
|2 |b |d |S |31-Aug-10| |2
在differenc col中我只需要没有()中提到的旁白的差异。 请帮忙......
答案 0 :(得分:0)
获得此更正的关键是使表具有与您的行顺序相匹配的顺序(自动编号)键。如果您的表上还没有类似的键,则需要将值插入到新的临时表中,并将自动编号字段作为键。我已经在一个名为MyTable的表中使用名为TempID的自动编号键。
然后,您需要使用以下定义创建名为PreviousRow的查询:
SELECT [TempID]+1 AS PreviousTempID, MyTable.ID, MyTable.AN, MyTable.CRN, MyTable.CRT, MyTable.SD, MyTable.ED FROM MyTable;
然后您的结果是以下查询:
SELECT MyTable.ID, MyTable.AN, MyTable.CRN, MyTable.CRT, MyTable.SD, MyTable.ED, IIf([MyTable.ID]=[PreviousRow.ID] And [MyTable.CRT]=[PreviousRow.CRT],DateDiff("d",[PreviousRow.ED],[MyTable.SD]),0) AS Difference FROM MyTable LEFT JOIN PreviousRow ON MyTable.TempID = PreviousRow.PreviousTempID;