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

时间:2017-05-09 09:54:09

标签: sql sql-server sql-server-2012 sql-server-2016

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

表数据格式

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

3 个答案:

答案 0 :(得分: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 |
+---------+--------------------+-------------------------+

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

这是完整的场景,使用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