I have these two datasets
t5 and t6
I want to find the tax_id with the latest date
So I have used UNION instead of join
Select * from (
Select s_id, tax_id, date_of,
row_number() over(partition by s_id order by date_of) row_num
FROM
(Select * from t5
UNION
Select * from t6) s1) s2
There is a value 2 with only have the same txn_date
I also want to take that as the latest one
EDIT : Thanks for your help
I am using SQL Server
I want the output as below.
4条答案
按热度按时间oxiaedzo1#
Generate dataset using
UNION ALL
then applyrow_number()
on it to generate row numbers by descendant order and pick the ones withrn = 1
:vecaoik12#
Use rownum = 1 for getting one data from list in Oracle SQL
bksxznpy3#
Select everything from two tables with
UNION ALL
and then choose the latest date byLIMIT 1
It would work for PostgreSQL, but I guess, for other DBMS it might work too.
jfgube3f4#
To find the latest date for each
tax_id
you could use theGROUP BY
clause withMAX
aggregate function.For this query to work in the majority of DBMS,
date_of
has to be defined as a datetime type.