gpt4 book ai didi

sql - 选择具有 MAX(Date) 的记录但没有获得不同的返回

转载 作者:行者123 更新时间:2023-12-02 00:06:45 26 4
gpt4 key购买 nike

我正在加入表格,我的代码如下所示:

SELECT P.*
FROM (
SELECT ABC.StockCode
,ABC.Supplier
,ABC.Price
,MAX(ABC.OrigReceiptDate) ReceiptDate
FROM (
SELECT GRND.StockCode
,GRND.Supplier
,OrigReceiptDate
,(GRND.OrigGrnValue / GRND.QtyReceived) AS Price
FROM [SysproCompanyR].[dbo].[GrnDetails] GRND
WHERE QtyReceived > 0
) ABC
GROUP BY ABC.StockCode
,ABC.Supplier
,ABC.Price
) P
WHERE P.StockCode IN (
SELECT StockCode
FROM [SysproCompanyR].[dbo].[InvMaster]
)
ORDER BY P.StockCode
,P.Price

但是结果集是:

enter image description here

我的问题是我需要获取最新购买的股票代码及其价格...但由于价格不同,我得到了重复值以帮助金发女郎>

最佳答案

试试这个:

WITH CTE
AS
(
SELECT
ABC.StockCode,
ABC.Supplier,
ABC.Price,
ABC.OrigReceiptDate AS ReceiptDate,
ROW_NUMBER() OVER(PARTITION BY ABC.Supplier
ORDER BY ABC.OrigReceiptDate DESC) AS RN
FROM
(
SELECT GRND.StockCode, GRND.Supplier,OrigReceiptDate,
(GRND.OrigGrnValue /GRND.QtyReceived) as Price
FROM [SysproCompanyR].[dbo].[GrnDetails] GRND
Where QtyReceived > 0
) AS ABC
INNER JOIN [SysproCompanyR].[dbo].[InvMaster] AS m
ON ABC.StockCode = m.StockCode
)
SELECT
StockCode,
Supplier,
Price,
ReceiptDate
FROM CTE
WHERE RN = 1;

这应该会为您提供每个供应商最新购买的股票代码及其价格。

关于sql - 选择具有 MAX(Date) 的记录但没有获得不同的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17919045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com