我现在遇到一个问题已经有一段时间了,找不到适合我的解决方案。可能是我没有正确执行操作,或者有其他方法可以更好地工作。我对任何想法都很开放,也很感激。
我在访问中有一个表(tblDocQueue),类似于下面显示的表。数据来自我们在工作中使用的较旧应用程序的数据摘录,因此无法更改源或摘录。我们将数据上传到Access,以分析和建立围绕它的指标。下表如下:
ID DocName OwnerName AccountNum DocRef
1 Doc 1 Matt 1001 Z0005638
2 Doc 1 Matt 1002 Z0005638
3 Doc 1 Tony 5010 Z0005639
4 Doc 2 Luke 1050 Z0005640
5 Doc 3 Luke 1050 Z0005641
6 Doc 3 Gary 1234 Z0005641
7 Doc 4 John 8789 Z0005642
8 Doc Ed 8789 Z0005642
9 Doc 5 Ed 8790 Z0005643
10 Doc 5 Connie 4579 Z0005644
11 Doc 6 Mary 3616 Z0005645
12 Doc 6 Lucy 4795 Z0005646
13 Doc 6 Tina 4795 Z0005646
14 Doc 7 Matt 1001 Z0005638
15 Doc 7约翰8789 Z0005647
列数比列出的数更多,但它们与问题无关。我试图基于三列(DocName,OwnerName,Doc Ref)删除重复项,并保留一个唯一值。我使用以下SQL进行了此操作,但是开始要花费几个小时(最多7个)来处理大约500,000行数据。我不确定效率问题是由于使用最小/最大还是其他原因引起的
SELECT tblDocQueue.ID AS Expr1, tblDocQueue.DocName AS Expr2,
tblDocQueue.OwnerName AS Expr3, tblDocQueue.AcctNo AS Expr4,
tblDocQueue.ExpDate AS Expr5, tblDocQueue.EffectiveDate AS Expr6,
tblDocQueue.SignatureDate AS Expr7, tblDocQueue.DocBNYSts AS Expr8,
tblDocQueue.StsDate AS Expr9, tblDocQueue.UserSts AS Expr10,
tblDocQueue.Location AS Expr11, tblDocQueue.Ackngmt AS Expr12,
tblDocQueue.OPID AS Expr13, tblDocQueue.Comments AS Expr14,
tblDocQueue.DocRef AS Expr15, tblDocQueue.ExternalComment AS Expr16,
tblDocQueue.FirstName AS Expr17, tblDocQueue.LastName AS Expr18,
tblDocQueue.ClientID AS Expr19, tblDocQueue.Address AS Expr20,
tblDocQueue.CountryCode AS Expr21
FROM tblDocQueue
WHERE ((([tblDocQueue].[ID])=(
SELECT Min(t.[ID])
FROM [tblDocQueue] AS t
WHERE t.[DocRef]=[tblDocQueue].[DocRef]
AND t.[DocName]=[tblDocQueue].[DocName])));
这对于企业团队来说是无法接受的。然后,我在VBA中开发了一种解决方法,可以使用excel中预装的removeplicates函数将数据导出到excel文件,然后将唯一数据导入到另一个表中。在excel中只需几秒钟。随着该数据库的使用开始扩大,并且我将以类似的方式每天删除数百个数据集的重复项,因此我试图在不使用上述变通方法的情况下使它工作。
以上示例数据的预期结果将是:
ID DocName OwnerName帐户编号DocRef
1 Doc 1 Matt 1001 Z0005638
3 Doc 1 Tony 5010 Z0005639
4 Doc 2 Luke 1050 Z0005640
5 Doc 3 Luke 1050 Z0005641
6 Doc 3 Gary 1234 Z0005641
7 Doc 4 John 8789 Z0005642
8 Doc 5 Ed 8789 Z0005642
9 Doc 5 Ed 8790 Z0005643
10 Doc 5 Connie 4579 Z0005644
11 Doc 6 Mary 3616 Z0005645
12 Doc 6 Lucy 4795 Z0005646
13 Doc 6 Tina 4795 Z0005646
14 Doc 7 Matt 1001 Z0005638
15 Doc 7 John 8789 Z0005647
如果有人可以通过SQL帮助我:
任何帮助都将不胜感激!
考虑一个简单的聚合查询,将其分组在您需要不同的DocName,OwnerName和DocRef的列上。然后,使用ID和AccountNumMin()
中的ID,其中还包括所有其他列:
SELECT Min(tblDocQueue.ID) AS MinOfID,
tblDocQueue.DocName,
tblDocQueue.OwnerName,
Min(tblDocQueue.AccountNum) AS MinOfAccountNum,
tblDocQueue.DocRef
FROM tblDocQueue
GROUP BY tblDocQueue.DocName,
tblDocQueue.OwnerName,
tblDocQueue.DocRef
ORDER BY Min(tblDocQueue.ID);
输出
MinOfID DocName OwnerName MinOfAccountNum DocRef
1 Doc 1 Matt 1001 Z0005638
3 Doc 1 Tony 5010 Z0005639
4 Doc 2 Luke 1050 Z0005640
5 Doc 3 Luke 1050 Z0005641
6 Doc 3 Gary 1234 Z0005641
7 Doc 4 John 8789 Z0005642
8 Doc 5 Ed 8789 Z0005642
9 Doc 5 Ed 8790 Z0005643
10 Doc 5 Connie 4579 Z0005644
11 Doc 6 Mary 3616 Z0005645
12 Doc 6 Lucy 4795 Z0005646
13 Doc 6 Tina 4795 Z0005646
14 Doc 7 Matt 1001 Z0005638
15 Doc 7 John 8789 Z0005647
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句