问题描述
亲爱的,
我有一张名为Trn_Purchase的桌子
字段是
ItemCode
ItemDesc
PurchaseDates
价格
我想得到最后一个购买价格取决于日期
表格如下数据
ItemCode PurchaseDate Price
A 01- Mar-2010 4
2011年3月2日6
结果应该是
itemCode PurchaseDate价格
2011年3月2日6
怎么做
谢谢
巴斯特。
我的尝试:
我在下面做了
Dear,
I have table called Trn_Purchase
fields are
ItemCode
ItemDesc
PurchaseDates
Price
I want to get the last purchase price depend on date
table have e.g below data
ItemCode PurchaseDate Price
A 01-Mar-2010 4
A 02-Mar-2011 6
then result should
itemCode PurchaseDate Price
A 02-Mar-2011 6
How to do this
Thanks
Basit.
What I have tried:
I did below
SELECT
p.*
FROM
Trn_Purchase p,
(SELECT * FROM Trn_Purchase) m
WHERE
p.Itemcode = m.Itemcode
and p.[Purchase Date] = m.[Purchase Date]
推荐答案
SELECT TOP 1 * FROM Trn_Purchase ORDER BY PurchaseDate DESC
是每个项目代码
如果你想要一个特定的输出,那么给出具体的例子,并准确解释你想要的东西:它意味着我们可以用特定的解决方案回复!
尝试:
"yes each item code"
If you want a specific output, then give specific examples, and explain exactly what you want: it means we can reply with a specific solution!
Try:
SELECT ItemCode, PurchaseDate, Price
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY ItemCode ORDER By PurchaseDate DESC) AS rn
FROM Trn_Purchase) x
WHERE rn = 1
这篇关于如何获得最后的购买价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!