Sql server 尝试获取产品的最后一个事务时会出现重复值
如何避免在尝试从表中获取产品的最后一个事务时出现重复值。我的查询如下,我的图像如下 可能是这样的:Sql server 尝试获取产品的最后一个事务时会出现重复值,sql-server,sql-server-2008,sql-server-2012,Sql Server,Sql Server 2008,Sql Server 2012,如何避免在尝试从表中获取产品的最后一个事务时出现重复值。我的查询如下,我的图像如下 可能是这样的: with cte as ( select rn = row_number() over ( partition by a.ProductID order by a.TransactDateTime desc), a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a
with cte as (
select
rn = row_number() over ( partition by a.ProductID order by a.TransactDateTime desc),
a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy
from
ALX_SubInventoryCashTransfers a
)
select
a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy
from
cte a
where
( a.rn = 1 )
可能是这样的:
with cte as (
select
rn = row_number() over ( partition by a.ProductID order by a.TransactDateTime desc),
a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy
from
ALX_SubInventoryCashTransfers a
)
select
a.branchid, a.TellerID, a.ProductID, a.TransactDateTime, a.ProductStock, a.ProductStockInLocalCrrncy
from
cte a
where
( a.rn = 1 )
按从组中删除Transact-DateTime
from ALX_SubInventoryCashTransfers group by
branchid,TellerID,ProductID,**TransactDateTime**) tm
您可以尝试此查询
SELECT
a.branchid,
a.TellerID,
a.ProductID,
a.TransactDateTime,
a.ProductStock,
a.ProductStockInLocalCrrncy
FROM ALX_SubInventoryCashTransfers a
INNER JOIN (SELECT
branchid,
TellerID,
ProductID,
MAX(TransactDateTime) as MaxDate
FROM ALX_SubInventoryCashTransfers
GROUP BY branchid,
TellerID,
ProductID) tm
ON a.BranchID = tm.BranchID
AND a.branchid = tm.BranchID
AND a.TellerID = tm.TellerID
AND a.ProductID = tm.ProductID
AND a.TransactDateTime = tm.MaxDate
按从组中删除Transact-DateTime
from ALX_SubInventoryCashTransfers group by
branchid,TellerID,ProductID,**TransactDateTime**) tm
您可以尝试此查询
SELECT
a.branchid,
a.TellerID,
a.ProductID,
a.TransactDateTime,
a.ProductStock,
a.ProductStockInLocalCrrncy
FROM ALX_SubInventoryCashTransfers a
INNER JOIN (SELECT
branchid,
TellerID,
ProductID,
MAX(TransactDateTime) as MaxDate
FROM ALX_SubInventoryCashTransfers
GROUP BY branchid,
TellerID,
ProductID) tm
ON a.BranchID = tm.BranchID
AND a.branchid = tm.BranchID
AND a.TellerID = tm.TellerID
AND a.ProductID = tm.ProductID
AND a.TransactDateTime = tm.MaxDate
如果我没有弄错你的问题,你希望每个日期的最大日期时间。请尝试以下查询:
SELECT
a.branchid,
a.TellerID,
a.ProductID,
a.TransactDateTime,
a.ProductStock,
a.ProductStockInLocalCrrncy
FROM ALX_SubInventoryCashTransfers a
INNER JOIN (
SELECT
branchid,
TellerID,
ProductID,
MAX(TransactDateTime) datetime
FROM ALX_SubInventoryCashTransfers
GROUP BY branchid,
TellerID,
ProductID,
CAST(TransactDateTime AS DATE)
) tm
ON a.BranchID = tm.BranchID
AND a.branchid = tm.BranchID
AND a.TellerID = tm.TellerID
AND a.ProductID = tm.ProductID
AND a.TransactDateTime = tm.datetime
如果我没有弄错你的问题,你希望每个日期的最大日期时间。请尝试以下查询:
SELECT
a.branchid,
a.TellerID,
a.ProductID,
a.TransactDateTime,
a.ProductStock,
a.ProductStockInLocalCrrncy
FROM ALX_SubInventoryCashTransfers a
INNER JOIN (
SELECT
branchid,
TellerID,
ProductID,
MAX(TransactDateTime) datetime
FROM ALX_SubInventoryCashTransfers
GROUP BY branchid,
TellerID,
ProductID,
CAST(TransactDateTime AS DATE)
) tm
ON a.BranchID = tm.BranchID
AND a.branchid = tm.BranchID
AND a.TellerID = tm.TellerID
AND a.ProductID = tm.ProductID
AND a.TransactDateTime = tm.datetime
你能给我们看一下包含副本的真实输出吗?您包含的屏幕截图显示了多个
datetime
值,这在您的查询中是不可能的,因为您限制了某个表中具有最大值的记录。请尝试从group by子句中删除TransactionDateTime字段。@tim,这是重复的。我只需要一个日期一个产品价值。你可以在pic中看到产品ID 2在同一时间重复两次,比如说,我一年只需要一次day@vicky是的,我也做了同样的事,我只取了日期,并展示了同样的东西occur@vicky我需要显示每个产品的日期。您能给我们显示包含重复项的实际输出吗?您包含的屏幕截图显示了多个datetime
值,这在您的查询中是不可能的,因为您限制了某个表中具有最大值的记录。请尝试从group by子句中删除TransactionDateTime字段。@tim,这是重复的。我只需要一个日期一个产品价值。你可以在pic中看到产品ID 2在同一时间重复两次,比如说,我一年只需要一次day@vicky是的,我也做了同样的事,我只取了日期,并展示了同样的东西occur@vicky我需要为每个产品显示日期我需要根据需要显示的日期显示日期(每个产品的最后一个值应在每天显示)我是说只从组中删除。。您可以根据我需要显示的日期(每个产品的最后一个值应在每天显示)使用子查询的maxdate i need date。我说的是仅从group by中删除。。您可以使用子查询中的maxdate。如果cte显示不正确,则类似于页面显示。如果cte显示不正确,则类似于页面显示