|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 31, 2012 2:45 PM
Points: 3,
Visits: 26
|
|
I have a table of Activities.
Table Activities: Id Int Identity CategoryId Int Activity nvarchar(255) StartDateTime datetime
This activities table has a CategoryId in each row as well as a StartDateTime column for each activity. My task is to come up with a query that produces the last activity for each CategoryId in the table. Currently there are 731 distinct CategoryId's in the table with a total of 71,000 Activity records.
Any help formulating a query that gives me the last activity(by StartDateTime) for each CategoryId in the table would greatly be apreciated.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Yesterday @ 10:38 AM
Points: 742,
Visits: 2,088
|
|
How about:
SELECT MAX(Datefield) as MostRecentDate, CategoryID FROM Table GROUP BY CategoryID
Or, did I miss something?
Michael L John To properly post on a forum: http://www.sqlservercentral.com/articles/61537/
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 31, 2012 2:45 PM
Points: 3,
Visits: 26
|
|
That did it. Wasn't even thinking about MAX()!!!!
That was the missing link. I knew there was a simple solution.
Thanks a bunch.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, January 31, 2012 2:45 PM
Points: 3,
Visits: 26
|
|
This is what i used: SELECT MAX(ACTIVITIES.StartDateTime) as LastStartDateTime, ACTIVITIES.CategoryId FROM Activities GROUP BY ACTIVITIES.CategoryId
This query gave me the 731 results i wanted, however i also need the Activity.Id
I tried this but was unsuccessful:
SELECT MAX(ACTIVITIES.StartDateTime) as LastStartDateTime, ACTIVITIES.CategoryId, ACTIVITIES.Id FROM Activities GROUP BY ACTIVITIES.CategoryId, ACTIVITIES.Id
This query results in 69378 results.
Am I still missing something here???
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 09, 2012 4:22 PM
Points: 187,
Visits: 76
|
|
could you:
SELECT top(1) [ACTIVITIES.StartDateTime] ,ACTIVITIES.CategoryId ,ACTIVITIES.Id FROM [Activities]
order by [ACTIVITIES.StartDateTime] desc
?
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Yesterday @ 10:38 AM
Points: 742,
Visits: 2,088
|
|
Think about what you have, and what you need.
By grouping both fields, you have all of the unique combinations of the category and activity for the date.
You table probably contains data like this: 01/01/2000 1 1 01/02/2000 1 2 01/03/2000 1 3
If this is true, you want one row returned, containing: 01/03/2000 1 3
By grouping on both fields, you would get all three rows in the above example, not one, which is what you want. There are numerious ways to do this, but joining on a subquery should provide you with the results you need. How about: SELECT A.StartDateTime, A.CategoryId, A.Id FROM Activities as A INNER JOIN (SELECT MAX(ACTIVITIES.StartDateTime) as LastStartDateTime, ACTIVITIES.CategoryId FROM Activities GROUP BY ACTIVITIES.CategoryId) as B ON A.StartDateTime = B.LastStartDateTime A.CategoryID = B.CategoryID
Michael L John To properly post on a forum: http://www.sqlservercentral.com/articles/61537/
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 6:20 AM
Points: 69,
Visits: 131
|
|
Here's another way using row_number/partitioning
DECLARE @tDUMMY TABLE ( StartDate DATE, Activity VARCHAR(100), CategoryID INT );
INSERT INTO @tDUMMY
SELECT CONVERT(DATE, '1876-01-01'), 'Action in 1876', 1 UNION ALL SELECT CONVERT(DATE, '1945-01-01'), 'Action in 1945', 1 UNION ALL SELECT CONVERT(DATE, '2000-01-01'), 'Chaos', 2 UNION ALL SELECT CONVERT(DATE, '2010-10-15'), 'Stare at wall', 3 UNION ALL SELECT CONVERT(DATE, '2010-10-16'), 'Unknown', 3 UNION ALL SELECT CONVERT(DATE, '2012-01-01'), '????', 4 UNION ALL SELECT CONVERT(DATE, '4712-01-01'), 'Faraway', 4
;
SELECT StartDate, Activity, CategoryID FROM ( SELECT StartDate, Activity, CategoryID, ROW_NUMBER() over (partition by CategoryID ORDER BY StartDate DESC)ORDERING FROM @tDUMMY ) tmp WHERE tmp.ORDERING = 1 ORDER BY 1
Result: 1945-01-01 Action in 1945 1 2000-01-01 Chaos 2 2010-10-16 Unknown 3 4712-01-01 Faraway 4
|
|
|
|