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 ...
随机推荐
- VS2017、netcore版本更新升级
VS2017 剩下的就是下一步了. netcore 访问:https://www.microsoft.com/net/download/archives 找到对应版本(最新版本) 下载安装就可以了 装 ...
- 217. Contains Duplicate【easy】
217. Contains Duplicate[easy] Given an array of integers, find if the array contains any duplicates. ...
- 基于AR9331(MIPS架构)分析系统启动过程(uboot)
前提: 1.AR9331是基于MIPS 24K CPU的一款WIFI1X1芯片,其SDK采用uboot作为引导.AR9331中定义的基地址是:0x9f00,0000 2.MIPS24K芯片,将固定的起 ...
- spark单机模式
1.下载spark,解压2.复制conf/spark-env.sh和conf/log4j.properties cp spark-env.sh.template spark-env.sh cp log ...
- css 定位标签设置格式
td a { color: #3c8dbc; } td a:hover { color: #00bdd8; } 上例即为定位td下的a标签.即用来给表格中的链接,未访问时和hove ...
- head&&tail
//參考<Linux shell脚本攻略 第2版> 1,head a)打印前10行: ubuntu@VM-62-13-ubuntu:~$ head file b)打印前5行: ubuntu ...
- SlidingMenu官方实例分析1——ExampleListActivity
1.SlidingMenuDemo下载: 由AndroidManifest.xml能看出项目是从ExampleListActivity启动的: ExampleListActivity继承了Sherlo ...
- linux命令之面试题1
1.请解释下列10个shell命令的用途 top:是linux下常用的性能分析工具,能够实时的显示系统中各个进程的资源占用情况,类似于windows的资源管理器,查看系统的cpu,内存,运行时间,交互 ...
- php 加入即时推送功能
打开浏览器保持与服务器握手的websocket 之前用workerman接过很花时间,现在workerman对其代码做了优化->https://www.workerman.net/web-sen ...
- 网易2016研发project师编程题
http://www.nowcoder.com/test/970447/summary [编程题] 小易的升级之路 小易常常沉迷于网络游戏.有一次,他在玩一个打怪升级的游戏,他的角色的初始能力值为 a ...