根据最新日期获取 table 的最新行和 SQL Server 2008 R2 中的不同列
Get the latest row of a table based on Latest Date and a distinct column in SQL Server 2008 R2
如果我有以下SQLTable
<table border="1">
<tr><td>colOne</td><td>colTwo</td><td>colThree</td><td>colDate</td></tr>
<tr><td>0</td><td>1</td><td>8</td><td>2014-02-05 00:00:00.000</td></tr>
<tr><td>0</td><td>6</td><td>8</td><td>2014-05-13 00:00:00.000</td></tr>
<tr><td>0</td><td>10</td><td>37</td><td>2014-07-25 00:00:00.000</td></tr>
<tr><td>0</td><td>12</td><td>184</td><td>2014-08-11 00:00:00.000</td></tr>
<tr><td>0</td><td>13</td><td>11</td><td>2014-08-12 00:00:00.000</td></tr>
<tr><td>0</td><td>41</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
<tr><td>0</td><td>42</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
</table>
并且我想要下面的结果,这是每个不同的colThree的最新记录
<table border="1">
<tr><td>colOne</td><td>colTwo</td><td>colThree</td><td>colDate</td></tr>
<tr><td>0</td><td>6</td><td>8</td><td>2014-05-13 00:00:00.000</td></tr>
<tr><td>0</td><td>10</td><td>37</td><td>2014-07-25 00:00:00.000</td></tr>
<tr><td>0</td><td>12</td><td>184</td><td>2014-08-11 00:00:00.000</td></tr>
<tr><td>0</td><td>13</td><td>11</td><td>2014-08-12 00:00:00.000</td></tr>
<tr><td>0</td><td>42</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
</table>
非常感谢。
也许最快的方法是:
select t.*
from t
where t.colDate = (select max(t2.colDate) from t t2 where t2.colThree = t.colThree);
使用 row_number()
的更规范的方式将满足您的需求:
select t.*
from (select t.*,
row_number() over (partition by colThree order by colDate desc) as seqnum
from t
) t
where seqnum = 1;
如果出现平局,这将选择任意一行。
如果我有以下SQLTable
<table border="1">
<tr><td>colOne</td><td>colTwo</td><td>colThree</td><td>colDate</td></tr>
<tr><td>0</td><td>1</td><td>8</td><td>2014-02-05 00:00:00.000</td></tr>
<tr><td>0</td><td>6</td><td>8</td><td>2014-05-13 00:00:00.000</td></tr>
<tr><td>0</td><td>10</td><td>37</td><td>2014-07-25 00:00:00.000</td></tr>
<tr><td>0</td><td>12</td><td>184</td><td>2014-08-11 00:00:00.000</td></tr>
<tr><td>0</td><td>13</td><td>11</td><td>2014-08-12 00:00:00.000</td></tr>
<tr><td>0</td><td>41</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
<tr><td>0</td><td>42</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
</table>
并且我想要下面的结果,这是每个不同的colThree的最新记录
<table border="1">
<tr><td>colOne</td><td>colTwo</td><td>colThree</td><td>colDate</td></tr>
<tr><td>0</td><td>6</td><td>8</td><td>2014-05-13 00:00:00.000</td></tr>
<tr><td>0</td><td>10</td><td>37</td><td>2014-07-25 00:00:00.000</td></tr>
<tr><td>0</td><td>12</td><td>184</td><td>2014-08-11 00:00:00.000</td></tr>
<tr><td>0</td><td>13</td><td>11</td><td>2014-08-12 00:00:00.000</td></tr>
<tr><td>0</td><td>42</td><td>14</td><td>2015-02-16 00:00:00.000</td></tr>
</table>
非常感谢。
也许最快的方法是:
select t.*
from t
where t.colDate = (select max(t2.colDate) from t t2 where t2.colThree = t.colThree);
使用 row_number()
的更规范的方式将满足您的需求:
select t.*
from (select t.*,
row_number() over (partition by colThree order by colDate desc) as seqnum
from t
) t
where seqnum = 1;
如果出现平局,这将选择任意一行。