Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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
带条件的SQL查询-计算每个ID的新值和返回值_Sql_Ms Access - Fatal编程技术网

带条件的SQL查询-计算每个ID的新值和返回值

带条件的SQL查询-计算每个ID的新值和返回值,sql,ms-access,Sql,Ms Access,因此,这是我在过去几天里一直试图解决但没有成功的问题:我有一个表格,它记录了由其唯一参与者识别的人的参与情况,以及由其唯一事件ID识别的某些事件 我想使用MS Access 2010编写一个SQL查询,它为每个事件返回返回的参与者数量,即已参与另一个事件且EventId较低的参与者数量、首次出现的新参与者数量(如果按EventId排序)以及该事件的参与者总数 例如: ParticipantId | EventId 1 1 1 3 1

因此,这是我在过去几天里一直试图解决但没有成功的问题:我有一个表格,它记录了由其唯一参与者识别的人的参与情况,以及由其唯一事件ID识别的某些事件

我想使用MS Access 2010编写一个SQL查询,它为每个事件返回返回的参与者数量,即已参与另一个事件且EventId较低的参与者数量、首次出现的新参与者数量(如果按EventId排序)以及该事件的参与者总数

例如:

ParticipantId | EventId
1               1
1               3
1               4
2               3
2               4
3               5
将提供:

EventId | New | Returning | Total
1         1     0           1
3         1     1           2
4         0     2           2
5         1     0           1
这甚至可以从一开始吗?你知道我该怎么做吗


非常感谢

您可以使用子查询来确定用户的第一个事件。然后,Access’iif允许您仅统计新列的第一个事件:


非常感谢你,这真是妙不可言!作为一个新手,我可能会使用更简单的查询在代码中进行计数,但是您的解决方案更加优雅和高效!!
select  e.eventid
,       sum(iif(e.eventid = p.FirstEvent,1,0)) as [New]
,       sum(iif(e.eventid <> p.FirstEvent,1,0)) as Returning
,       count(p.participantid) as Total
from    (
        select  participantid
        ,       min(eventid) as FirstEvent
        from    Table1
        group by
                participantid
        ) as p
left join
        Table1 e
on      p.participantid = e.participantid
group by
        e.eventid