【问题标题】:T-sql: calculate sum of factors when quantity of factors can differ from time to timeT-sql:当因子的数量可能不时变化时计算因子的总和
【发布时间】:2021-02-19 06:35:50
【问题描述】:

请您帮我开发计算客户评分的算法。 初始数据集和理想结果在下面的代码中。谢谢。

逻辑: 我们有客户和 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

【问题讨论】:

  • 你使用的是MySQL还是SQL Server
  • 松鼠,我用的是 SQL Server 2017
  • 您可以使用CASE 完成您想要的操作。它会有点难看,但它会工作
  • 为什么只有factor 1 to 3 with 1client 5result = 3
  • 松鼠,感谢您的纠正和理解逻辑。客户 5 的结果应该是 4。我会在一分钟内更正我的帖子。

标签: sql tsql logic sql-server-2017


【解决方案1】:

您可以使用CASE WHEN ...,正如 scaisEdge 所演示的那样。

我在这里要做的是使用CROSS APPLYUNPIVOT 表,然后使用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

【讨论】:

  • 松鼠,你的代码运行良好,我会很乐意使用它。剩下的唯一一件事是:让它充满活力。输入值的格式总是相等的:客户端和一些名称为 factor1、factor2、factorN 的因子(可能是 5,可能是 25 个因子)。
  • 如果可以对#tmp 表进行规范化,那就容易多了。这就是CROSS APPLY 正在做的事情。它unpivot 并对其进行规范化
  • 松鼠,谢谢。由于没有太多使用经验,我将花一些时间学习“交叉应用”功能。
【解决方案2】:

您可以尝试使用 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 - 感谢您的解决方案。如果因子的数量始终相等,则效果很好。我已经开发了您的解决方案并将其发布在主题中。但是当因素的数量不时不同时, id 不起作用。拥有动态 SQL 会很棒。
  • @AlexIvanov 。 . . (1)这回答了您提出的问题(与其他答案一样)。你应该接受答案。 (2) SQL 表具有固定数量的列,表或您的问题都没有动态。 (3) 如果您有不同数量的因素的问题,您应该修复您的数据模型,因此因素存储在行中,而不是列中。 (4) 如果您有关于动态列数的问题,这是一个不同的问题,应作为问题提出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多