CREATE TABLE [dbo].[tbl_currencies](
[currency_id] [int] IDENTITY(1,1) NOT NULL,
[currency] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_tbl_currencies] PRIMARY KEY CLUSTERED
(
[currency_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
currency_id currency
1 LBP
2 USD
3 EUR
CREATE TABLE [dbo].[tbl_rate](
[rate_id] [int] IDENTITY(1,1) NOT NULL,
[currency_id] [int] NULL,
[currency] [nvarchar](50) NULL,
[time_stamp] [date] NULL,
[rate] [decimal](18, 4) NULL
) ON [PRIMARY]
GO
rate_id currency_id currency time_stamp rate
1 1 LBP 2018-01-01 1.0000
2 2 USD 2018-01-01 1500.0000
3 1 LBP 2019-01-01 1.0000
4 2 USD 2019-01-01 1057.5000
What I have tried:
Select c.currency_id,c.currency,r.rate,Max(r.time_stamp) from tbl_currencies c
inner join tbl_rate r on c.currency_id=r.currency_id
group by c.currency_id,c.currency,r.rate
currency_id currency rate (No column name)
1 LBP 1.0000 2019-01-01
2 USD 1057.5000 2019-01-01
2 USD 1500.0000 2018-01-01
Ther Correct Result that must i retrive is
currency_id currency rate (No column name)
1 LBP 1.0000 2019-01-01
2 USD 1057.5000 2019-01-01