ms access-row\u number分区依据

js4nwp54  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(275)

我正在尝试在ms access中执行与行数分区等效的操作。
我在access中有一个叫做fx\u rates的货币表。
此表有货币字段、汇率字段和日期字段。我想在一个字段中插入一个记录计数,这个字段基本上会显示这个表中是否有重复的记录,因为我想删除重复的值,所以任何计数大于1的记录都可以。
例如,如果我有以下数据:

Currency    ExchangeRate    Date
EUR         1               31/12/2019
EUR         1               31/12/2019
GBP         1.1             31/12/2019
GBP         1.5             31/12/2019

我希望输出像这样:

Currency    ExchangeRate    Date            Count
EUR         1               31/12/2019      1
EUR         1               31/12/2019      2
GBP         1.1             31/12/2019      1
GBP         1.5             31/12/2019      1

通过在线查找,我得到了以下代码:

Call RunSQL("INSERT INTO FX_Rates_New  " & _
    "SELECT " & _
    " t1.Currency, t1.Exchange_Rate, t1.Date_of_Report" & _
    ",COUNT(*) AS Count_of_Record " & _
    "FROM FX_Rates AS t1 " & _
    "INNER JOIN FX_Rates AS t2 ON t1.Currency = t2.Currency AND t1.Date_of_Report = t2.Date_of_Report " & _
    "GROUP BY " & _
    " t1.Currency, t1.Exchange_Rate, t1.Date_of_Report " & _
    "ORDER BY t1.Date_of_Report, t1.Currency ")

然而,上面只是将唯一的记录插入到一个新表中,我知道只有2个项目的计数是4。
理想情况下,我希望用前面演示的每个表旁边的计数来更新当前表。有人知道最好的方法吗?
提前谢谢,
编辑:如果有人想做类似的事情,我实际上使用了下面的代码,在6月7日的回复后,我添加了一个autonum字段,这使我更容易完成我想要的:

Call RunSQL("DELETE * FROM FX_Rates " & _
    "WHERE FX_Rates.ID NOT IN ( " & _
    "SELECT Min(FX_Rates.ID) AS ID " & _
    "FROM FX_Rates " & _
    "GROUP BY  " & _
    "FX_Rates.Currency, FX_Rates.Date) ")
wgx48brx

wgx48brx1#

需要唯一的记录标识符-应提供自动编号字段(下面查询中的id)。考虑:

SELECT FX_Rates.Currency, FX_Rates.Exchange_Rate, FX_Rates.Date_of_Report, 
  (SELECT Count(*) AS HowMany
   FROM FX_Rates AS Dupe
   WHERE Dupe.ID < FX_Rates.ID AND Dupe.Currency=FX_Rates.Currency 
   AND Dupe.Exchange_Rate=FX_Rates.Exchange_Rate
   AND Dupe.Date_of_Report=FX_Rates.Date_of_Report)+1 AS GrpSeq
FROM FX_Rates;

更多信息,请查看http://allenbrowne.com/ranking.html

相关问题