I have table src
. In this table, each Product-Country combination might contain NULL
in the Country
column for the first x
rows.
How can I SELECT
rows for each Product-Country combination, only rows after the first x
rows that contain NULL
in the Country column? It's important to also SELECT
any latter rows, even if they do contain NULL
in the Country column, as long as there was at least one row with a true value, prior to it.
Expected Output
For example.
- For product A, I would like to
SELECT
rows with ID 3 and 4, but not ID 1 and 2. - For Product B, I would like to
SELECT
rows with ID 6 and 7, but not ID 5.
ID | Product | Country | Timestamp |
---|---|---|---|
3 | A | AT | 2022-01-23 14:29:06.830 |
4 | A | NULL | 2022-01-24 14:29:06.830 |
6 | B | CH | 2022-01-23 14:29:06.830 |
7 | B | NULL | 2022-01-24 14:29:06.830 |
Sample Table and Data
create table dbo.src
(
ID int not null
,Product varchar(2) not null
,Country varchar(2) null
,[Timestamp] datetime not null
)
insert into dbo.src
(ID,Product, Country, [Timestamp])
values
(1,'A',NULL,'2022-01-21 14:29:06.830')
,(2,'A',NULL,'2022-01-22 14:29:06.830')
,(3,'A','AT','2022-01-23 14:29:06.830')
,(4,'A',NULL,'2022-01-24 14:29:06.830')
,(5,'B',NULL,'2022-01-22 14:29:06.830')
,(6,'B','CH','2022-01-23 14:29:06.830')
,(7,'B',NULL,'2022-01-24 14:29:06.830')