T-sql:当因子的数量有时会不同时,计算因子之和
T-sql: calculate sum of factors when quantity of factors can differ from time to time
你能帮我开发计算客户评级的算法吗?
初始数据集和理想结果在下面的代码中。谢谢。
逻辑:
我们有客户和 6 个因素(值为 1 或 0(存在或不存在))。
我们应该计算客户的评分:
1(最大速率)- 客户拥有所有因素
2 - 客户有因子 1-5 而没有因子 6
3 - 客户有因子 1-4 而没有因子 5(因子 6 无关紧要)
4 - 客户有因素 1-3 而没有因素 4(因素 5-6 无关紧要)
5 - 客户有因素 1-2 而没有因素 3(因素 4-6 无关紧要)
6 - 客户有因素 1 而没有因素 2(因素 3-6 无关紧要)
7 - 客户没有因素 1(因素 2-6 无关紧要)
关键是因素的数量会不时发生变化。
drop table if exists #tmp;
create TABLE #tmp (
[client] [nvarchar] null,
[factor1] [int] NULL,
[factor2] [int] NULL,
[factor3] [int] NULL,
[factor4] [int] NULL,
[factor5] [int] NULL,
[factor6] [int] null,
[desirable_result] [int] NULL
)
insert into #tmp (
[client]
,[factor1]
,[factor2]
,[factor3]
,[factor4]
,[factor5]
,[factor6]
,[desirable_result]
)
select '1', 1,1,1,1,1,1,1 union all
select '2', 1,1,0,1,1,1,5 union all
select '3', 1,0,1,1,0,1,6 union all
select '4', 1,1,1,1,1,0,2 union all
select '5', 1,1,1,0,0,1,4
此解决方案有效,但前提是因子数始终相等。
关键是因素的数量可能会不时发生变化。
select *
, "factor1" + "factor2" + "factor3" + "factor4" + "factor5" + "factor6" sum_6
, "factor1" + "factor2" + "factor3" + "factor4" + "factor5" sum_5
, "factor1" + "factor2" + "factor3" + "factor4" sum_4
, "factor1" + "factor2" + "factor3" sum_3
, "factor1" + "factor2" sum_2
, "factor1" sum_1
into #tmp2
from #tmp
select *
, case when sum_6 = 6 then 1 else
(case when sum_5 = 5 and sum_6 < 6 then 2 else
(case when sum_4 = 4 and sum_5 < 5 then 3 else
(case when sum_3 = 3 and sum_4 < 4 then 4 else
(case when sum_2 = 2 and sum_3 < 3 then 5 else
(case when sum_1 = 1 and sum_2 < 2 then 6 else
7
end)
end)
end)
end)
end)
end rate
from
#tmp2
您可以尝试使用 CASE WHEN
select case
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
AND [factor5] = 1
AND [factor6] = 1
then 'ALL6'
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
AND [factor5] = 1
then 'FIRST5'
.....
....
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
then 'FIRST4'
when [factor1] = 1
then 'ONLy1' END client_rate
from my_table
用缺少的条件填入点....
您可以像 scaisEdge 演示的那样使用 CASE WHEN ...
。
我这里的是 UNPIVOT
table 使用 CROSS APPLY
然后使用 SUM()
和 CASE
来锻炼必要的逻辑
select t.client,
t.[desirable_result],
case when sum(f.fval) = 6 then 1
when sum(f.fval) = 5
and sum(case when f.fno = 6 then f.fval end) = 0 then 2
when sum(case when f.fno <= 4 then f.fval end) = 4
and sum(case when f.fno = 5 then f.fval end) = 0 then 3
when sum(case when f.fno <= 3 then f.fval end) = 3
and sum(case when f.fno = 4 then f.fval end) = 0 then 4
when sum(case when f.fno <= 2 then f.fval end) = 2
and sum(case when f.fno = 3 then f.fval end) = 0 then 5
when sum(case when f.fno = 1 then f.fval end) = 1
and sum(case when f.fno = 2 then f.fval end) = 0 then 6
when sum(case when f.fno = 1 then f.fval end) = 0 then 7
end
from #tmp t
cross apply
(
values
(1, factor1),
(2, factor2),
(3, factor3),
(4, factor4),
(5, factor5),
(6, factor6)
) f (fno, fval)
group by t.client, t.[desirable_result]
order by t.client
你能帮我开发计算客户评级的算法吗? 初始数据集和理想结果在下面的代码中。谢谢。
逻辑: 我们有客户和 6 个因素(值为 1 或 0(存在或不存在))。
我们应该计算客户的评分:
1(最大速率)- 客户拥有所有因素
2 - 客户有因子 1-5 而没有因子 6
3 - 客户有因子 1-4 而没有因子 5(因子 6 无关紧要)
4 - 客户有因素 1-3 而没有因素 4(因素 5-6 无关紧要)
5 - 客户有因素 1-2 而没有因素 3(因素 4-6 无关紧要)
6 - 客户有因素 1 而没有因素 2(因素 3-6 无关紧要)
7 - 客户没有因素 1(因素 2-6 无关紧要)
关键是因素的数量会不时发生变化。
drop table if exists #tmp;
create TABLE #tmp (
[client] [nvarchar] null,
[factor1] [int] NULL,
[factor2] [int] NULL,
[factor3] [int] NULL,
[factor4] [int] NULL,
[factor5] [int] NULL,
[factor6] [int] null,
[desirable_result] [int] NULL
)
insert into #tmp (
[client]
,[factor1]
,[factor2]
,[factor3]
,[factor4]
,[factor5]
,[factor6]
,[desirable_result]
)
select '1', 1,1,1,1,1,1,1 union all
select '2', 1,1,0,1,1,1,5 union all
select '3', 1,0,1,1,0,1,6 union all
select '4', 1,1,1,1,1,0,2 union all
select '5', 1,1,1,0,0,1,4
此解决方案有效,但前提是因子数始终相等。 关键是因素的数量可能会不时发生变化。
select *
, "factor1" + "factor2" + "factor3" + "factor4" + "factor5" + "factor6" sum_6
, "factor1" + "factor2" + "factor3" + "factor4" + "factor5" sum_5
, "factor1" + "factor2" + "factor3" + "factor4" sum_4
, "factor1" + "factor2" + "factor3" sum_3
, "factor1" + "factor2" sum_2
, "factor1" sum_1
into #tmp2
from #tmp
select *
, case when sum_6 = 6 then 1 else
(case when sum_5 = 5 and sum_6 < 6 then 2 else
(case when sum_4 = 4 and sum_5 < 5 then 3 else
(case when sum_3 = 3 and sum_4 < 4 then 4 else
(case when sum_2 = 2 and sum_3 < 3 then 5 else
(case when sum_1 = 1 and sum_2 < 2 then 6 else
7
end)
end)
end)
end)
end)
end rate
from
#tmp2
您可以尝试使用 CASE WHEN
select case
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
AND [factor5] = 1
AND [factor6] = 1
then 'ALL6'
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
AND [factor5] = 1
then 'FIRST5'
.....
....
when [factor1] = 1
AND [factor2] = 1
AND [factor3] = 1
AND [factor4] = 1
then 'FIRST4'
when [factor1] = 1
then 'ONLy1' END client_rate
from my_table
用缺少的条件填入点....
您可以像 scaisEdge 演示的那样使用 CASE WHEN ...
。
我这里的是 UNPIVOT
table 使用 CROSS APPLY
然后使用 SUM()
和 CASE
来锻炼必要的逻辑
select t.client,
t.[desirable_result],
case when sum(f.fval) = 6 then 1
when sum(f.fval) = 5
and sum(case when f.fno = 6 then f.fval end) = 0 then 2
when sum(case when f.fno <= 4 then f.fval end) = 4
and sum(case when f.fno = 5 then f.fval end) = 0 then 3
when sum(case when f.fno <= 3 then f.fval end) = 3
and sum(case when f.fno = 4 then f.fval end) = 0 then 4
when sum(case when f.fno <= 2 then f.fval end) = 2
and sum(case when f.fno = 3 then f.fval end) = 0 then 5
when sum(case when f.fno = 1 then f.fval end) = 1
and sum(case when f.fno = 2 then f.fval end) = 0 then 6
when sum(case when f.fno = 1 then f.fval end) = 0 then 7
end
from #tmp t
cross apply
(
values
(1, factor1),
(2, factor2),
(3, factor3),
(4, factor4),
(5, factor5),
(6, factor6)
) f (fno, fval)
group by t.client, t.[desirable_result]
order by t.client