Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按不同值对bigint列进行分组_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 按不同值对bigint列进行分组

Sql 按不同值对bigint列进行分组,sql,sql-server,tsql,Sql,Sql Server,Tsql,我在一家银行工作。考虑到保持平衡,我们计划根据以下规则为客户打分: Balance ---- Point(per 1000) 1000-5000 10 5000-10000 20 10000-20000 30 例如,一个人的账户中有20000,我们根据以下几点给他/她450分: (5*10) + (5*20)+ (10*30)=450 我们大约有300万客户需要计算他们的积分 我们应该如何使用T-SQL编写此查询?有多种方法。这里有一种方法: se

我在一家银行工作。考虑到保持平衡,我们计划根据以下规则为客户打分:

Balance    ----  Point(per 1000)
1000-5000        10
5000-10000       20
10000-20000      30
例如,一个人的账户中有20000,我们根据以下几点给他/她450分:

(5*10) + (5*20)+ (10*30)=450
我们大约有300万客户需要计算他们的积分


我们应该如何使用T-SQL编写此查询?

有多种方法。这里有一种方法:

select t.*,
       ((case when balance > 1000 and balance < 5000
              then (balance - 1000) * 10.0 / 1000
              else (5000 - 1000) * 10.0 / 1000
        ) +
        (case when balance > 5000 and balance < 10000
              then (balance - 5000) * 20.0 / 1000
              else (10000 - 5000) * 20.0 / 1000
        ) +
        (case when balance > 10000 and balance < 20000
              then (balance - 10000) * 30.0 / 1000
              else (20000 - 10000) * 30.0 / 1000
        )
       ) as points
from t 
选择t.*,
((天平>1000且天平<5000时的情况)
然后(余额-1000)*10.0/1000
其他(5000-1000)*10.0/1000
) +
(余额>5000且余额<10000时的情况)
然后(余额-5000)*20.0/1000
其他(10000-5000)*20.0/1000
) +
(余额>10000且余额<20000时的情况)
然后(余额-10000)*30.0/1000
其他(20000-10000)*30.0/1000
)
)作为要点
从t

一种解决方法如下所示。关键假设是范围没有差距。


我喜欢把我的层次放在一张桌子上。这将从代码中删除逻辑,并允许您在一个结构中支持多个层

示例

Declare @Tier table (TierGrp varchar(50),TierTitle varchar(50),TierR1 money,TierR2 money,Pnts int)
Insert Into @Tier values
 ('Sample','0 - 5'   ,0     ,5000   ,10)
,('Sample','5 - 10'  ,5000  ,10000  ,20)
,('Sample','10 - 20' ,10000 ,20000  ,30)
,('Sample','20 - 50' ,20000 ,50000  ,40)
,('Sample','50 - 100',50000 ,100000 ,50)
,('Sample','10+'     ,100000,9999999,60)

Declare @YourTable table (ID int,RemainBalance money)
Insert Into @YourTable values
 (1,20000)
,(2,15500)
,(3,30000)

Select A.ID
      ,A.RemainBalance
      ,Points = sum(floor(((IIF(RemainBalance>TierR2,TierR2,RemainBalance)-TierR1)/1000)) * Pnts )
 From  @YourTable A
 Join  @Tier      B on TierGrp='Sample' and RemainBalance >=TierR1
 Group By A.ID,A.RemainBalance
 Order By A.ID
返回

ID  RemainBalance   Points
1   20000.00        450.00
2   15500.00        300.00
3   30000.00        850.00
ID  RemainBalance   Points
1   20000.00        450.00
2   15500.00        300.00
3   30000.00        850.00