简体   繁体   English

选择最近的日期

[英]Select most recent dates

I am trying to write a query that will return only the most recent results. 我正在尝试编写一个仅返回最新结果的查询。 The table I am pulling information from has no uniqiue columns, and contains information on rate changes so for any particular client there can be several rate changes - I only want the most recent rate change for each client. 我从中获取信息的表没有uniqiue列,并且包含有关费率变化的信息,因此对于任何特定客户端,都可以有多个费率变化-我只希望每个客户的最新费率变化。

The structure is: 结构为:

mrmatter VARCHAR(14)
mrtk VARCHAR(14)
mreffdate DATETIME
mrtitle VARCHAR(100)
mrrate INT
mrdevper INT

Some sample data is: 一些样本数据是:

mrmatter         mrtk      mreffdate                  mrtitle  mrrate   mrdevper
184-00111        0005      2001-03-19 00:00:00.000    !        250      NULL
184-00111        0259      2001-03-19 00:00:00.000    !        220      NULL
184-00111        9210      2001-03-19 00:00:00.000    !        220      NULL
184-00111        0005      2007-07-01 00:00:00.000    !        NULL     NULL

From the data above you can see there is two mrtk (0005), from these results it should only return three instead of the four rows. 从上面的数据中可以看到有两个mrtk(0005),从这些结果来看,它应该仅返回三行而不是四行。

The query isnt just on mrtk, instead of mrtk there could be a mrtitle in which case I would need to find the most recent date, when there is multiples. 该查询不是仅在mrtk上出现,而不是在mrtk上可能会有一个mrtitle,在这种情况下,如果有多个,我将需要查找最新日期。

I have tried the following query, it returns the results sorted in newest to oldest, but it returns four rows (two 0005) instead of only the three. 我尝试了以下查询,它返回的结果按从最新到最旧的顺序排序,但是它返回四行(两个0005)而不是仅三行。 I have tried different ways of doing the same query but it all returns the same results. 我尝试了执行相同查询的不同方法,但它们均返回相同的结果。

SELECT mrmatter,mrtk,mrrate,MAX(mreffdate) AS 'MostRecent'
FROM mexrate
WHERE mrmatter='184866-00111'
GROUP BY mrmatter,mrtk,mrrate

Any assistance that can be provided would be greatly appreciated. 可以提供的任何帮助将不胜感激。

UPDATE: The mrrate column can contain nulls, and the nulls can be the most recent entry. 更新: mrrate列可以包含null,并且null可以是最新的条目。 What I am after is the most recent entry for the same mrmatter AND (mrtk OR mrtitle). 我需要的是相同的matter和(mrtk或mrtitle)的最新条目。

Some more sample data is: 一些其他示例数据是:

mrmatter      mrtk               mrtk     mrrate   mreffdate
100626-01406    Senior Assoc    !   235.000 2006-01-25 00:00:00.000
100626-01406    Solicitor   !   235.000 2006-01-25 00:00:00.000
100626-01407    Associate            !  265.000 2006-01-30 00:00:00.000
100626-01407    Associate            !  276.000 2007-07-01 00:00:00.000
100626-01407    Partner          !  265.000 2006-01-30 00:00:00.000
100626-01407    Partner          !  276.000 2007-07-01 00:00:00.000
100626-01407    Senior Assoc    !   265.000 2006-01-30 00:00:00.000
100626-01407    Senior Assoc    !   276.000 2007-07-01 00:00:00.000

Matt 马特

The group by clause have mrrate column as well. group by子句也具有mrrate列。 For the two rows of mrtk(0005), first row have mrrate as 250 and second row have mrrate as NULL. 对于mrtk(0005)的两行,第一行的mrrate为250,第二行的mrrate为NULL。 This will certainly result in two rows of 0005. 这肯定会导致两行0005。

The query is working fine. 查询工作正常。 You may remove mrrate from group by but probably there is some functionality attached to it which is not clear. 您可以从group by中删除mrrate,但是可能附加了一些尚不清楚的功能。

I was able to achieve what I was after by using this query: 通过使用以下查询,我可以实现所追求的目标:

SELECT t1.mrmatter,t2.mrtk,t1.mrrate,t2.MostRecent
FROM mexrate t1
INNER JOIN
(
    SELECT DISTINCT(mrtk),MAX(mreffdate) AS MostRecent
    FROM mexrate
    WHERE mrmatter='184866-00111'    
    GROUP BY mrtk
) t2 ON t1.mrtk=t2.mrtk AND t1.mreffdate=t2.MostRecent
WHERE mrmatter='184866-00111' 

Thanks everyone for your assistance with this problem, it is, as always, greatly appreciated. 感谢大家在此问题上的协助,一如既往,我们非常感谢。

Matt 马特

try this: 尝试这个:

DECLARE @YourTable table
(mrmatter VARCHAR(14)
,mrtk VARCHAR(14)
,mreffdate DATETIME
,mrtitle VARCHAR(100)
,mrrate INT
,mrdevper INT
)

insert into @YourTable values('184-00111',        '0005'     , '2001-03-19 00:00:00.000'   , '!'    ,    250   ,   NULL)
insert into @YourTable values('184-00111',        '0259'     , '2001-03-19 00:00:00.000'   , '!'    ,    220   ,   NULL)
insert into @YourTable values('184-00111',        '9210'     , '2001-03-19 00:00:00.000'   , '!'    ,    220   ,   NULL)
insert into @YourTable values('184-00111',        '0005'     , '2007-07-01 00:00:00.000'   , '!'    ,    NULL  ,   NULL)

SELECT
    mrmatter
        ,mrtk
        ,mreffdate
        ,mrtitle
        ,mrrate
        ,mrdevper
    FROM (SELECT
              row_number() over(partition by mrtk order by mrtk,mreffdate DESC) AS RankValue,*
              FROM @YourTable
         ) dt
    WHERE RankValue=1

output: 输出:

mrmatter       mrtk           mreffdate               mrtitle  mrrate      mrdevper
-------------- -------------- ----------------------- -------- ----------- -----------
184-00111      0005           2007-07-01 00:00:00.000 !        NULL        NULL
184-00111      0259           2001-03-19 00:00:00.000 !        220         NULL
184-00111      9210           2001-03-19 00:00:00.000 !        220         NULL

(3 row(s) affected)

EDIT 编辑
I've read your question again, and I'm not 100% sure for your grouping requirement, perhaps because the sample data is a little sparse. 我已经再次阅读了您的问题,但是我不确定您的分组要求是100%肯定,这可能是因为样本数据很少。 This may be the query for you: 这可能是您的查询:

SELECT
    mrmatter
        ,mrtk
        ,mreffdate
        ,mrtitle
        ,mrrate
        ,mrdevper
    FROM (SELECT
              row_number() over(partition by mrmatter,mrtk,mrrate order by mrmatter,mrtk,mrrate,mreffdate DESC) AS RankValue,*
              FROM @YourTable
         ) dt
    WHERE RankValue=1

It will yield the same 4 rows as your query, because you are trying to put mrrate into the group, and 0005 has two: 250 and NULL. 它将产生与查询相同的4行,因为您正试图将mrrate放入组中,并且0005具有两个:250和NULL。 If you want to eliminate NULL use: 如果要消除NULL,请使用:

SELECT
    mrmatter
        ,mrtk
        ,mreffdate
        ,mrtitle
        ,mrrate
        ,mrdevper
    FROM (SELECT
              row_number() over(partition by mrmatter,mrtk,mrrate order by mrmatter,mrtk,mrrate,mreffdate DESC) AS RankValue,*
              FROM @YourTable
              WHERE mrrate IS NOT NULL
         ) dt
    WHERE RankValue=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM