SQL 查询 - 合并两个表,删除重复项并仅按日期保留最新的
SQL Query - Combining Two tables, removing duplicates and only keeping most recent by date
我正在尝试在 SQLServer Management Studio 2008 中同时进行查询,我通过 'tax_id' 加入了两个 table,但我有来自 table 的重复条目2 (Tax_Rate_Table) 我只需要显示最近的一个条目 'effective_date' 如下所示,Tax_ID 4 有一个重复的条目:
1.TAX_TABLE---------
tax_id description
1 AZ State
2 AZ-Maricopa Co
4 AZ-Maricopa/Mesa
2.Tax_RATE_TABLE-------
tax_id effective_date tax_percent
1 2015-01-01 00:00:00.000 5.6
2 2015-01-01 00:00:00.000 0.7
4 2015-01-01 00:00:00.000 1.75
4 2019-03-01 00:00:00.000 2
我按生效日期加入和下降有效,但是,我正在尝试使用 "order by effective_date desc LIMIT 1;" 但是限制功能不起作用。
一个选项使用横向连接(并不是说 SQL 服务器不支持 limit
语法,而是使用 top
代替):
select t.*, tr.effective_date, tr.tax_percent
from tax_table t
cross apply (
select top (1) *
from tax_rate_table tr
where tr.tax_id = t.tax_id
order by tr.effective_date desc
) tr
您还可以使用相关子查询进行连接和过滤:
select t.*, tr.effective_date, tr.tax_percent
from tax_table t
inner join tax_rate_table tr on tr.tax_id = t.tax_id
where tr.effective_date = (
select max(tr1.effective_date)
from tax_rate_table tr1
where tr1.tax_id = tr.tax_id
)
或者您可以使用 row_number()
:
select *
from (
select
t.*,
tr.effective_date,
tr.tax_percent,
row_number() over(partition by t.tax_id order by tr.effective_date desc) rn
from tax_table t
inner join tax_rate_table tr on tr.tax_id = t.tax_id
) t
where rn = 1
;with rates
AS
(
SELECT *
,ROW_NUMBER() OVER (PARTITION BY tax_id ORDER BY effective_date desc) as pID
FROM tax_table t
inner join tax_rate_table r on t.ID = r.tax_id
)
select
tax_id
,DESCRIPTION
,effective_date
,percentage
FROM rates
WHERE pid = 1
我正在尝试在 SQLServer Management Studio 2008 中同时进行查询,我通过 'tax_id' 加入了两个 table,但我有来自 table 的重复条目2 (Tax_Rate_Table) 我只需要显示最近的一个条目 'effective_date' 如下所示,Tax_ID 4 有一个重复的条目:
1.TAX_TABLE---------
tax_id description
1 AZ State
2 AZ-Maricopa Co
4 AZ-Maricopa/Mesa
2.Tax_RATE_TABLE-------
tax_id effective_date tax_percent
1 2015-01-01 00:00:00.000 5.6
2 2015-01-01 00:00:00.000 0.7
4 2015-01-01 00:00:00.000 1.75
4 2019-03-01 00:00:00.000 2
我按生效日期加入和下降有效,但是,我正在尝试使用 "order by effective_date desc LIMIT 1;" 但是限制功能不起作用。
一个选项使用横向连接(并不是说 SQL 服务器不支持 limit
语法,而是使用 top
代替):
select t.*, tr.effective_date, tr.tax_percent
from tax_table t
cross apply (
select top (1) *
from tax_rate_table tr
where tr.tax_id = t.tax_id
order by tr.effective_date desc
) tr
您还可以使用相关子查询进行连接和过滤:
select t.*, tr.effective_date, tr.tax_percent
from tax_table t
inner join tax_rate_table tr on tr.tax_id = t.tax_id
where tr.effective_date = (
select max(tr1.effective_date)
from tax_rate_table tr1
where tr1.tax_id = tr.tax_id
)
或者您可以使用 row_number()
:
select *
from (
select
t.*,
tr.effective_date,
tr.tax_percent,
row_number() over(partition by t.tax_id order by tr.effective_date desc) rn
from tax_table t
inner join tax_rate_table tr on tr.tax_id = t.tax_id
) t
where rn = 1
;with rates
AS
(
SELECT *
,ROW_NUMBER() OVER (PARTITION BY tax_id ORDER BY effective_date desc) as pID
FROM tax_table t
inner join tax_rate_table r on t.ID = r.tax_id
)
select
tax_id
,DESCRIPTION
,effective_date
,percentage
FROM rates
WHERE pid = 1