Ask SQL SERVER to extract the latest records according to the specified conditions.

there is a system that has no source code and cannot find a developer. Now I want to make a report by myself, but I have encountered a disgusting problem. Consult
database SQL SERVER 2008 R2
the simplified table structure is as follows
id customer ID purchase date due time commodity ID unit price
1 1 2017-1 2018-3-1 3 12 2
2 1 2018-5-1 2019-6-13 13 3
3 3 2016-3-15 2017-2-18 14
4 3 2017-3-15 2018-1-18 15 5
5 3 2018-2-1 2018-6-1 9 16 6
6 3 2018-7-1 2018-6-9-17-7
7 4 2015-4 2016-12-2015 18 8
8 4 2016-12-21 2017-5-4 15 19
9 4 2017-5 -5 2017-12-31 15 1 10 10
10 4 2018-6-1 2020-1-1 15 1 11 11

clipboard.png

ID
:

id ID ID
2 1 2018-5-1 2019-6-1 3 1 3 3
4 3 2017-3-15 2018-1-1 8 1 5 5
6 3 2018-7-1 2018-6-1 9 -1 7 -7
10 4 2018-6-1 2020-1-1 15 1 11 11

clipboard.png


//:
// Table_Bill

,


id  |  ID  |    |    |  ID  |    |    |  

Id  |  CusId   |  BuyTime  |  DueTime  | ArticlId |  Num   | Price |  Money

,

SELECT a.*  FROM [Table_Bill] a,(SELECT MAX(BuyTime) AS BuyTime ,ArticlId FROM [Table_Bill] GROUP BY ArticlId) b

WHERE a.BuyTime=b.BuyTime AND a.ArticlId=b.ArticlId 

ORDER BY a.ArticlId

Menu