1
select  first(orderid), accountid 
from [Order] 
group by AccountId 
order by DateCreated desc

first() is invalid function.

max() does not work for unique identifiers

How would I get the last orderid created for all accounts? Thanks.

3
  • add a row_number() and pick where rownum = 1 (since already sorted by date desc) May 26, 2011 at 9:12
  • Please take out some time to go through my answer as well... :)
    – Pankaj
    May 26, 2011 at 10:04
  • I hope you are not using the same query multiple times. For that reason sub Query should be also a good option to choose.
    – Pankaj
    May 26, 2011 at 10:13

3 Answers 3

1

Something like (untested):

;WITH CTE_LatestOrders AS (
    select accountid, lastcreated = max(datecreated)
    from [Order]
    group by accountid
)
select
    accountid, orderid
from
    [Orders] o
    join CTE_LatestOrders l 
        on o.AccountID = l.AccountID 
        and o.datecreated = l.lastcreated
6
  • No worries. CTE tables are great for this sort of thing as they make it really clear what's going on. May 26, 2011 at 9:19
  • CTE? You answered so quickly, I cant yet mark it as correct ;) there is a time limit. Will do in a sec.
    – Konrad
    May 26, 2011 at 9:24
  • CTE = Common Table Expression. A way of creating a temporary table to use in your main query, which is in scope for only that query. Often makes messy queries far simpler. Its worth googling if you've not come across them before. May 26, 2011 at 9:26
  • A CTE is not a temp/UDT table; think of a CTE as a view that is defined only for your current query. Just like a view, a CTE is expanded and folded into the overall query plan. Global optimization will still occur, but do not think that just because you use a CTE you will only execute the query once. Here is a trivial example that fits in this space: WITH vw AS ( SELECT COUNT(*) c FROM Person ) SELECT a.c, b.c FROM vw a, vw b; The query plan will clearly show two scans/aggregations and a join instead of just projecting the same result twice.
    – Pankaj
    May 26, 2011 at 10:08
  • @SQL: Fair enough. Was being over simplistic, but point taken. May 26, 2011 at 10:12
1

Max() does work for unique identifiers as of MS SQL 2012

0

You can proceed with below as well.

Select Temp.orderid, T.AccountId, T.DateCreated
From
(
    Select AccountId, max(DateCreated) as  DateCreated
    From [Order]
    Group By AccountId
)T
Inner Join [Order] Temp on Temp.AccountId = T.AccountId 
AND Temp.DateCreated = T.DateCreated

A CTE is not a UDT/temp table; think of a CTE as a view that is defined only for your current query. Just like a view, a CTE is expanded and folded into the overall query plan. Global optimization will still occur, but do not think that just because you use a CTE you will only execute the query once. Here is a trivial example that fits in this space: WITH vw AS ( SELECT COUNT(*) c FROM Person ) SELECT a.c, b.c FROM vw a, vw b; The query plan will clearly show two scans/aggregations and a join instead of just projecting the same result twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.