我们如何使用ms sql查询获取表中的最后2个交易数据 [英] How we get last 2 transactions data in table using ms sql query

查看:110
本文介绍了我们如何使用ms sql查询获取表中的最后2个交易数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个表``tbl_student''字段是(名称,类别,年龄,地址)确定.

我的表中没有ID列(主键和身份正常).
我输入了记录,

名称类别年龄地址
------ ------ ----- --------
Mohan X 18 Erode
Kumar XII 20钦奈----& gt; 1
阿伦XI 19 Trichy
Bala VII 17 Coimbatore ----& gt; 2
Vijay VIII 17钦奈
Karthi X 19 Erode


我终于存储了"Kumar"和"Bala"详细信息(1,2).
然后...

我如何从sql查询中获取这些最后2个事务
任何一个已知的plz都告诉我....

来自Mohan.

Hi everyOne,



I have One Table ''tbl_student'' fields are (Name,Class,Age,Address) OK.

No id column(primary key and identity ok)in my table.
I entered records,

Name Class Age Address
------ ------ ----- --------
Mohan X 18 Erode
Kumar XII 20 Chennai ---->1
Arun XI 19 Trichy
Bala VII 17 Coimbatore ---->2
Vijay VIII 17 Chennai
Karthi X 19 Erode


I stored ''Kumar'' and ''Bala'' Details(1,2) finally ok.
then...

How I get these Last 2 TRANSACTIONS from sql query
any One Known plz tell me....

By from Mohan.

推荐答案

最近2次来自SQL查询的事务
听起来好像您正在查看基于时间的记录.为了拥有它,在表"LastUpdated"中再增加一列.在插入/更新时填充它.将这些数据存储在表中后,就可以在需要时获取基于第一个,最后一个或时间范围的数据.
Last 2 TRANSACTIONS from sql query
Sounds like you are looking at time based records. In order to have it, add one more column to the table ''LastUpdated''. Populate it at the time of insertion/updation. Once you have this data in the table, you can get the first, last or time range based data whenever needed.


您无法基于事务检索数据.您可以检索row_number(),但这只会告诉您插入的记录,不一定是最后一个事务,因为一个事务可能是更新.考虑到这一限制,执行此操作的方法将是使用
You can''t retrieve data based on the transactions. You could retrieve the row_number(), but this would only tell you the records inserted, which is not necessarily the last transactions because one transaction might be an update. With that constraint in mind, the way to do this would be to use something like
select top 2 row_number() over (order by Name desc) as 'RowNum', ...
from tbl_student
group by Name
order by Name desc

请注意,我还没有测试过它,它只是在编辑器中输入的,因此您可能需要稍微调整一下语法.

Note that I haven''t tested this, it''s just been entered in the editor so you may have to tweak the syntax a little bit.


这篇关于我们如何使用ms sql查询获取表中的最后2个交易数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆