博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
绕开在包含外部引用的被聚合表达式中指定了多个列
阅读量:4162 次
发布时间:2019-05-26

本文共 2098 字,大约阅读时间需要 6 分钟。

今天在做一个统计,其中有一个小小的需求在实现的时候小小的坑了一把

对于一个表,统计指定条件的数据有多少条,其中一个条件是经过cross apply计算出来的

那么,上一个小例子看看

declare @tb table(id int identity,dt date)declare @i intset @i=0-- 随机扔100个日期进临时表while @i<100    begin        insert into @tb(dt) select dateadd(d,-rand()*2000,getdate())        select @i = @i + 1    endselect * from (    -- 获得最小日期    select min(dt) as start from @tb) across apply (    -- 根据最小日期获得当前自然年对应的开始日期    select (case when dateadd(year,datediff(year,start,getdate()),start)>convert(date,getdate()) then dateadd(year,datediff(year,start,getdate())-1,start) else dateadd(year,datediff(year,start,getdate()),start) end) as current_year_start) bcross apply (    -- 获得统计数据    select count(0) as cnt    -- 获得所有日期中与当前日期差为偶数的数量    ,sum(case when datediff(d,dt,getdate())%2=0 then 1 else 0 end) as aa     -- 获取当前自然年的日期数量,提示“在包含外部引用的被聚合表达式中指定了多个列。如果被聚合的表达式包含外部引用,那么该外部引用就必须是该表达式中所引用的唯一的一列。”    ,sum(case when dt>b.current_year_start then 1 else 0 end) as cur    from @tb) c

在这个例子里,我想统计符合当前自然年的数据时,报错了,在包含外部引用的被聚合表达式中指定了多个列。如果被聚合的表达式包含外部引用,那么该外部引用就必须是该表达式中所引用的唯一的一列。

尽管,我们还可以单独再附加一个cross apply,但总觉得这样不太方便

突然灵光一闪,我们可以对统计的内容也做个cross apply啊,把我们需要的数据直接扔到统计里,直接参与不就好了?

变形一下指令成为

declare @tb table(id int identity,dt date)declare @i intset @i=0-- 随机扔100个日期进临时表while @i<100    begin        insert into @tb(dt) select dateadd(d,-rand()*2000,getdate())        select @i = @i + 1    endselect * from (    -- 获得最小日期    select min(dt) as start from @tb) across apply (    -- 获得统计数据    select count(0) as cnt    -- 获得所有日期中与当前日期差为偶数的数量    ,sum(case when datediff(d,dt,getdate())%2=0 then 1 else 0 end) as aa     -- 获取当前自然年的日期数量    ,sum(case when dt between current_year_start and getdate() then 1 else 0 end) as cur    from @tb x    cross apply (        -- 根据最小日期获得当前自然年对应的开始日期        select (case when dateadd(year,datediff(year,start,getdate()),start)>convert(date,getdate()) then dateadd(year,datediff(year,start,getdate())-1,start) else dateadd(year,datediff(year,start,getdate()),start) end) as current_year_start    ) y) c

嗯,结果一切正常了,脑袋有坑啊,开始为什么就没这么想呢?

在这种统计的需求里,我们可以直接把其他需要参与计算的列一起扔到统计的cross apply里,这样就不会再有引用其他表数据的问题出现了

转载地址:http://nxvxi.baihongyu.com/

你可能感兴趣的文章
优秀程序员的十个习惯
查看>>
一个老程序员对学弟学妹的一些忠告
查看>>
管理者:我不需要你喜欢我
查看>>
程序员赚钱致富的6种方法
查看>>
世界上的编程语言已这么丰富,为什么还不够?
查看>>
想变富?先学学有钱人的10种思维方式!
查看>>
CListCtrl 和 CListBox的设置选中问题
查看>>
什么时候需要定义拷贝构造函数
查看>>
管理时间是空话,集中精力吃青蛙
查看>>
五大绝招助你成为下属爱戴的好上司
查看>>
程序员需谨记的8条团队开发原则
查看>>
[项目管理]关于项目管理和项目计划制定的对话
查看>>
c++中两个类的头文件互相包含编译出错的解决办法
查看>>
创始人和VC推荐企业家读的21本书
查看>>
Android L的API变化
查看>>
程序员成长历程的四个阶段
查看>>
为什么程序员跟其他人比起来应该喝更多的水
查看>>
分布式与集群的区别
查看>>
分布式集群技术
查看>>
不少程序员都会碰到的三个面试题
查看>>