开发者

Extract Both Counts AND Earliest Instance from my Dataset

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

 U开发者_如何学JAVAserDetails
  uId
  uName

and

PassDates
   fkUser
   CourseId
   PassDate

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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜