Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Access SQL查询中使用count()时遇到问题_Sql_Ms Access - Fatal编程技术网

在Access SQL查询中使用count()时遇到问题

在Access SQL查询中使用count()时遇到问题,sql,ms-access,Sql,Ms Access,使用Access 2007 我有一个包含以下字段的表清单: 集装箱 福德里德 我的目标是找出哪些folderid对应于多个ContainerID,以及这些记录的ContainerID是什么。我认为最好的处理方法是执行一个查询,返回ContainerID、FolderID和每个FolderID的计数,这样我就可以对列表进行排序,将计数大于1的记录放在顶部。我试过这样做: select ContainerID,FolderID,count(FolderID) from (select distinc

使用Access 2007

我有一个包含以下字段的表清单:

集装箱 福德里德

我的目标是找出哪些folderid对应于多个ContainerID,以及这些记录的ContainerID是什么。我认为最好的处理方法是执行一个查询,返回ContainerID、FolderID和每个FolderID的计数,这样我就可以对列表进行排序,将计数大于1的记录放在顶部。我试过这样做:

select ContainerID,FolderID,count(FolderID) from (select distinct * from Inventory);
但是Access给了我一条错误消息:“您试图执行的查询没有将指定的表达式‘ContainerID’作为聚合函数的一部分。”

我怎样才能得到我想要的结果

编辑:
我正在尝试你的所有解决方案,但是所有的解决方案都会得到很多重复的行。我只是试图用不同的方法过滤掉它们,但由于某种原因,访问被冻结了。我得回家了,所以我明天早上再检查一下。感谢您提供建议。

请尝试以下查询

select
     I.*
    ,T.FolderCount
from
    Inventory I
inner join
(
    select
          ContainerID
        , Count(*) as FolderCount
    from
        Inventory
    group by
        ContainerID
    having
        Count(*) > 1
) T
on
    I.ContainerID = T.ContainerID
编辑:

select
     I.*
    ,T.ContainerCount
from
    Inventory I
inner join
(
    select
          FolderID
        , Count(*) as ContainerCount
    from
        Inventory
    group by
        FolderID
    having
        Count(*) > 1
) T
on
    I.FolderID = T.FolderID

请尝试以下查询

select
     I.*
    ,T.FolderCount
from
    Inventory I
inner join
(
    select
          ContainerID
        , Count(*) as FolderCount
    from
        Inventory
    group by
        ContainerID
    having
        Count(*) > 1
) T
on
    I.ContainerID = T.ContainerID
编辑:

select
     I.*
    ,T.ContainerCount
from
    Inventory I
inner join
(
    select
          FolderID
        , Count(*) as ContainerCount
    from
        Inventory
    group by
        FolderID
    having
        Count(*) > 1
) T
on
    I.FolderID = T.FolderID

这应该可以做到:

SELECT ContainerID, FolderID, count(FolderID) 
FROM    Inventory 
GROUP BY ContainerID, FolderID; 

count函数需要GROUP BY来处理特定的分组,否则它将对所有可用数据进行操作。除非您只有一个唯一的ContainerID和FolderID组合,否则不能选择这些字段以及聚合函数“count”的结果。

这应该可以做到:

SELECT ContainerID, FolderID, count(FolderID) 
FROM    Inventory 
GROUP BY ContainerID, FolderID; 

count函数需要GROUP BY来处理特定的分组,否则它将对所有可用数据进行操作。除非您只有一个唯一的ContainerID和FolderID组合,否则无法选择这些字段以及聚合函数“count”的结果。

步骤1:获取包含多个ContainerID的FolderID:

select folderid 
from inventory 
group by folderid 
having count(folderid)>1
步骤2:获取所有这些FolderId及其对应的ContainerId的列表:

select * 
from inventory 
where folderid in
(
   select folderid 
   from inventory 
   group by folderid 
   having count(folderid)>1
)
order by folderid, containerid

步骤1:获取具有多个ContainerID的FolderID:

select folderid 
from inventory 
group by folderid 
having count(folderid)>1
步骤2:获取所有这些FolderId及其对应的ContainerId的列表:

select * 
from inventory 
where folderid in
(
   select folderid 
   from inventory 
   group by folderid 
   having count(folderid)>1
)
order by folderid, containerid

很遗憾,我没有access 2007,但在sql server中,以下功能可以正常工作:

create table Inventory
(
 ContainerID int,
 FolderID int
)

insert into Inventory (ContainerID, FolderID)
select 1, 1
union all select 1, 2
union all select 1, 3
union all select 1, 1
union all select 2, 1
union all select 2, 2
union all select 3, 1

select distinct i1.ContainerID, i1.FolderID
from Inventory i1
inner join (select FolderID
            from Inventory a
            group by FolderID
            having count(distinct ContainerID) > 1) i2
on i1.FolderID = i2.FolderID

为澄清我提交此答案的原因,添加了示例。根据我对这个问题的理解,结果是容器/文件夹对是多个containerID而不是多行。

不幸的是,我没有access 2007,但在sql server中,以下功能有效:

create table Inventory
(
 ContainerID int,
 FolderID int
)

insert into Inventory (ContainerID, FolderID)
select 1, 1
union all select 1, 2
union all select 1, 3
union all select 1, 1
union all select 2, 1
union all select 2, 2
union all select 3, 1

select distinct i1.ContainerID, i1.FolderID
from Inventory i1
inner join (select FolderID
            from Inventory a
            group by FolderID
            having count(distinct ContainerID) > 1) i2
on i1.FolderID = i2.FolderID

为澄清我提交此答案的原因,添加了示例。根据我对这个问题的理解,结果是容器/文件夹对是多个containerID而不是多行。

我尝试了这个方法,但它返回了很多结果,其中FolderID对于许多不同的containerID是相同的。我试着从另一个角度来理解它——对于每一个FolderID,我想知道它对应的是哪一个集装箱,只要有不止一个集装箱id。我从另一个角度来理解。更新了代码。看看这是否有帮助。好的,这是消除重复条目,但我仍然得到只有一个FolderID实例存在的结果。我只想返回带有多个ContainerID的FolderID。例如,如果我的集合是F1 C1、F1 C2、F2 C3、F2 C3,那么我想返回F1 C1、F1 C2。现在查询返回F1 C1,F1 C2,F2 C3。好的,我知道了。我使用了更多的嵌套查询来获取所需的数据。谢谢你的帮助!我尝试了这个,但是它返回了很多结果,其中FolderID对于许多不同的集装箱来说是相同的。我试着从另一个角度来理解它——对于每一个FolderID,我想知道它对应的是哪一个集装箱,只要有不止一个集装箱id。我从另一个角度来理解。更新了代码。看看这是否有帮助。好的,这是消除重复条目,但我仍然得到只有一个FolderID实例存在的结果。我只想返回带有多个ContainerID的FolderID。例如,如果我的集合是F1 C1、F1 C2、F2 C3、F2 C3,那么我想返回F1 C1、F1 C2。现在查询返回F1 C1,F1 C2,F2 C3。好的,我知道了。我使用了更多的嵌套查询来获取所需的数据。谢谢你的帮助!这是一个基本问题——其他答案可能更符合OP的实际需求,但聚合查询的
SELECT
子句中的所有字段必须聚合或出现在
groupby
子句中。这是一个基本问题——其他答案可能更符合OP的实际需求,但聚合查询的
SELECT
子句中的所有字段必须聚合或出现在
GROUP BY
子句中。