thank you in advance for any assistance you can provide.
I am having to work in Oracle SQL and one issue I'm coming across is how to use applies in SQL. Specifically, hoping someone can help me write the code below to work in SQL. It's not a great example but I'm trying to figure out if there is a good way to:
- do implicit applies, referencing columns from the "from" clause table, so they can be used in future applies/joins in the same query without having to do multiple temp tables, CTEs, or inner queries.
- same question using top 1 in outer applies
Example:
--Drop Table #Users Drop Table #UsersLoginAttempts
Create table #Users(Username varchar(10), Password1 int)
Create Table #UsersLoginAttempts (Username varchar(10), Password1 int, AttemptDate date)
insert into #Users Values(' MAllen',123), ('SEllis ',124), ('MChen ',126)
insert into #UsersLoginAttempts Values
('MAllen ',123, '20221001'), (' SEllis ',124, '20221001')
, (' MChen ',126, '20221001'), ('MAllen ',126, '20221008')
, (' SEllis ',123, '20221008'), (' MChen',128, '20221008')
Select t.*, u.*
from #Users t
outer apply ( select ltrim(rtrim(t.username)) as UserNameTrim) unt
Outer apply ( select top 1 * from #UsersLoginAttempts ula
where ltrim(rtrim(ula.UserName)) = unt.UserNameTrim
order by ula.AttemptDate desc) u