Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从SQL Server中的重复行中获取最后一行?_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

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

如何从SQL Server中的重复行中获取最后一行?,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个SQL查询来检索以前的日期记录。我想从重复行中选择最后一条记录,我如何才能做到这一点? 我的问题如下 Declare @previous datetime Set @previous = (select dateadd(day, -1, '20180216')) select MstSmartItemLedger.smartitemid,MstSmartItemLedger.ItemCode,Balanceqty from MstSmartItemLedger where (CON

我有一个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
正如上面的结果,有两行itemcode11129,所以我想要smartitemid816的最后一条记录。我希望结果如下

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

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

如果您在itemcode上对其进行分组并使用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
试试这个:

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
    )

groupby
子句与
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