English 中文(简体)
使用分析功能选择有2个日期栏的记录
原标题:performant query using analytic function to select records with 2 date columns

I m 寻求一种手法,以书写SQ。

缩略语 并且i必须查阅该表中的以下记录:,lname,accountid,最新日期以greatest(max(生育_date),max(update_date)(注:/code> 无效

我预计,我需要利用分析职能。

我有这些情况:

(id,fname,lname,accountid,creation_date,update_date)
(1, a , a , 2 , 07/01/2010 10:59:43 , 07/01/2010 10:59:43 )
(2, a , a , 2 , 07/01/2010 10:59:43 , 07/01/2010 10:59:43 )
(3, a , a , 2 , 07/01/2010 10:59:43 , 07/01/2010 10:59:43 )

我要选择最后一点:这一记录(3,a,a, 2 , 07/01/2010 10:59:43 , 07/01/2010 10:59:43 )

(id,fname,lname,accountid,creation_date,update_date)
(3, b , a , 2 , 07/01/2009 10:59:43 , 07/01/2010 10:59:43 )
(4, b , a , 2 , 07/01/2011 10:59:43 ,null)
(5, b , a , 2 , 07/01/2009 10:59:43 , 07/01/2009 10:59:43 )

我想选择两个栏上的最新一个栏目(编号:日期,日期:日期)(4,b, a, 2 , 07/01/201110:59:43,null)

(id,fname,lname,accountid,creation_date,update_date)
(6, c , g , 4 , 07/01/2010 10:59:43 ,null)
(7, c , g , 4 , 07/01/2011 10:59:43 ,null)
(8, c , g , 4 , 07/01/2009 10:59:43 ,null)

我想选择两个栏上的最新一栏(制作——日期,日期——日期),即(7,c, g, 4 , 07/01/2011 10:59:43,null)

(id,fname,lname,accountid,creation_date,update_date)
(9, k , t , 2 , 07/01/2009 10:59:43 , 07/01/2012 10:59:43 )
(10, k , t , 2 , 07/01/2011 10:59:43 ,null)
(11, k , t , 2 , 07/01/2009 10:59:43 , 07/01/2009 10:59:43 )

I want to choose the most recent one on both columns (creation_date,update_date) which is (9, k , t , 2 , 07/01/2009 10:59:43 , 07/01/2012 10:59:43 )

最佳回答
问题回答

就像你想要的东西一样。

SELECT *
  FROM (SELECT a.*,
               rank() over (partition by fname, lname, accountid
                                order by coalesce( update_date, creation_date ) desc,
                                         id desc) rnk
          FROM your_table)
 WHERE rnk = 1
  • I m guessing a bit at why you would want the row with an id of 3 returned rather than the rows with an id of 1 or 2 since all 3 rows have the same update_date and creation_date. My guess is that you want to pick the tie by choosing the row with the largest id value. If you want to implement a different rule, you ll need to tell us what rule that is.
  • I am also assuming that the update_date, if it exists, will always be greater than or equal to the creation_date. It would be odd if a row were updated before it was created.




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签