简体   繁体   English

选择SQL中的最新条目

[英]Select Most Recent Entry in SQL

I'm trying to select the most recent non zero entry from my data set in SQL. 我正在尝试从SQL中的数据集中选择最近的非零条目。 Most examples of this are satisfied with returning only the date and the group by variables, but I would also like to return the relevant Value. 多数示例仅返回日期和分组依据变量,但我也想返回相关的值。 For example: 例如:

ID       Date          Value
----------------------------
001      2014-10-01     32
001      2014-10-05     10
001      2014-10-17      0
002      2014-10-03     17
002      2014-10-20     60
003      2014-09-30     90
003      2014-10-10      7
004      2014-10-06    150
005      2014-10-17      0
005      2014-10-18      9

Using 运用

SELECT ID, MAX(Date) AS MDate FROM Table WHERE Value > 0 GROUP BY ID

Returns: 返回:

ID       Date      
-------------------
001      2014-10-05
002      2014-10-20
003      2014-10-10
004      2014-10-06
005      2014-10-18

But whenever I try to include Value as one of the selected variables, SQLServer results in an error: 但每当我尝试将Value包含为所选变量之一时,SQLServer都会导致错误:

"Column 'Value' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." “列'值'在选择列表中无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。”

My desired result would be: 我想要的结果是:

ID       Date          Value
----------------------------
001      2014-10-05     10
002      2014-10-20     60
003      2014-10-10      7
004      2014-10-06    150
005      2014-10-18      9

One solution I have thought of would be to look up the results back in the original Table and return the Value that corresponds to the relevant ID & Date (I have already trimmed down and so I know these are unique), but this seems to me like a messy solution. 我想到的一个解决方案是在原始表中查找结果并返回与相关ID和日期相对应的值(我已经减少了,所以我知道这些是唯一的),但这在我看来像一个混乱的解决方案。 Any help on this would be appreciated. 任何有关这方面的帮助将不胜感激。

NOTE: I do not want to group by Value as this is the result I am trying to pull out in the end (ie for each ID, I want the most recent Value). 注意:我不想按值分组,因为这是我最终想要提取的结果(即对于每个ID,我想要最新的值)。 Further Example: 进一步举例:

ID       Date          Value
----------------------------
001      2014-10-05     10
001      2014-10-06     10
001      2014-10-10     10
001      2014-10-12      8
001      2014-10-18      0

Here, I only want the last non zero entry. 在这里,我只想要最后一个非零条目。 (001, 2014-10-12, 8) (001,2014-10-12,8)

SELECT ID, MAX(Date) AS MDate, Value FROM Table WHERE Value > 0 GROUP BY ID, Value

Would return: 会回来:

ID       Date          Value
----------------------------
001      2014-10-10     10
001      2014-10-12      8

This can also be done using a window function which is very ofter faster than a join on a grouped query: 这也可以使用窗口函数来完成,该函数比对分组查询的联接要快得多:

select id, date, value
from (
  select id,
         date,
         value,
         row_number() over (partition by id order by date desc) as rn
  from the_table
) t
where rn = 1
order by id;

Assuming you don't have repeated dates for the same ID in the table, this should work: 假设您没有表中相同ID重复日期,这应该有效:

SELECT A.ID, A.Date, A.Value
FROM
   T1 AS A
   INNER JOIN (SELECT ID,MAX(Date) AS Date FROM T1 WHERE Value > 0 GROUP BY ID) AS B
      ON A.ID = B.ID AND A.Date = B.Date
select a.id, a.date, a.value from Table1 a inner join (

select id, max(date) mydate from table1 
where Value>0 group by ID) b on a.ID=b.ID and a.Date=b.mydate

Using Subqry, 使用Subqry,

SELECT  ID, Date AS MDate, VALUE 
FROM    table  t1
where   date = (Select max(date)
                from    table t2
                where   Value >0
                and     t1.id = t2.id
                )

Answers provided are perfectly adequate, but Using CTE: 提供的答案是完全足够的,但使用CTE:

;WITH cteTable
AS
(
  SELECT
    Table.ID [ID], MAX(Date) [MaxDate]
  FROM
    Table
  WHERE
    Table.Value > 0
  GROUP BY
    Table.ID
)

SELECT
    cteTable.ID, cteTable.Date, Table.Value
FROM
    Table INNER JOIN cteTable ON (Table.ID = cteTable.ID)

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

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