如何从SQL Server中的重复行中获取最后一行?

问题描述 投票:1回答:3

我有一个SQL查询来检索以前的日期记录。我想从重复行中选择最后一条记录,我该怎么做? 我的查询如下。

Declare @previous datetime
Set @previous = (select dateadd(day, -1, '20180216'))
select  MstSmartItemLedger.smartitemid,MstSmartItemLedger.ItemCode,Balanceqty 
from MstSmartItemLedger 
where (CONVERT(Nvarchar, MstSmartItemLedger.SDate, 112) = @previous)

我得到了这样的结果

smartitemid itemcode balanceqty
    802       1141    -3
    803       118     -13
    804       1110    -24
    805       112     -21
    806       115    -24
    807       11141   -5
    808       1127    -21
    809       1129     -4
    810       11129   -181
    811       1139    -179
    812       1134     -32
    813       11103     -3
    814       1199      -6
    815       11102    -7
    816       11129    -183
    817       1188     -18
    818       1189      -11
    819       1139     -180
    820       117      -43
    821       114      -34
    822       1155     -20
    823       11140    -58
    824       1188     -22
    825       1188     -22
    826       1111     -11

如上面的结果有两行商品代码11129所以我想要smartitemid 816的最后一条记录。我想要的结果如下

smartitemid itemcode balanceqty
        802       1141    -3
        803       118     -13
        804       1110    -24
        805       112     -21
        806       115    -24
        807       11141   -5
        808       1127    -21
        809       1129     -4
        812       1134     -32
        813       11103     -3
        814       1199      -6
        815       11102    -7
        816       11129    -183
        818       1189      -11
        819       1139     -180
        820       117      -43
        821       114      -34
        822       1155     -20
        823       11140    -58
        825       1188     -22
        826       1111     -11

我怎样才能得到这个结果?请帮忙

sql sql-server sql-server-2008
3个回答
1
投票

使用GROUP BY子句与max()..min()聚合函数

SELECT
       MAX(smartitemid) smartitemid, itemcode, MIN(balanceqty) balanceqty
FROM MstSmartItemLedger m
WHERE CONVERT(varchar, SDate, 112) = @previous
GROUP BY itemcode
ORDER BY 1

可能查找表也很有用

SELECT m.* 
FROM MstSmartItemLedger m
INNER JOIN (
    SELECT MAX(smartitemid) smartitemid 
    FROM MstSmartItemLedger
    WHERE CONVERT(varchar, SDate, 112) = @previous 
    GROUP BY itemcode
)a ON a.smartitemid = m.smartitemid

1
投票

试试这个:

Declare @previous datetime
Set @previous = (select dateadd(day, -1, '20180216'))

SELECT  MstSmartItemLedger.smartitemid,MstSmartItemLedger.ItemCode,Balanceqty 
FROM MstSmartItemLedger
WHERE MstSmartItemLedger.smartitemid IN(
    select  MAX(MstSmartItemLedger.smartitemid)
    from MstSmartItemLedger 
        where (CONVERT(Nvarchar, MstSmartItemLedger.SDate, 112) = @previous)
    GROUP BY MstSmartItemLedger.ItemCode
    )

0
投票

如果您将其分组到商品代码上并使用max smartitemid,您应该根据需要获取列表

Declare @previous datetime
Set @previous = (select dateadd(day, -1, '20180216'))
select  MAX(MstSmartItemLedger.smartitemid) smartitemid, MstSmartItemLedger.ItemCode, Balanceqty 
from MstSmartItemLedger 
where (CONVERT(Nvarchar, MstSmartItemLedger.SDate, 112) = @previous)
GROUP BY MstSmartItemLedger.ItemCode, Balanceqty
© www.soinside.com 2019 - 2024. All rights reserved.