帐户语句的SQL Server查询

时间:2017-12-04 10:41:18

标签: sql-server sql-server-2012

如何从下表生成最后三个交易?

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [ 

{
    "type": "node",
    "request": "launch",
    "name": "Launch Program",
    "program": "${workspaceFolder}/yourjsfile.js"
},
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${file}",
        "outFiles": [
            "${workspaceFolder}/**/*.js"
        ]
    }
]
}

我正在使用SQL Server 2012

2 个答案:

答案 0 :(得分:0)

像这样你应该得到预期的结果:

SELECT * FROM
(
   SELECT TOP 3 *
   FROM TransactionTable
   ORDER BY [Date] DESC
) AS t
ORDER by t.[Date]

答案 1 :(得分:-1)

如果您的要求是使用最新日期获得3笔交易。你可以使用以下任何一种。

简单订购:

select
top 3 
* from YourTable
where isnull(Tran,'')<>''
order by [Date] desc

使用行号

;with cte
as
(
  select
   seqno = row_number() over(order by [date] desc),
  *
  from YourTable
where isnull(Tran,'')<>''
 )
 select
 * from cte
 where SeqNo <=3
order by SeqNo desc