从我的数据集中提取计数和最早的实例

发布于 2024-10-22 09:25:16 字数 735 浏览 1 评论 0原文

使用 Microsoft Sql 2000,

我需要能够通过电子邮件发送详细说明许多事件的月度报告。

(我的电子邮件有点可疑)。

我需要通过电子邮件发送的数据包括有关人们参加的某些课程数量的报告。 (到目前为止很简单,几个内部联接和一个 Count() ,我就在那里。)

除此之外,我们运行的一些内部课程有一个到期日期,这会提示推荐人课程。我已经能够粗略地获取我需要的数据,方法是使用第一部分的 sql 代码并将结果集粘贴到临时表中,然后迭代该表中的每一行,获取用户 ID,查询用户课程出勤情况,按日期排序,使最早的位于顶部,并只取 TOP 1 记录。

这看起来效率很低,所以有什么方法可以修改我当前的查询,以便我也可以获得用户参加的最早课程的日期?

例如

SELECT uName, COUNT(uId), [ not sure what would go in here] FROM UserDetails
  INNER JOIN PassDates
  ON PassDates.fkUser = uId)
GROUP BY uName, uId 

,在哪里

 UserDetails
  uId
  uName

PassDates
   fkUser
   CourseId
   PassDate

希望我已经解释得足够好,有人可以帮助我。

Using Microsoft Sql 2000

I have a requirement to be able to email a monthly report that details a number of events.

(I have got the email bit sussed).

Amongst the data I need to email is a report on the number of certain courses people have attended. (so far so easy, couple of inner joins and a Count() and Im there.)

To add to that, some of the internal courses that we run have an expiry date which prompts a referesher course. I have been able to crudely get the data I need by using the sql code for part one and sticking the result set into a temp table, then by iterating over each row in that table, getting the user Id, querying the users course attendences, sorting it on date so that the earliest is at the top, and just taking the TOP 1 record.

This seems so inefficient, so is there any way I can ammend my current query so that I can also get the date of just the earliest course that the user attended?

i.e.

SELECT uName, COUNT(uId), [ not sure what would go in here] FROM UserDetails
  INNER JOIN PassDates
  ON PassDates.fkUser = uId)
GROUP BY uName, uId 

where, for examples sake

 UserDetails
  uId
  uName

and

PassDates
   fkUser
   CourseId
   PassDate

Hope Ive explained this well enough for someone to help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-10-29 09:25:16

要回答问题..

SELECT uName, COUNT(uId), MIN(PassDate)
FROM UserDetails
INNER JOIN PassDates ON PassDates.fkUser = uId
GROUP BY uName, uId

如果您的用户还没有任何课程,您可以将其转换为左连接

To put an answer to the question..

SELECT uName, COUNT(uId), MIN(PassDate)
FROM UserDetails
INNER JOIN PassDates ON PassDates.fkUser = uId
GROUP BY uName, uId

You can turn it into a left join if you have users without any courses (yet)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文