访问SQL - 在计数查询中显示其他列

时间:2012-10-26 18:44:55

标签: sql ms-access count ms-access-2007

我正在尝试获取重复的详细列表,Foldername是重复值。 我需要为重复的每个实例计算重复实例和其他相应的列,例如FullPath

我正在尝试返回每个完整路径的列表,其中包含相应的foldername计数,以及其他列,例如单位,大小等。

FolderName (has the possibility of a duplicate)
FullPath (is completly unique)

我对此进行了一些尝试;在SQL Server中,这似乎更明显......但在Access中,我在这里有点迷失。

这是我到目前为止提出的基本SQL:

    /* ''''''''''''''Works but doesn't return Count'''''''''''''' */
Select Snapshot.Unit, Snapshot.FolderName, Snapshot.FullPath
From Snapshot
Where Snapshot.FolderName in 
(
    Select Snapshot.FolderName
    From Snapshot
    Group by Snapshot.Foldername
    Having Count(Snapshot.FolderName)> 1
)
Order by Snapshot.FolderName, Snapshot.FullPath

以下是我想要的示例输出:

Unit       FolderName     FullPath                          Count
BCU        Misc           C:\blah\blah\blah\Misc            2
ENV        Misc           R:\blah\blah\blah\Misc            2
CLR        Monkey         Q:\blah\blah\blah\blah\Monkey     17
ATL        Zebra          Z:\blah\blah\zoo\Zebra            24

我引用了: Having trouble using count() in Access SQL query

1 个答案:

答案 0 :(得分:5)

在子查询中进行计数,并将INNER JOIN快照计入子查询。

SELECT s.Unit, s.FolderName, s.FullPath, sub.num_dupes
FROM
    Snapshot AS s
    INNER JOIN
    (
        SELECT FolderName, Count(*) AS num_dupes
        FROM Snapshot
        GROUP BY FolderName
        HAVING Count(*)> 1
    ) AS sub
    ON s.FolderName = sub.FolderName
ORDER BY s.FolderName, s.FullPath