由於其他註釋狀態,根據規定的規則,倒數第二行在B中應該有一個值'N'。以下腳本根據規定的規則返回正確的結果,但與您的示例輸出不匹配:
/* test data */
select '001' as a, 'Y' as b, 'Pizza' as c, 'Pepperoni' as d into testtable union all
select '002' as a, 'Y' as b, 'Pizza' as c, 'Pepperoni' as d union all
select '003' as a, 'Y' as b, 'Pizza' as c, 'Pepperoni' as d union all
select '003' as a, 'N' as b, 'Pizza' as c, 'Sausage' as d union all
select '004' as a, 'Y' as b, 'Pizza' as c, 'Pepperoni' as d union all
select '005' as a, 'Y' as b, 'Pizza' as c, 'Pepperoni' as d union all
select '005' as a, 'N' as b, 'Pizza' as c, 'Sausage' as d union all
select '005' as a, 'N' as b, 'Hamburger' as c, 'Cheese' as d
go
/* script */
select
a
,case when b = 'Y' then null else
case when c = lag(c, 1) over (partition by a order by c desc, d) then 'Y' else 'N' end
end as b
from testtable
go
/* Results...*/
|-----+------|
| A | B |
|-----+------|
| 001 | NULL |
| 002 | NULL |
| 003 | NULL |
| 003 | Y |
| 004 | NULL |
| 005 | NULL |
| 005 | Y |
| 005 | N |
|-----+------|
你能正確格式化你的例子嗎? – fhossfel
我改進了格式。有人必須進行同行評審並接受它。 –
爲什麼倒數第二是Y輸出。它應該是N,因爲對於ID 005,有值比薩餅和漢堡包 –