admin管理员组文章数量:1125588
I am trying to select the maximum ImportDate for each person. For this person "Bob James" he should have the last 2 rows selected with ID 267311 and 267342. I need to see every column in the table but just the maximum importdate row or rows if they have more than one file that came in on the same date. I tried to create subquery and tie it back to a larger query but when I try to use max its only bringing back one row with 11-01-2024 importdate.
select MAX(importdate),
firstname, lastname
from ClmEligibility_Intake
where lastname = 'james'
and firstname = 'bob'
group by firstname, lastname
Also tried this subquery, not working. It should only bring back 2 IDs:
I am trying to select the maximum ImportDate for each person. For this person "Bob James" he should have the last 2 rows selected with ID 267311 and 267342. I need to see every column in the table but just the maximum importdate row or rows if they have more than one file that came in on the same date. I tried to create subquery and tie it back to a larger query but when I try to use max its only bringing back one row with 11-01-2024 importdate.
select MAX(importdate),
firstname, lastname
from ClmEligibility_Intake
where lastname = 'james'
and firstname = 'bob'
group by firstname, lastname
Also tried this subquery, not working. It should only bring back 2 IDs:
Share Improve this question edited yesterday jarlh 44.7k8 gold badges50 silver badges67 bronze badges asked 2 days ago Austin Austin 455 bronze badges 9 | Show 4 more comments2 Answers
Reset to default 2Easy way:
--get all the max(importdate)s per person in a CTE
;with CTE as (
select maxdate = MAX(importdate)
, firstname
, lastname
from ClmEligibility_Intake
group by firstname, lastname
)
--just select all the rows that have the same importdate as max(importdate)
select *
from ClmEligibility_Intake EI
inner join CTE on CTE.lastname = EI.lastname
and CTE.firstname = EI.firstname
and CTE.maxdate = importdate
Better way:
select *
from ClmEligibility_Intake EI
--self-join rows where a greater importdate exists:
left join ClmEligibility_Intake X on X.lastname = EI.lastname
and X.firstname = EI.firstname
and X.importdate > EI.importdate
where X.Id is null --only show rows for which no greater importdate was found
Same thing but you may find this more idiomatic:
select *
from ClmEligibility_Intake EI
where not exists (select *
from ClmEligibility_Intake X
where X.lastname = EI.lastname
and X.firstname = EI.firstname
and X.importdate > EI.importdate)
I call the table alias X
to indicate it’s only pulled in to be thrown away.
If you insist on window functions:
select *
from (select *, YeOldeRank = rank() over (partition by lastname, lastname order by importdate desc)
from ClmEligibility_Intake) EI
where YeOldeRank = 1
I think this will help you :
SELECT MAX(importdate),
firstname, lastname,
ROW_NUMBER() OVER(PARTITION BY firstname, lastname ORDER BY ID DESC) AS N
FROM ClmEligibility_Intake
WHERE lastname = 'james'
AND firstname = 'bob'
GROUP BY firstname, lastname
ORDER BY N OFFSET 0 ROWS FECTH NEXT 2 ROWS ONLY
本文标签:
版权声明:本文标题:subquery - Using SQL , how do I select the max values when there is more than one with the same max value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736637325a1945912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
RANK()
windows function. – Isolated Commented 2 days ago