SQL Server:选择列中的值从值列表更改为另一个值列表的行

时间:2017-08-22 16:19:50

标签: sql sql-server

让我说我在sql server数据库中有这个表,通过增加hist_pp进行排序:

hist_id   hist_yr   hist_pp   hist_empl_id   hist_empl_sect_id
90619       2017       5         00018509           61
92295       2017       6         00018509           61
93991       2017       7         00018509           61
95659       2017       8         00018509           99
103993      2017       9         00018509           99
120779      2017       10        00018509           99

我想找到hist_empl_sect_id从一组数字中的任何值变化的行,例如(60,61,62,63)到另一组数字中的任何值,比如说(98,99,100等) 。它必须是每年,所以对于2017年的值.hist_pp将是一年中增加的数字。 hist_id也是一个id自动编号列。

该员工应该返回

95659       2017       8         00018509           99

我尝试了一些我在其他帖子中看到的例子,用CTE等尝试过,我似乎无法让它发挥作用。

这是我尝试过的一个例子,但没有用,当员工应该只有1时,为员工获得了多行:

select a.hist_id, a.hist_yr, format(cast(a.hist_pp as integer), '0#') as hist_pp, a.hist_empl_id, a.hist_empl_sect_id
from temshist a
where a.hist_empl_sect_id <>
    (SELECT top 1 b.hist_empl_sect_id
    FROM temshist as b
    where a.hist_empl_id = b.hist_empl_id
    and a.hist_yr = b.hist_yr
    and a.hist_pp > b.hist_pp
    Order by b.hist_pp desc
    )
order by hist_empl_id

5 个答案:

答案 0 :(得分:2)

我怀疑 Lag()非常适合。

示例

 ;with cte as (
    Select * 
          ,PrevValue= Lag(hist_empl_sect_id,1,hist_empl_sect_id) over (Partition by hist_empl_id Order By hist_pp)
     From  @YourTable
)
Select *
 From  cte Where PrevValue/98<>hist_empl_sect_id/98
  

编辑 - VamsiPrabhala指出

您也可以按年份分区

      ,PrevValue= Lag(hist_empl_sect_id,1,hist_empl_sect_id) over (Partition by hist_yr,hist_empl_id Order By hist_pp)

答案 1 :(得分:0)

这是另一种选择,我用CTE嘲笑它来模拟你的数据,然后重新加入。

with emp_hist as
(
    select 90619 as hist_id, 2017 as hist_yr, 5 as hist_pp, '00018509' as hist_empl_id, 61 as hist_empl_sect_id from dual
    union all
    select 92295 as hist_id, 2017 as hist_yr, 6 as hist_pp, '00018509' as hist_empl_id, 61 as hist_empl_sect_id from dual
    union all
    select 93991 as hist_id, 2017 as hist_yr, 7 as hist_pp, '00018509' as hist_empl_id, 61 as hist_empl_sect_id from dual
    union all
    select 95659 as hist_id, 2017 as hist_yr, 8 as hist_pp, '00018509' as hist_empl_id, 99 as hist_empl_sect_id from dual
    union all
    select 103993 as hist_id, 2017 as hist_yr, 9 as hist_pp, '00018509' as hist_empl_id, 99 as hist_empl_sect_id from dual
    union all
    select 120779 as hist_id, 2017 as hist_yr, 10 as hist_pp, '00018509' as hist_empl_id, 99 as hist_empl_sect_id from dual
)
select eh2.*
from emp_hist eh1
join emp_hist eh2
on eh1.hist_empl_id = eh2.hist_empl_id
and eh1.hist_pp = (eh2.hist_pp - 1)
and eh1.hist_yr = eh2.hist_yr
where eh2.hist_empl_sect_id in (98, 99, 100)
and eh1.hist_empl_sect_id in (60, 61, 62, 63)
;

答案 2 :(得分:0)

您可以使用案例陈述(以确定组成员资格)和滞后窗口函数(以比较两个连续行)按雇员和年份划分并按hist_pp排序

这假定     (1)员工ID可以跨越多年     (2)Hist_pp对于每个员工ID,年份组合是唯一的     (3)如果员工ID中的hist_empl_sect_id只有一个唯一值,年份组合(该年度该员工的hist_empl_sect_id不会更改),则结果集不应包含该员工ID,年份组合的任何行。

Hist_pp可能有差距。

    select hist_id, hist_yr, hist_pp, hist_empl_id, hist_empl_sect_id
    from 
    (

       select a.hist_id, a.hist_yr, 
              format(cast(a.hist_pp as integer), '0#') as hist_pp, 
              a.hist_empl_id, 
              -- hist_empl_sect_id of current row
              a.hist_empl_sect_id,

              -- hist_empl_sect_id of preceding row, when ordered by hist_pp for each employee year combination
              lag(a.hist_empl_sect_id, 1) 
                   OVER (
                          PARTITION BY a.hist_empl_id,a.hist_yr 
                             ORDER BY format(cast(a.hist_pp as integer), '0#') 
                         ) as prev_hist_empl_sect_id
            from temshist a
    ) as outr
where 
    -- group membership of hist_empl_sect_id of current row
    (case when hist_empl_sect_id IN (98, 99, 100) then 1 else 0 end) 
    <> 
    -- group membership of hist_empl_sect_id of preceding row, ordered by hist_pp for each year
    (case when prev_hist_empl_sect_id IN (98, 99, 100) then 1 else 0 end)

    AND

    -- Preceding row does not belong to a different employee or year
    prev_hist_empl_sect_id IS NOT NULL

答案 3 :(得分:0)

查看结果数据,我假设每个hist_yrhist_empl_id,您需要hist_ppminhist_empl_sect_id为{1}}的记录行max,下面是生成所需输出的查询。

SELECT t3.* from
  (SELECT t2.*, min(hist_pp) over(partition BY hist_yr, hist_empl_id) AS hist_pp_minValue
   FROM
     (SELECT hist_id, hist_yr, hist_pp, hist_empl_id, hist_empl_sect_id, r1, max(r1) over (partition BY hist_yr, hist_empl_id) AS maxRank
      FROM
        (SELECT hist_id, hist_yr, hist_pp, hist_empl_id, hist_empl_sect_id, dense_rank() over(partition BY hist_yr, hist_empl_id
                                                                                              ORDER BY hist_empl_sect_id) AS r1
         FROM table1)t1) t2
   WHERE t2.maxRank = t2.r1 )t3
WHERE t3.hist_pp_minValue = t3.hist_pp

我已对相关数据进行了测试,结果如下。

hist_id | hist_yr  |  hist_pp | hist_empl_id | hist_empl_sect_id
---------------------------------------------------------
95659     2017         8         18509          99

为了重新保证,我增加了一些样本数据,如下所示。

insert into table1 values(90619  ,2018,5  ,00018508,62);
insert into table1 values(92295  ,2018,6  ,00018508,62);
insert into table1 values(93991  ,2018,7  ,00018508,62);
insert into table1 values(95659  ,2018,8  ,00018508,91);
insert into table1 values(103993 ,2018,9  ,00018508,91);
insert into table1 values(120779 ,2018,10 ,00018508,91);

以下是生成的结果。

hist_id | hist_yr  |  hist_pp | hist_empl_id | hist_empl_sect_id
---------------------------------------------------------
95659     2017         8         18509          99
95659     2018         8         18508          91

您可以查看演示here

希望这会有所帮助。

答案 4 :(得分:0)

我认为您想要使用CTE来解决这个问题。这类似于John Cappelletti所做的,但不需要SQL 2012或更高版本。

declare @temshist table
(
    hist_id int,
    hist_yr int,
    hist_pp int,
    hist_empl_id varchar(max),
    hist_empl_sect_id int
)

insert into @temshist ( hist_id, hist_yr, hist_pp, hist_empl_id, hist_empl_sect_id )
values
    ( 90619, 2017, 5, '00018509', 61 ),
    ( 92295, 2017, 6, '00018509', 61 ),
    ( 93991, 2017, 7, '00018509', 61 ),
    ( 95659, 2017, 8, '00018509', 99 ),
    ( 103993, 2017, 9, '00018509', 99 ),
    ( 120779, 2017, 10, '00018509', 99 )

;with empl_cte as
(
    select
        row_number() over (partition by hist_empl_id, hist_yr order by hist_pp) as [rn],
        hist_id,
        hist_yr,
        hist_pp,
        hist_empl_id,
        hist_empl_sect_id
    from @temshist
)
select 
    nxt.hist_id,
    nxt.hist_yr,
    nxt.hist_pp,
    nxt.hist_empl_id,
    nxt.hist_empl_sect_id
from empl_cte prv
    left join empl_cte nxt on 
        prv.hist_empl_id = nxt.hist_empl_id and 
        prv.rn = nxt.rn - 1
where prv.hist_empl_sect_id in (60, 61, 62, 63/*, ...*/) and nxt.hist_empl_sect_id in (98, 99, 100/*, ...*/)