T-SQL利用笛卡尔积累计、累加
T-SQL利用笛卡尔积/窗口函数/表连接累计、累加
【1】 笛卡尔积与子查询解决累计
方法1:笛卡尔积
--原始数据 select templateid,needitem1Count from db_tank..TS_CardMain --累计数据 select t1.templateId,t1.needitem1Count,sum(t2.needitem1count) sum_num from db_tank..TS_CardMain t1
cross join db_tank..TS_CardMain t2
where t2.templateid <= t1.templateid
group by t1.templateid,t1.needitem1Count

方法2:子查询
select templateid,needitem1Count,
(select sum(needitem1Count) from db_tank..TS_CardMain t2 where t2.templateid<=t1.templateid ) as sum_num
from db_tank..TS_CardMain t1
【2】解决分组累加问题:利用表连接、笛卡尔积、子查询
基于多个分组的分别累加
方法1:笛卡尔积
;with temp1 as (
select 1 as id ,1 as num
union all
select 1 as id ,2 as num
union all
select 1 as id ,3 as num
union all
select 2 as id ,4 as num
union all
select 2 as id ,5 as num
union all
select 2 as id ,6 as num
)
select t1.id,t1.num,sum(t2.num) sum_num from temp1 t1 join temp1 t2 on t2.id =t1.id AND t2.num <= t1.num
group by t1.id,t1.num
order by id

解法2:利用子查询
;with temp1 as (
select 1 as id ,1 as num
union all
select 1 as id ,2 as num
union all
select 1 as id ,3 as num
union all
select 2 as id ,4 as num
union all
select 2 as id ,5 as num
union all
select 2 as id ,6 as num
)
select *,(select sum(num) from temp1 where id=t.id and num <= t.num) sum_num from temp1 t

【3】窗口函数_分析函数(sum over)
sql server 2012及以上可用
rows between unbounded preceding and current row
--【3.1】利用sum() over()嵌套使用
;with temp1 as (
select 1 as id ,1 as num union all
select 1 as id ,2 as num union all
select 1 as id ,3 as num union all
select 2 as id ,4 as num union all
select 2 as id ,5 as num union all
select 2 as id ,6 as num
)
select *,sum(num) over(partition by id order by num asc rows between unbounded preceding and current row) from temp1

T-SQL利用笛卡尔积累计、累加的更多相关文章
- SQL利用Case When Then多条件判断SQL 语句
http://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html SQL利用Case When Then多条件判断SQL ,用于sele ...
- SQL SERVER 雨量计累计雨量(小时)的统计思路
PLC中定时读取5分钟雨量值,如何将该值统计为小时雨量作为累计?在sql server group by聚合函数,轻松实现该目的. 1.编写思路 数据库中字段依据datetime每五分钟插入一条语句, ...
- 【SQL】SQL中笛卡尔积、内连接、外连接的数据演示
SQL的查询语句中,常使用到内连接.外连接,以及连接的基础--笛卡尔积运算. 在简单的SQL中,也许我们还分辨清楚数据如何连接,一旦查询复杂了,脑子也犯浆糊了,迷迷糊糊的. 本文,简单以数据形式记录连 ...
- SQL中笛卡尔积-cross join的用法
在数学中,笛卡尔乘积是指两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 假设集合A={a ...
- SQL利用Case When Then多条件判断
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 THEN 结果4 ....... ...
- SQL利用临时表实现动态列、动态添加列
--方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...
- (转)SQL利用Case When Then多条件判断
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 THEN 结果4 ....... ...
- SQL利用Case When Then多条件
CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 THEN 结果4......... ...
- SQL 对比,移动累计
数据对比 两种常用模型 1.对比下一行,判断增长.减少.维持现状 -- 建表 drop table sales create table sales( num int, soc int ); inse ...
随机推荐
- Ionic学习笔记1_基本布局
<body> <!-- 头部 --> bar里嵌入子元素:title,button,button-bar和 inpu ...
- 14. First Position of Target 【easy】
14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...
- Django学习之网站图标
首先,你要有一个.ico的文件,可以从easy icon直接搜索一个图标. 1.先导入RedirectView,是一个通用类视图. from django.views.generic.base imp ...
- java - day15 - NonameInner
匿名内部类 public interface Inter {} interface Inter2{ void show(); } main(){ //错误,接口不能实例化 Inter t = new ...
- JME的flyCam和cam的区别
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:camera 注意这句话: The flyCam class field gives ...
- oracle 数据查询
1,读取从今天到1个月前之间的数据select * from tablewhere column between add_months(sysdate, -1) and sysdate;
- GNU_MAKE--工程管理
GNU MAKE--工程管理 makefile是为工程组织编译,为“自动化编译”,一旦写成,只需要一个make命令,整个工程完全自动编译,极大提高了软件开发效率.make是一个命令工具,是一个解释ma ...
- scp -P 非22端口拷贝
scp 远程拷贝非22端口的服务器文件的方法:上传文件到服务器scp -P 29966 /Users/ianMac/Desktop/progit.zh.pdf root@远程ssh服务器的ip:/ho ...
- Oracle 查询并修改
update test1 a set a.name=(select b.name from test2 b where a.id=b.id) where a.id in (select id from ...
- java中.currentTimeMillis的用法和含义
用法:可以用法获取当前时间的毫秒数,可以通过毫秒数进行时间比较,时间转化以及时间格式化等.public class SystemTime {public static void main(String ...