选择最近的日期
我正在尝试编写一个仅返回最新结果的查询。 我从中提取信息的表没有唯一的列,并且包含有关费率变化的信息,因此对于任何特定客户端,可能会有多个费率变化 - 我只想要每个客户端的最新费率变化。
结构是:
mrmatter VARCHAR(14)
mrtk VARCHAR(14)
mreffdate DATETIME
mrtitle VARCHAR(100)
mrrate INT
mrdevper INT
一些示例数据是:
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
从上面的数据中您可以看到有两个 mrtk (0005),从这些结果中它应该只返回三行而不是四行。
查询不仅仅在 mrtk 上,而不是 mrtk,可能有一个 mrtitle,在这种情况下,当有多个日期时,我需要找到最近的日期。
我尝试了以下查询,它返回按最新到最旧排序的结果,但它返回四行(两行 0005),而不是仅三行。 我尝试了不同的方法来执行相同的查询,但它都返回相同的结果。
SELECT mrmatter,mrtk,mrrate,MAX(mreffdate) AS 'MostRecent'
FROM mexrate
WHERE mrmatter='184866-00111'
GROUP BY mrmatter,mrtk,mrrate
如果您能提供任何帮助,我们将不胜感激。
更新: mrrate 列可以包含空值,并且空值可以是最近的条目。 我所追求的是相同 mrmatter AND (mrtk OR mrtitle) 的最新条目。
更多示例数据是:
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
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.
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.
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.
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. 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. What I am after is the most recent entry for the same mrmatter AND (mrtk OR 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
输出:
编辑
我再次阅读了您的问题,我不能 100% 确定您的分组要求,也许是因为样本数据有点稀疏。 这可能是适合您的查询:
它将产生与您的查询相同的 4 行,因为您试图将 mrrate 放入组中,而 0005 有两个:250 和 NULL。 如果你想消除 NULL 使用:
try this:
output:
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. This may be the query for you:
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. If you want to eliminate NULL use:
group by 子句也有 mrrate 列。 对于 mrtk(0005) 的两行,第一行的 mrrate 为 250,第二行的 mrrate 为 NULL。 这肯定会导致两行 0005。
查询工作正常。 您可以从 group by 中删除 mrrate,但可能附加了一些尚不清楚的功能。
The group by clause have mrrate column as well. For the two rows of mrtk(0005), first row have mrrate as 250 and second row have mrrate as NULL. This will certainly result in two rows of 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.
我能够通过使用此查询实现我所追求的目标:
感谢大家对这个问题的帮助,一如既往,非常感谢。
马特
I was able to achieve what I was after by using this query:
Thanks everyone for your assistance with this problem, it is, as always, greatly appreciated.
Matt