删除或更改ETL中的记录

问题描述

我有一张表,在上面建立了ETL服务。货物记录(到达/离开)进入表格。我已经做完了,我的桌子将被删除。当商品标识符第二次到达数据库时,两条记录都将被删除

<?PHP
                //related products
                $terms = get_the_terms( $post->ID,'product-category','string');
                $term_ids = wp_list_pluck($terms,'term_id');

                  $second_query = new WP_Query( array(
                      'post_type' => 'products','tax_query' => array(
                                    array(
                                        'taxonomy' => 'product-category','field' => 'id','terms' => $term_ids,'operator'=> 'AND' //Or 'AND' or 'NOT IN'
                                     )),'posts_per_page' => 3,'ignore_sticky_posts' => 1,'orderby' => 'rand','post__not_in'=>array($post->ID)
                   ) );
                      if($second_query->have_posts()) {
                         while ($second_query->have_posts() ) : $second_query->the_post(); ?>
                          <div class="single_related">
                               <?PHP if (has_post_thumbnail()) { ?>
                                <a href="<?PHP the_permalink() ?>" title="<?PHP the_title(); ?>"> <?PHP the_post_thumbnail( 'related_sm',array('alt' => get_the_title()) ); ?> </a>
                                <?PHP } else { ?>
                                     <a href="<?PHP the_permalink() ?>" title="<?PHP the_title(); ?>"><?PHP the_title(); ?></a>
                                <?PHP } ?>
                           </div>
                       <?PHP endwhile; wp_reset_query();
                       }?>

现在ETL服务删除记录(每30秒一次):

label   cost   time
x2       29    14/5/2020 01:00:00
x3       20    14/5/2020 01:02:00
x2       29    15/5/2020 03:12:02

我使用以下功能将其删除

label   cost   time
x3       20    14/5/2020 01:02:00

一个问题困扰。谈到餐桌,这仅意味着价格变动。

变种1:

with todelete as (
      select *,count(*) over (partition by label) as cnt,ROW_NUMBER() over (partition by label order by time DESC) as r_number
      from Table1
     )
delete from todelete
    where cnt >= 2 

现在“删除功能”和

我的目标:

label   cost   time
x2       29    14/5/2020 01:00:00
x3       20    14/5/2020 01:02:00
x2       30    15/5/2020 03:12:02

我不知道如何在删除函数中同时处理这两个问题。

解决方法

要满足这两个要求,您应检查以下费用的变化

 ; with todelete as (
      select *,count(*) over (partition by label) as cnt,lag(cost) over (partition by label order by time ASC) as lastcost
           ROW_NUMBER() over (partition by label order by time ASC) as r_number
      from Table1
     )
delete from todelete 
    where cnt > 1 and r_number between 1 and (cnt/2)*2 and  cost=ISNULL(lastcost,cost)