[英]SQL Server join query to get row with latest date closest to another date
I have looked through a lot of pages and can't find quite what I want to achieve. 我浏览了很多页面,但找不到我想要实现的目标。 I have a select statement that I want to get the latest date that it is greater than so for example:
我有一条select语句,我想获取比它大的最新日期,例如:
If I have two dates of 2015/06/01, 2015/07/01 and in my table I have one record with the date 2015/05/01 and another with the date 2015/06/20. 如果我有两个日期为2015/06 / 01、2015 / 07/01,并且在我的表中,我有一个记录的日期为2015/05/01,而另一个记录的日期为2015/06/20。
For the 2015/06/01 I want to have the max date that it is greater than in this case would be 2015/05/01. 对于2015/06/01,我想要的最大日期大于此情况下的最大日期为2015/05/01。
For the 2015/07/01, I want to have the max date that it is greater than, in this case it would be 2015/06/20. 对于2015/07/01,我希望有一个大于的最大日期,在这种情况下,它将是2015/06/20。
I want only the one option to return not all the matches which is what I currently get. 我只希望一个选项不返回我当前得到的所有匹配项。
There are other joins etc to other tables and it would use all of them but for this purpose I have taken that information out because it is irrelevant to what I'm trying to achieve. 其他表还有其他联接等,它会使用所有这些联接,但出于这个目的,我将这些信息排除在外,因为它与我要实现的目标无关。 If you require any more information just comment.
如果您需要更多信息,请发表评论。
After comments I have created a Fiddle db structure, http://sqlfiddle.com/#!6/2738f/2/0 . 评论后,我创建了一个Fiddle db结构http://sqlfiddle.com/#!6/2738f/2/0 。
So here is my current query: 所以这是我当前的查询:
With IptBandRates as (
select
ibr.ipt_band_code,
ibr.ipt_rate,
ibr.concessionary_date,
max(ibr.concessionary_date) as concessionary_date2
from product.ipt_band_rates ibr
inner join quote.quote qu on '4' = qu.quote_id
where ibr.concessionary_date <= qu.[effective_date]
group by ibr.concessionary_date, ibr.ipt_rate, ipt_band_code
)
select
IptBandRates.ipt_rate as ipt_rate
from quote.premium
inner join quote.cover on quote.premium.cover_id = quote.cover.cover_id
inner join quote.quote qu on quote.cover.quote_id = qu.quote_id
inner join product.premium_class on
quote.cover.product_code = product.premium_class.product_code and
quote.cover.product_ver_no = product.premium_class.product_ver_no and
quote.cover.class_type_code = product.premium_class.class_type_code
inner join product.ipt_band on
product.premium_class.ipt_band_code = product.ipt_band.ipt_band_code
inner join IptBandRates on
product.ipt_band.ipt_band_code = IptBandRates.ipt_band_code
inner join product.ipt_band_rates ibrs on
ibrs.ipt_band_code = IptBandRates.ipt_band_code and
ibrs.concessionary_date = IptBandRates.concessionary_date
where qu.quote_id = '4'
do you need somethin like this? 您需要像这样的东西吗?
x is the table with your dates and y is only a reference to get dates of the 1st day of each month to extract the maximum date lesser than.. x是带有日期的表,而y只是获取每月小于1的最大日期的参考。
;with
x as (
select 1 id, '2015/05/01' d
union all
select 2 id, '2015/06/20' d
union all
select 3 id, '2015/06/15' d
union all
select 4 id, '2015/05/10' d
union all
select 5 id, '2015/07/02' d
union all
select 6 id, '2015/04/02' d
),
y as (
select '2015/05/01' ref_month
union all
select '2015/06/01'
union all
select '2015/07/01'
)
SELECT *
FROM y
outer apply (
select MAX(d) max_date
from x
where d < y.ref_month
) z
result will be: 结果将是:
ref_month max_date
2015/05/01 2015/04/02
2015/06/01 2015/05/10
2015/07/01 2015/06/20
I know this is old, but this is a way of doing it. 我知道这很古老,但这是一种解决方法。
IF OBJECT_ID('tempdb..#FilteringDates') IS NOT NULL
DROP TABLE #FilteringDates
CREATE TABLE #FilteringDates (filterDate DATE)
INSERT INTO #FilteringDates (
filterDate)
VALUES
('2018-01-01'),
('2018-02-15'),
('2018-03-20')
IF OBJECT_ID('tempdb..#DataDates') IS NOT NULL
DROP TABLE #DataDates
CREATE TABLE #DataDates (
dataDate DATE,
otherValue INT)
INSERT INTO #DataDates (
dataDate,
otherValue)
VALUES
('2017-06-01', 1),
('2018-01-03', 90),
('2018-01-15', 150),
('2018-01-20', 35),
('2018-02-20', 400),
('2018-03-10', 425),
('2018-04-20', 3)
;WITH DateRankings AS
(
SELECT
F.filterDate,
D.dataDate,
D.otherValue,
MostRecentRanking = ROW_NUMBER() OVER (
PARTITION BY
F.filterDate
ORDER BY
D.dataDate DESC)
FROM
#FilteringDates AS F
INNER JOIN #DataDates AS D ON D.dataDate <= F.filterDate
)
SELECT
D.filterDate,
MostRecentDate = D.dataDate,
D.otherValue
FROM
DateRankings AS D
WHERE
D.MostRecentRanking = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.