Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
steve_joecool
Starting Member
21 Posts |
Posted - 2014-06-09 : 19:35:33
|
I have a question(if it’s not too much to ask) about a specific query. Here’s my query select tsk.id as "Task ID",tsk.AccountId,acc.name,MAX(CONVERT(DATE, tsk.LastModifiedDate, 131))as "Last modified date", tsk.Subject,usr.Aliasfrom task as tsk join Account as acc on acc.Id = tsk.AccountIdjoin users as usr on tsk.LastModifiedById = usr.Idwhere tsk.AccountId = '001i000000a1D3aAAE'AND acc.OwnerId = tsk.LastModifiedByIdgroup by tsk.AccountId, acc.Name, tsk.LastModifiedDate, tsk.Subject, usr.Alias, tsk.idorder by 4 descI get 4 rows of information in returnTask ID AccountId name Last modified date Subject Alias00Ti000000eZTbAEAW 001i000000a1D3aAAE HERSHEY FOODS CORPORATION - WEST HERSHEY 5/15/2014 Call bnest00Ti000000QAoe6EAD 001i000000a1D3aAAE HERSHEY FOODS CORPORATION - WEST HERSHEY 1/13/2014 Email: Line 3 Project bnest00Ti000000QAp31EAD 001i000000a1D3aAAE HERSHEY FOODS CORPORATION - WEST HERSHEY 1/13/2014 Email: RE: Perrigo bnest00Ti000000QApTEEA1 001i000000a1D3aAAE HERSHEY FOODS CORPORATION - WEST HERSHEY 1/13/2014 Email: RE: Line 3 Project bnestIs there any way I can filter that so I show ONLY the latest date on that list? I thought the MAX function would do that, but obviously is not doing what I tried. Any suggestions? |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-09 : 19:37:30
|
select top 1 tsk.id as "Task ID",tsk.AccountId,acc.name,MAX(CONVERT(DATE, tsk.LastModifiedDate, 131))as "Last modified date", tsk.Subject,usr.Aliasfrom task as tsk join Account as acc on acc.Id = tsk.AccountIdjoin users as usr on tsk.LastModifiedById = usr.Idwhere tsk.AccountId = '001i000000a1D3aAAE'AND acc.OwnerId = tsk.LastModifiedByIdgroup by tsk.AccountId, acc.Name, tsk.LastModifiedDate, tsk.Subject, usr.Alias, tsk.idorder by 4 descTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
steve_joecool
Starting Member
21 Posts |
Posted - 2014-06-09 : 19:40:58
|
quote: Originally posted by tkizer select top 1 tsk.id as "Task ID",tsk.AccountId,acc.name,MAX(CONVERT(DATE, tsk.LastModifiedDate, 131))as "Last modified date", tsk.Subject,usr.Aliasfrom task as tsk join Account as acc on acc.Id = tsk.AccountIdjoin users as usr on tsk.LastModifiedById = usr.Idwhere tsk.AccountId = '001i000000a1D3aAAE'AND acc.OwnerId = tsk.LastModifiedByIdgroup by tsk.AccountId, acc.Name, tsk.LastModifiedDate, tsk.Subject, usr.Alias, tsk.idorder by 4 descTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/
Thank you! now, if I take the constraint of the accountID (meaning I will query all accounts). I need to show the last activity for EVERY account. I don't know if this will only show the first record.... am I right? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-10 : 11:50:04
|
You can use the ROW_NUMBER() function to achieve that.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
|
|
|
|
|