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利用笛卡尔积累计、累加的更多相关文章

  1. SQL利用Case When Then多条件判断SQL 语句

    http://www.cnblogs.com/kevin2013/archive/2010/07/02/1769682.html SQL利用Case When Then多条件判断SQL ,用于sele ...

  2. SQL SERVER 雨量计累计雨量(小时)的统计思路

    PLC中定时读取5分钟雨量值,如何将该值统计为小时雨量作为累计?在sql server group by聚合函数,轻松实现该目的. 1.编写思路 数据库中字段依据datetime每五分钟插入一条语句, ...

  3. 【SQL】SQL中笛卡尔积、内连接、外连接的数据演示

    SQL的查询语句中,常使用到内连接.外连接,以及连接的基础--笛卡尔积运算. 在简单的SQL中,也许我们还分辨清楚数据如何连接,一旦查询复杂了,脑子也犯浆糊了,迷迷糊糊的. 本文,简单以数据形式记录连 ...

  4. SQL中笛卡尔积-cross join的用法

    在数学中,笛卡尔乘积是指两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 假设集合A={a ...

  5. SQL利用Case When Then多条件判断

    CASE     WHEN 条件1 THEN 结果1     WHEN 条件2 THEN 结果2     WHEN 条件3 THEN 结果3     WHEN 条件4 THEN 结果4 ....... ...

  6. SQL利用临时表实现动态列、动态添加列

    --方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...

  7. (转)SQL利用Case When Then多条件判断

    CASE     WHEN 条件1 THEN 结果1     WHEN 条件2 THEN 结果2     WHEN 条件3 THEN 结果3     WHEN 条件4 THEN 结果4 ....... ...

  8. SQL利用Case When Then多条件

    CASE    WHEN 条件1 THEN 结果1    WHEN 条件2 THEN 结果2    WHEN 条件3 THEN 结果3    WHEN 条件4 THEN 结果4.........    ...

  9. SQL 对比,移动累计

    数据对比 两种常用模型 1.对比下一行,判断增长.减少.维持现状 -- 建表 drop table sales create table sales( num int, soc int ); inse ...

随机推荐

  1. git服务器gitlab之搭建和使用

    git服务器比较有名的是gitosis和gitolite,这两个管理和使用起来稍微有些复杂,没有web页面,而gitlab则是类似于github的一个工具,github无法免费建立私有仓库,并且为了代 ...

  2. DSP6455的cmd文件

    DSP6455的cmd文件 CMD 的专业名称叫链接器配置文件,存放链接器的配置信息,DSP编译器的编译结果是未定位的,DSP也没有操作系统来定位执行代码,DSP系统的配置需求也不尽相同,因此需要定义 ...

  3. Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Errcode: 17)

    这个错误时在Mysql主从配置产生的,最后找到这个Mysql的一个bug http://bugs.mysql.com/bug.php?id=62055 bug的主要原因是:打开文件的函数中指定打开模式 ...

  4. Chrome插件之一键保存网页为PDF1.1发布

    最新版本:V1.1 下载地址:http://download.csdn.net/detail/bdstjk/5722317 http://pan.baidu.com/share/link?sharei ...

  5. apache gzip

    1.首先启动模块mod_deflate.so和mod_headers.so 2.httpd.conf #文件缓存时间配置<FilesMatch ".(flv|gif|jpg|jpeg| ...

  6. 关于LNMP服务器 Thinkphp5验证码不显示问题

    关于LNMP服务器 Thinkphp5验证码不显示问题   浏览:246 发布日期:2017/09/20 分类:ThinkPHP5专区 关键字: thinkphp验证码不显示 nginx下验证码不显示 ...

  7. OC 基础语法

    :Obect c 与 c 语言的区别 () 后缀名不一样,C语言是.c 结尾 ,OC 是 .h结尾. () 输出信息不同 C语言是用print() 输出,OC 是用NSLog输出. () NSLog会 ...

  8. Easyui Datagrid扩展fixRownumber方法

    首先,从datagrid生成的代码,我们可以发现,在rowNumber上都有特定的class标记,datagrid-cell-rownumber,datagrid-header-rownumber. ...

  9. Linux 超级守护进程 xinetd

    在Linux中,守护进程有两种方式,一种是svsy方式,一种是xinetd方式(超级守护进程). 每个守护进程都会有一个脚本,可以理解成工作配置文件,守护进程的脚本需要放在指定位置,独立启动守护进程: ...

  10. SlidingMenu官方实例分析3——PropertiesActivity

    PropertiesActivity此类主要是对SlidingMenu设置的一些展示,也是为了使用者能快速的掌握SlidingMenu 的特点. 首先获得SlidingMenu对象: SlidingM ...