英文:
How can I use the TSQL commands Top and IN together in SQL Server?
问题
以下是翻译好的内容:
示例代码如下:
SELECT top(3) * FROM AcceptorRequests
WHERE Terminal IN ('70270191','70347395')
ORDER BY Id DESC
但它只适用于一个终端号码。我想为每个终端显示三个最终记录。
英文:
The sample code that I am referring to is as follows:
SELECT top(3) * FROM AcceptorRequests
WHERE Terminal IN ('70270191','70347395')
ORDER BY Id DESC
But it works for only one terminal number. I want to display three end records for each terminal.
答案1
得分: 0
;with cte
as
(
SELECT *,row_number() over (partition by terminal order by id desc) as rownum FROM AcceptorRequests
WHERE Terminal IN ('70270191','70347395')
)
select * from cte where rownum<=3
英文:
;with cte
as
(
SELECT *,row_number() over (partition by terminal order by id desc) as rownum FROM AcceptorRequests
WHERE Terminal IN ('70270191','70347395')
)
select * from cte where rownum<=3
答案2
得分: -1
你可以使用 union all
:
SELECT top(3) *
FROM AcceptorRequests
WHERE Terminal = '70270191'
UNION ALL
SELECT top(3) *
FROM AcceptorRequests
WHERE Terminal = '70347395'
ORDER BY Id DESC
英文:
You could use union all
:
SELECT top(3) *
FROM AcceptorRequests
WHERE Terminal = '70270191'
UNION ALL
SELECT top(3) *
FROM AcceptorRequests
WHERE Terminal = '70347395'
order by Id desc
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论