为同一字段上有多个计数列的access数据库配置数据集

sqxo8psd  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(307)

我有这个sql到目前为止,它的工作很好。对于后台,这是来自尝试配置与access数据库相关的xsd数据集的查询,我正在vsreportbuilder中处理它以生成报告。people保存关于people的信息,sessionsloged保存关于活动运行的信息,其中id是针对每个特定会话的,但是sessionid更像是活动的类型,因此您可以为每个sessionid获取多个id,这些表由peoplesessions表链接:

SELECT People.AriadneNo, People.Forename, People.Surname, People.Gender, People.MobileNo, Countries.Country, COUNT(SessionsLogged.ID) AS CountOfID, SessionsLogged.SessionID, People.EmailAddress
FROM     (((SessionsLogged INNER JOIN
                  PeopleSessions ON SessionsLogged.ID = PeopleSessions.SLID) LEFT OUTER JOIN
                  People ON PeopleSessions.PersonID = People.ID) LEFT OUTER JOIN
                  Countries ON People.Country = Countries.ID)
WHERE  (SessionsLogged.SessionID IN (1))
GROUP BY People.AriadneNo, People.Forename, People.Surname, People.Gender, People.MobileNo, Countries.Country, SessionsLogged.SessionID, People.EmailAddress

目前,我可以从中得到一份报告,其中提供了参加某个选定活动的每个人的详细信息,以及他们参加该活动的次数。我想修改这一点,这样我可以得到每个人谁参加了5个选定的活动,然后5列,其中显示了他们参加每种活动的次数之一的详细信息。我尝试过使用case,但这给了我一个错误(我想是因为它是一个access数据库?),我也尝试过iif和子查询,但我要么做错了(可能),要么它们不起作用,因为数据集告诉我我做错了什么。

xkrw2x1b

xkrw2x1b1#

正如您所期望的,您可以为每个感兴趣的sessionid创建一个计算变量。下面是模拟数据的sql,我在其中创建了countsession1和countsession2。换句话说,如果sessionid为1 countsessionid为1,否则为0,我们求和而不是计算结果。忽略正确的连接这正是我设置模拟数据的方式,

下面是生成的sql:

SELECT People.ForeName, People.SurName, SessionsLogged.SessionID, Sum(IIf([SessionsLogged].[SessionID]=1,1,0)) AS CountSession1, Sum(IIf([SessionsLogged].[SessionID]=2,1,0)) AS CountSession2
FROM SessionsLogged INNER JOIN ((Countries RIGHT JOIN People ON Countries.ID = People.Country) RIGHT JOIN PeopleSessions ON People.ID = PeopleSessions.PersonID) ON SessionsLogged.ID = PeopleSessions.SLID
GROUP BY People.ForeName, People.SurName, SessionsLogged.SessionID;

相关问题