SQLServer复杂SQL逻辑实现

一、问题

如下图,已有表a与表b的数据,如何通过SQL语句变成表c?表a是UserName相同时,根据Month降序汇总Salary数据;表b是UserName对应SumSalary的上限值;表c是从表a中取出表b上限值以下的分录,若分录的SumSalary值大于b表的Salary,则拆分出刚好汇总等于b表的Salary数。

表A的SQL语句:

create table b(
  UserName nvarchar(200),Salary int
)
create table User_Salary (UserName nvarchar(200), Month nvarchar(20), Salary int)
go
insert into User_Salary (UserName,Month,Salary ) values('AAA','2010/12',1000)
insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/01',2000)

insert into User_Salary (UserName,Month,Salary ) values('AAA','2011/02',3000)

insert into User_Salary (UserName,Month,Salary ) values('BBB','2010/12',2000)

insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/01',2500)

insert into User_Salary (UserName,Month,Salary ) values('BBB','2011/02',2500)

insert into User_Salary (UserName,Month,Salary ) values('CCC','2013/12',5000)

insert into User_Salary (UserName,Month,Salary ) values('CCC','2013/04',4000)

insert into User_Salary (UserName,Month,Salary ) values('CCC','2013/02',3000)

二、SQL

SQLServer:

SELECT username,MONTH,Salary2 salary,SumSalary2 sumsalary FROM (
SELECT k.*,case when b.flag1=0 then -1 else b.flag1 end flag2
,case when B.flag1=1 and k.flag1<>1 then k.salary-(k.SumSalary-k.maxSalary) else k.salary end Salary2
,case when B.flag1=1 and k.flag1<>1 then k.maxSalary else k.SumSalary end SumSalary2   FROM
(SELECT A.*,row_number() OVER(order by UserName,month desc) r  FROM
(SELECT a.*,case when maxSalary>SumSalary then 1 when maxSalary=SumSalary then 0 else -1 end flag1 from(
select u.*
,(select SUM(salary) from User_Salary where User_Salary.Month>=u.month and User_Salary.UserName = u.userName) as SumSalary
,(select Salary from b where b.UserName = u.UserName) as maxSalary
from User_Salary u
) a )A
) k
LEFT JOIN
(SELECT A.*,row_number() OVER(order by UserName,month desc) r FROM
(SELECT a.*,case when maxSalary>SumSalary then 1 when maxSalary=SumSalary then 0 else -1 end flag1 from (
select u.*
,(select SUM(salary) from User_Salary where User_Salary.Month>=u.month and User_Salary.UserName = u.userName) as SumSalary
,(select Salary from b where b.UserName = u.UserName) as maxSalary
from User_Salary u
) a )A
) B
on k.R = B.R+1
) a WHERE FLAG1=1 OR FLAG1 = 0 OR FLAG2 = 1

三、总结

主要还是对上一行下一行记录的获取上,也就是以前写的SQLServer的lead,lag的实现。如果SQLServer2010(好像是这版本),就有内置的lead,lag了,sql就简单了。

个人觉得这类SQL当做练手是不错的,但用着应用中复杂度就有点高了,应对对应用的业务和表结构做重新处理。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 03:27:01

SQLServer复杂SQL逻辑实现的相关文章

python 3 mysql sql逻辑查询语句执行顺序

python 3 mysql sql逻辑查询语句执行顺序 一 .SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <havin

稍微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出

项目中有一个业务需求是:默认加载15条历史记录(按时间顺序从早到晚). 下面是我构造的sql逻辑,mark一下,亲测可行. SELECT * FROM (SELECT *FROM group_chatmsg_v WHERE ((group_Id=46 AND send_user_id=28 AND receive_user_id=70) OR (group_Id=46 AND receive_user_id=28 AND STATUS=1)) AND is_delete =0 ORDER BY

【转】sqlserver使用sql导出索引

转自:http://blog.itpub.net/16436858/viewspace-609341/ BEGIN WITH tx AS ( SELECT a.object_id ,b.name AS schema_name ,a.name AS table_name ,c.name as ix_name ,c.is_unique AS ix_unique ,c.type_desc AS ix_type_desc ,d.index_column_id ,d.is_included_column

略微复杂的sql逻辑(从数据库逆序查找有限条记录(limit))并按相反顺序输出

项目中有一个业务需求是:默认载入15条历史记录(按时间顺序从早到晚). 以下是我构造的sql逻辑,mark一下,亲測可行. SELECT * FROM (SELECT *FROM group_chatmsg_v WHERE ((group_Id=46 AND send_user_id=28 AND receive_user_id=70) OR (group_Id=46 AND receive_user_id=28 AND STATUS=1)) AND is_delete =0 ORDER BY

45、SQL逻辑查询语句执行顺序

一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition> ORDER BY <o

SQL逻辑读变成零

使用缓存HINT 让逻辑读变成0. create table t as select * from dba_objects; insert into t select * from t; commit; set autotrace on set timing on set linesize 1000 select /*+ result_cache */ count(*) from t; ---接下来再次执行(居然发现逻辑读为0): set autotrace on select /*+ resu

mysql五补充部分:SQL逻辑查询语句执行顺序

阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition>

mysql第四篇--SQL逻辑查询语句执行顺序

mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN <right_table> ON <join_condition> WHERE <where_condition> GROUP BY <group_by_list> HAVING <having_condition&g

SQL 逻辑优化 case when 转为 union all

通常数据库的优化从硬件层面去考虑可分为4个方面: CPU:即降低计算复杂度,如减少sql各类聚合函数,窗口函数,case when等. IO :(较少查询结果集过程中对数据的访问量.数据优化很大程度从这里入手 网络 : 较少查询结果集的大小,去除不必要的查询字段 数据库资源  : 这里讲的数据库资源主要是数据的一些参数设置,如索引.数据缓存.锁的争用,死锁,锁等. 锁问题大部分从业务逻辑上去优化.如拆分事务,降低事务复杂度及事务中的表关联.做到少量多次提交.即让事务尽快完成,释放资源.另一方面,