比较并从同一个表中获取列详细信息

Posted

技术标签:

【中文标题】比较并从同一个表中获取列详细信息【英文标题】:compare and fetch column detail from same table 【发布时间】:2017-05-09 09:54:08 【问题描述】:

我需要一些帮助来解决以下问题。

表格数据格式

Value       |     MATERIAL                DATE 
2949.00     |   000000000430450051 |  03/11/2013 00:00:00 
2949.00     |   000000000430450051 |  04/15/2013 00:00:00 
2949.00     |   000000000430450051 |  05/13/2013 00:00:00 
2949.00     |   000000000430450051 |  06/24/2013 00:00:00 
2949.00     |   000000000430450051 |  09/03/2013 00:00:00 
2949.00     |   000000000430450051 |  10/14/2013 00:00:00 
2949.00     |   000000000430450051 |  11/11/2013 00:00:00 
2949.00     |   000000000430450051 |  12/16/2013 00:00:00 
2949.00     |   000000000430450051 |  02/24/2014 00:00:00 
2425.26     |   000000000430450051 |  04/21/2014 00:00:00 <===
2425.26     |   000000000430450051 |  05/05/2014 00:00:00 
2425.26     |   000000000430450051 |  08/11/2014 00:00:00 
2425.26     |   000000000430450051 |  09/04/2014 00:00:00 
2425.26     |   000000000430450051 |  10/13/2014 00:00:00 
2425.26     |   000000000430450051 |  12/10/2014 00:00:00 
2425.26     |   000000000430450051 |  02/02/2015 00:00:00 
2425.26     |   000000000430450051 |  03/18/2015 00:00:00 
3032.00     |   000000000430450051 |  04/13/2015 00:00:00 <===
3032.00     |   000000000430450051 |  05/26/2015 00:00:00 
3032.00     |   000000000430450051 |  06/15/2015 00:00:00 
3032.00     |   000000000430450051 |  07/20/2015 00:00:00 

我怎样才能实现如下输出,我需要选择与值列更改对应的日期列,如下所示

2425.26     |     04/21/2014 00:00:00
3032.00     |     04/13/2015 00:00:00

【问题讨论】:

到目前为止你有什么尝试,你有没有做过任何研究,你有什么想法?我们不是来为您工作,而是帮助您自己找到答案。 This question 可能会帮助您删除中心列,而 this 可能会帮助您检查值是否已更改。 【参考方案1】:

您可以使用滞后并获取下一个值并找到如下变化

select * from (
select *, NextValue = lag(value,1,null) over (order by date) from #yourMaterial
) a
where value <> nextvalue

你的输出:

+---------+--------------------+-------------------------+
|  value  |      material      |          date           |
+---------+--------------------+-------------------------+
| 2425.26 | 000000000430450051 | 2014-04-21 00:00:00.000 |
| 3032    | 000000000430450051 | 2015-04-13 00:00:00.000 |
+---------+--------------------+-------------------------+

【讨论】:

【参考方案2】:

您可以对值和材料列进行分组,并在选择语句中获取 MIN 日期

SELECT Value,
Material,
MIN(Date)
FROM [YourTable]
GROUP BY Value,
Material

【讨论】:

【参考方案3】:

这是完整的场景,使用 WITH 子句以提高可读性:

WITH
-- your input
input(value,material,dt) AS (
          SELECT 2949.00,'000000000430450051',DATE '03/11/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '04/15/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '05/13/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '06/24/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '09/03/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '10/14/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '11/11/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '12/16/2013'
UNION ALL SELECT 2949.00,'000000000430450051',DATE '02/24/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '04/21/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '05/05/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '08/11/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '09/04/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '10/13/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '12/10/2014'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '02/02/2015'
UNION ALL SELECT 2425.26,'000000000430450051',DATE '03/18/2015'
UNION ALL SELECT 3032.00,'000000000430450051',DATE '04/13/2015'
UNION ALL SELECT 3032.00,'000000000430450051',DATE '05/26/2015'
UNION ALL SELECT 3032.00,'000000000430450051',DATE '06/15/2015'
UNION ALL SELECT 3032.00,'000000000430450051',DATE '07/20/2015'
)
-- end of your input, start "real" WITH clause - and your query - here
,
input_w_change_marker AS (
  SELECT
    *
  , value <> LAG(value) OVER(PARTITION BY material ORDER BY dt) AS val_has_changed
  FROM input
)
SELECT
  value
, dt
FROM input_w_change_marker
WHERE val_has_changed
;

value   |dt
2,425.26|2014-04-21
3,032.00|2015-04-13

【讨论】:

以上是关于比较并从同一个表中获取列详细信息的主要内容,如果未能解决你的问题,请参考以下文章

比较两个列并从同一个表中的另一列获取数据

如何从一个表中获取包含另一表的详细信息的记录计数

输入详细信息表时,我无法从主表中获取 ID(Laravel 8)

oracle学习篇十二:索引

接受用户的员工编号 从“员工”表中获取员工详细信息并将其存储到 PL SQL 记录类型变量并打印相同

如何从更新表中获取最新状态并将其与 MySQL 中的详细信息表连接?