开发者

Select DISTINCT items rows and associated columns based on a condition

开发者 https://www.devze.com 2023-01-09 21:26 出处:网络
Let\'s say I have a table like this in my DB DateItemIdCount 7/29/201031 7/30/201032 7/31/201023 7/29/201034

Let's say I have a table like this in my DB

   Date   ItemId  Count
7/29/2010   3   1
7/30/2010   3   2
7/31/2010   2   3
7/29/2010   3   4
7/30/2010   1   5
7/31/2010   1   6
7/29/2010   2   7
7/30/2010   3   8
7/31/2010   1   2

For each item type, I want to select the count on the latest date found in the table only.

Is there an easy way to do this?

I'm thinking of doing something with DISTINCT to get the u开发者_StackOverflownique ItemIDs, but I'm stuck from there.


  SELECT ItemId, Count
  FROM
  ( SELECT Date, ItemId, Count, RANK() OVER (partition by ItemId, Order by ItemId, Date Desc) r
    FROM Table
  ) 
  WHERE r = 1
0

精彩评论

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