最近日期对应问题,求指点
TableA
bookid bookname
1 name1
2 name2
3 name3
4 name4
TableB
id dates sumNumber
1 2013-07-01 2
2 2013-07-01 1
3 2013-07-02 3
4 2013-07-04 2
TableC
id bid bookid values
1 1 1 0.4
2 1 2 0.3
3 2 3 0.5
4 3 1 0.4
5 3 2 0.4
6 3 3 0.6
7 4 1 0.4
8 4 2 0.45
A表是商品表,B表相当于订单主单C表是详细记录
求最近日期销售的商品A对应的values,不要游标。
结果:
bookid bookname dates values
1 name1 2013-07-04 0.4
2 name2 2013-07-04 0.45
3 name1 2013-07-02 0.6
请高手指点。
------解决方案--------------------SELECT
A.bookID,
A.bookName,
T.Dates,
T.[Values]
FROM tableA A
CROSS APPLY
(
SELECT TOP(1) B.dates,C.bookid,C.[Values],C.bid
FROM tableC C
INNER JOIN tableB B
ON C.bid = B.id
WHERE C.bookid = A.bookID
ORDER BY B.dates DESC, C.id DESC
) T
------解决方案--------------------
Declare @TableA table( boodid int, bookName varchar(50))
insert @TableA