I need some guidance with using ROW_NUMBER()
along with BETWEEN
in SQL Server 2012.
I have written the following code:
SELECT DISTINCT
*,
CASE WHEN (row_number() OVER BETWEEN (PARTITION BY LocationCode
ORDER BY startdate ASC) AND EndDate) = 1
THEN 1 ELSE 0
END AS Testr,
FROM
Table
The purpose is to tag those locationCode BETWEEN MIN(startDate) AND EndDate
with 1 ELSE 0.
How can I do that?
I have added a table : this is how it looks right now
LocationCode StartDate EndDate
10808247 20140617 20140701
10808247 20140618 20140701
10808247 20140618 20140701
10808247 20140617 20140701
10808247 20140709 20140801
Objective:
LocationCode StartDate EndDate New
10808247 20140617 20140701 1
10808247 20140618 20140701 1
10808247 20140618 20140701 1
10808247 20140617 20140701 1
10808247 20140709 20140801 0
The last row for column New
is zero because it is outside the EndDate
.