簡體   English   中英

SQL Server聯接查詢以獲取具有最接近另一個日期的最新日期的行

[英]SQL Server join query to get row with latest date closest to another date

我瀏覽了很多頁面,但找不到我想要實現的目標。 我有一條select語句,我想獲取比它大的最新日期,例如:

如果我有兩個日期為2015/06 / 01、2015 / 07/01,並且在我的表中,我有一個記錄的日期為2015/05/01,而另一個記錄的日期為2015/06/20。

對於2015/06/01,我想要的最大日期大於此情況下的最大日期為2015/05/01。

對於2015/07/01,我希望有一個大於的最大日期,在這種情況下,它將是2015/06/20。

我只希望一個選項不返回我當前得到的所有匹配項。

其他表還有其他聯接等,它會使用所有這些聯接,但出於這個目的,我將這些信息排除在外,因為它與我要實現的目標無關。 如果您需要更多信息,請發表評論。

評論后,我創建了一個Fiddle db結構http://sqlfiddle.com/#!6/2738f/2/0

所以這是我當前的查詢:

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'

您需要像這樣的東西嗎?

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

結果將是:

ref_month   max_date
2015/05/01  2015/04/02
2015/06/01  2015/05/10
2015/07/01  2015/06/20

我知道這很古老,但這是一種解決方法。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM