SQL 服务器复杂案例
SQL Server Complex Case
我找不到一种方法来为与此示例类似的内容创建案例。我援引他的知识!
我每个月都会收到新奇的东西,字母从 A 到 E,我需要在每个客户礼物的大字母中写上 0。因此,如果我只收到 B,则 B=0,如果我收到 A 和 B,则 A=1 且 B=0,如果我收到 A、B、C,则 A=1、B=1 且 C=0,如果我收到 A 和 C,然后 A=1 和 C=0,由客户分组。
Customer Codes
111 A
111 B
111 C
222 A
222 B
333 A
In the below SQl you're doing some row number functions and then ordering by the highest row number. After that you do another row number function by the highest row_number it gets in the cte.
With myCte
as
(
Select Customer,Code,Row_Number() OVER (PARTITION BY Customer Order By Code) rn
from myTable
)
select Customer,Code,
Case when rn2 = 1 then 0
else 1
end as myArbitraryNumber
from
(
select Customer, Code, rn, Row_Number() OVER (Partition by Customer,rn Order by rn DESC) rn2
from myCte
)
我找不到一种方法来为与此示例类似的内容创建案例。我援引他的知识! 我每个月都会收到新奇的东西,字母从 A 到 E,我需要在每个客户礼物的大字母中写上 0。因此,如果我只收到 B,则 B=0,如果我收到 A 和 B,则 A=1 且 B=0,如果我收到 A、B、C,则 A=1、B=1 且 C=0,如果我收到 A 和 C,然后 A=1 和 C=0,由客户分组。
Customer Codes
111 A
111 B
111 C
222 A
222 B
333 A
In the below SQl you're doing some row number functions and then ordering by the highest row number. After that you do another row number function by the highest row_number it gets in the cte.
With myCte
as
(
Select Customer,Code,Row_Number() OVER (PARTITION BY Customer Order By Code) rn
from myTable
)
select Customer,Code,
Case when rn2 = 1 then 0
else 1
end as myArbitraryNumber
from
(
select Customer, Code, rn, Row_Number() OVER (Partition by Customer,rn Order by rn DESC) rn2
from myCte
)