简体   繁体   English

数据处理Row_Number()SQL Server

[英]Data Manipulation Row_Number() SQL SERVER

I need some guidance and help with a data manipulation question in SQL SERVER 2012. 我需要一些有关SQL SERVER 2012中数据处理问题的指导和帮助。

This is how my data looks like: 这是我的数据的样子:

=================================================
YearMonth LocationCode  New     ValidTo
=================================================
201412         2020     1       201502
201501        2020      1       201503
201503        3030      1       201506
201506        3030      1       201509

Problem description 问题描述

if you look at the above table, you will see the column YearMonth , locationCode , New which tells whether the locationCode is new for the month in the row. 如果你看一下上面的表格,你会看到列YearMonthlocationCodeNew它告诉是否locationCode是新换的行中的一个月。 The ValidTo column shows to which ValidTo YearMonth it is validto. ValidTo列显示对哪个ValidTo YearMonth有效。

For the example, for YearMonth 201412, the locationCode 2020 is 1, which means here it is New and that LocationCode is considered New till ValidTo 201502. 例如,对于YearMonth 201412, locationCode 2020为1,这意味着此处为New并且在ValidTo 201502之前, LocationCode被视为New。

My problem is that I need to make each LocationCode , that appears in the earliest YearMonth Column, to New, is this case "1" till the ValidTo YearMonth. 我的问题是,我需要将最早出现在YearMonth列中的每个LocationCode设为“ 1”,直到ValidTo YearMonth都为New。

Objective: 目的:

=================================================
YearMonth LocationCode  New     ValidTo
=================================================
201412         2020     1       201502
201501        2020      1       201503
201503        3030      1       201506
201506        3030      0       201509

Basically, I need to find out the MIN() for each LocationCode and then categorize it as NEW ,"1" and if the LocationCode appears within the MIN() YearMonth and ValidTo then categorize the LocationCode in New as "1". 基本上,我需要找出每个LocationCodeMIN() ,然后将其归类为NEW ,“ 1”,如果LocationCode出现在MIN() YearMonthValidTo然后将NewLocationCode归类为“ 1”。

How Can I do that? 我怎样才能做到这一点? The above table provides a visual example. 上表提供了一个直观的示例。

I have edited my final table to make it simpler to understand my question. 我已经编辑了决赛桌,以使其更容易理解我的问题。

Basically,the MIN() Year for LocationCode 3030 is 201503 and the LocationCode is valid till 201506 as demostrated in the Validto Column. 基本上, LocationCode 3030的MIN() Year为201503, LocationCode的有效期至201506,如Validto列中所述。 If the LocationCode 3030 were to appear in the YearMonth row, 201505 with a VALIDTO till 201506, then we classify it as New (1) as well. 如果LocationCode 3030出现在YearMonth行的201505中,并带有VALIDTO到201506,则我们也将其归类为New (1)。

Basically,pseudocode 基本上,伪代码

SELECT

MIN(YearMonth),
LocationCode

from Tabl

If LocationCode , is in MIN(YearMonth) AND ValidTO timeperiod then classify it as 1. How can I do this? 如果LocationCode在MIN(YearMonth)和ValidTO内,则将其分类为1。我该怎么做?

Please try the below query for updating the table: The inner most query calculates the minimum Yearmonth per LocationCode which is used to calculate all ValidTo and LocationCode s which need to be updated to 1 in ValidTo column 请尝试以下查询以更新表:最里面的查询将计算每个LocationCode的最小Yearmonth ,用于计算需要在ValidTo列中更新为1的所有ValidToLocationCode

update t2
set New= CASE when t1.ValidTo is null then 0 else 1 end 
FROM tbl t2
LEFT JOIN 
(
  select t.ValidTo, t.locationCode 
       from tbl t inner join
           (
             select 
                MIN(YearMonth) as MYM, 
                LocationCode 
             from tbl
               group by LocationCode
            ) g 
       on t.YearMonth=g.MYM and t.LocationCode=g.LocationCode
) t1
on t2.ValidTo=t1.ValidTo and t2.LocationCode=t1.LocationCode

sample sql fiddle for demo: http://sqlfiddle.com/#!6/229d8 用于演示的示例sql小提琴: http ://sqlfiddle.com/#!6 / 229d8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM