我正在研究我的数据库项目。我做了一个显示客人信息的视图。如您所见,Frank Collins有两行。如何使用SELECT作为视图,只显示他的一行(最近的逗留日期)?
答案 0 :(得分:2)
我认为这是最简单的解决方案......:
select top 1 * from MY_VIEW
where lastname = 'Collins' and firstname = 'Frank'
order by dateto desc
或更多食用:
select top 1 * from MY_VIEW
where lastname + ' ' + firstname = 'Collins Frank'
order by dateto desc
答案 1 :(得分:0)
试试这个
WITH CTE
AS
(
SELECT
SeqNo = ROW_NUMBER() OVER(PARTITION BY LastName,FirstName ORDER BY DateTo DESC),
*
FROM Guests
)
SELECT
*
FROM CTE
WHERE SeqNo = 1
此查询将返回每个用户的最新记录,如果有第一个名称+姓氏组合的多个记录,它将根据dateTo为您提供最新记录,否则它将返回记录
因此,每次执行此操作时,每个访客只能获得一行
答案 2 :(得分:0)
尝试:
const mapStateToProps = (state) => {
return { userData: state.userData };
};
答案 3 :(得分:0)
您不需要CTE,只需使用TOP WITH TIES
+ ROW_NUMBER()
SELECT TOP 1 WITH TIES *
FROM guests
ORDER BY ROW_NUMBER() OVER(PARTITION BY lastname, firstname ORDER BY dateto DESC)