我需要通过从 SQL Server 中的现有列分配值来创建新列。
我想根据
commission2
金额获取 commission
列。
有些客户可以包含1或2个品牌和佣金金额。
客户编号 | 计数客户 | 品牌 | 佣金 | 佣金2 |
---|---|---|---|---|
1000 | 1 | B | -28 | 0 |
1001 | 2 | B | 12 | 12 |
1001 | 2 | F | 252 | 252 |
1002 | 1 | B | 62 | 62 |
1003 | 1 | B | 5 | 5 |
1004 | 1 | B | -61 | 0 |
1005 | 1 | F | 0 | 0 |
1006 | 1 | F | 32 | 32 |
1007 | 1 | F | 0 | 0 |
1008 | 1 | B | -496 | 0 |
1009 | 1 | F | 1 | 1 |
1010 | 1 | B | 1 | 1 |
1011 | 1 | B | 0 | 0 |
1012 | 1 | F | -10 | 0 |
1013 | 1 | B | 82 | 82 |
1014 | 1 | B | 54 | 54 |
1015 | 1 | B | 18 | 18 |
1016 | 1 | B | 0 | 0 |
1017 | 2 | B | 0 | 0 |
1017 | 2 | F | -21 | 0 |
1018 | 1 | B | 0 | 0 |
1019 | 1 | B | 5 | 5 |
1020 | 1 | B | -3 | 0 |
1021 | 1 | F | -1 | 0 |
1022 | 1 | B | 95 | 95 |
1023 | 1 | B | -20 | 0 |
1024 | 1 | B | 0 | 0 |
1025 | 2 | B | -65 | 10 |
1025 | 2 | F | 10 | 10 |
1026 | 2 | B | 24 | 24 |
1026 | 2 | F | -7 | 24 |
1027 | 2 | B | 0 | 0 |
1027 | 2 | F | 0 | 0 |
规则如下:
如果
CountCustomer = 1
那就很�
75e1
��易了。"如果 commission <= 0
则 commission2
为 0,否则等于 commission
如果
CountCustomer = 2
(表示客户有2个品牌),且其中一个佣金金额> 0,而另一个为< 0, then both commission2
,则值应为正数。如果两个佣金数字都高于 0,那么这些数字在 commission2
期间应保持不变。如果两个佣金都是 < 0 then commission2 should be 0 for both. You can understand more clear from bolded rows on the table above.
使用窗口函数查看每个客户的条目数量以及最小和最大佣金:
select
customerid,
count(*) over (partition by customerid) as countcustomer,
brand,
commission,
case
when count(*) over (partition by customerid) = 1 then
case when commission < 0 then 0 else commission end
else
case
when max(commission) (partition by customerid) <= 0 then
0
when min(commission) (partition by customerid) > 0 then
commission
else
max(commission) (partition by customerid)
end
end as commission2
from mytable
order by customerid, brand;
或者,由于多行条件也适用于单行客户,因此:
select
customerid,
count(*) over (partition by customerid) as countcustomer,
brand,
commission,
case
when max(commission) (partition by customerid) <= 0 then
0
when min(commission) (partition by customerid) > 0 then
commission
else
max(commission) (partition by customerid)
end as commission2
from mytable
order by customerid, brand;
我不认为这是你正在寻找的逻辑,但如果这是我的理解,那么 CASE WHEN 应该是这样的:
SELECT Customerid, CountCustomer, Brand, comission,
CASE WHEN CountCustomer = 1 AND commission > 0 THEN commision
WHEN CountCustomer = 1 AND commission < 0 THEN 0
WHEN CountCustomer = 2 AND commission > 0 THEN comission
WHEN CountCustomer = 2 AND commission < 0 THEN 0
END AS commission2
FROM YourTable
SELECT
Customerid
,CountCustomer
,Brand
,commission
CASE WHEN CountCustomer = 1 AND commission >= 0 THEN commission
WHEN CountCustomer = 2 AND commission > 0 AND commission2 < 0 THEN commission
WHEN CountCustomer = 2 AND commission < 0 AND commission2 > 0 THEN commission2
WHEN CountCustomer = 2 AND commission < 0 AND commission2 < 0 THEN 0
ELSE commission2 AS 'commission2'
FROM MyTable
我尽力听从你的解释。您可以根据需要修改这些语句。这应该按照您的需要返回列 commission2。
这将处理所提出的案例:
max(case when commission < 0 then 0 else commission end) over (partition by customerid)
如果这种情况可能的话,它将返回两个正值中较大的一个。