sum()函数中使用排序语句的怪现象

allanli 2011-07-19 03:06:35
在这边重新开个贴,基础性的东西,呵呵
原帖请看:http://topic.csdn.net/u/20110718/13/7267b9b0-76e2-40b9-9c9f-3d11db85a9d9.html?99610

我们没有装2000以上版本的SQL Server,谁有的可以测试一下下面语句:


select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd

select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode
go

drop table #DayPrice

/******************************************(2000是这样的结果)
companycode DayCount Price3
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2

--------------------------
--我希望的应该是返回(2008返回这样的结果)
companycode DayCount Price3
600001 5 46.2
600002 5 76.2

...全文
223 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
的确很奇怪,关注一下
把where aa.companycode=a.companycode注释掉呢?
allanli 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cd731107 的回复:]

确实如楼主所言,2000里面是很难,改了一下就能用了,看下面
SQL code
select a.companycode,(select sum(PriceEnd) from #DayPrice aa where companycode=a.companycode
and PriceDate in (select top 3 PriceDate from #DayPrice bb
whe……
[/Quote]

终于找到一个可以实现我要求的2000的写法了,呵呵
再等等大家的讨论,没有更好结果的结贴给分
cd731107 2011-07-20
  • 打赏
  • 举报
回复
不是在9楼已经写好了吗,测试过的
allanli 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 acherat 的回复:]

SQL code

select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '6……
[/Quote]
你确认是在sql server 2000中执行返回这样的结果吗?
我是返回第一种结果的
allanli 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 huang_yungui0515 的回复:]

字段再套一个SUM。 2楼应该是正确的
select a.companycode,count(a.PriceDate) as DayCount
,sum(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) as Price3
from #……
[/Quote]

这样写法在2000没法通过语法检测,不能这样写的
yinhaichao2008 2011-07-19
  • 打赏
  • 举报
回复
sql2005执行结果如下
600001 5 46.2
600002 5 76.2
Well 2011-07-19
  • 打赏
  • 举报
回复
检查执行计划。。。
yubofighting 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 acherat 的回复:]
SQL code

--2000

select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union……
[/Quote]

奇怪的是加distinct还是一样。。。。
右脚小拇指 2011-07-19
  • 打赏
  • 举报
回复
字段再套一个SUM。 2楼应该是正确的
select a.companycode,count(a.PriceDate) as DayCount
,sum(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) as Price3
from #DayPrice as a
group by a.companycode
--小F-- 2011-07-19
  • 打赏
  • 举报
回复
没测试环境 进来围观
oO寒枫Oo 2011-07-19
  • 打赏
  • 举报
回复
多谢11
cd731107 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lxpbs8851 的回复:]
谁帮忙试试5楼的 呵呵。
[/Quote]
服务器: 消息 207,级别 16,状态 3,行 1
列名 'companycode' 无效。
oO寒枫Oo 2011-07-19
  • 打赏
  • 举报
回复
谁帮忙试试5楼的 呵呵。
cd731107 2011-07-19
  • 打赏
  • 举报
回复
确实如楼主所言,2000里面是很难,改了一下就能用了,看下面
select a.companycode,(select sum(PriceEnd) from #DayPrice aa where companycode=a.companycode 
and PriceDate in (select top 3 PriceDate from #DayPrice bb
where companycode=a.companycode order by PriceDate desc))
from #DayPrice as a
group by a.companycode
AcHerat 元老 2011-07-19
  • 打赏
  • 举报
回复

--2000

select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd
go

select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode

drop table #DayPrice

/***********

companycode DayCount Price3
----------- ----------- ----------------------------------------
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2


楼主查询加个distinct吧!
pt1314917 2011-07-19
  • 打赏
  • 举报
回复
结果没问题噢。
yubofighting 2011-07-19
  • 打赏
  • 举报
回复
是否跟排序规则有关,等高人解答
oO寒枫Oo 2011-07-19
  • 打赏
  • 举报
回复

select a.companycode,
(select count(1) from #DayPrice b where a.companycode=b.companycode) as DayCount,
(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice b where a.companycode=b.companycode order by PriceDate desc) c
) Price3
from (select distinct companycode from #DayPrice) a
P1mm 2011-07-19
  • 打赏
  • 举报
回复
Try:


order by PriceDate desc
-->

order by aa.PriceDate desc
AcHerat 元老 2011-07-19
  • 打赏
  • 举报
回复

select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd
go

select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode
go

drop table #DayPrice

/*********

companycode DayCount Price3
----------- ----------- ---------------------------------------
600001 5 46.2
600002 5 76.2

(2 行受影响)
加载更多回复(2)
软件编程规范培训实例与练习 软件编程规范培训实例与练习  问题分类 1 逻辑类问题(A类)-指设计、编码出现的计算正确性和一致性、程序逻辑控制等方面出现的问题,在系统起关键作用,将导致软件死机、功能正常实现等严重问题; 接口类问题(B类)-指设计、编码出现的函数和环境、其他函数、全局/局部变量或数据变量之间的数据/控制传输不匹配的问题,在系统起重要作用,将导致模块间配合失效等严重问题; 维护类问题(C类)-指设计、编码出现的对软件系统的维护方便程度造成影响的问题,在系统不起关键作用,但对系统后期维护造成不便或导致维护费用上升; 可测试性问题(D类)-指设计、编码因考虑不周而导致后期系统可测试性差的问题。  处罚办法 问题发生率: P=D/S D=DA+0.5DB+0.25DC 其: P -问题发生率 D -1个季度内错误总数 DA -1个季度内A类错误总数 DB -1个季度内B类错误总数 DC -1个季度内C类错误总数 S -1个季度内收到问题报告单总数 1)当D≥3时,如果P≥3%,将进行警告处理,并予以公告; 2)当D≥5时,如果P≥5%,将进行罚款处理,并予以公告。 目 录 一、逻辑类代码问题 第5页 1、变量/指针在使用前就必须初始化 第5页 【案例1.1.1】 第5页 2、防止指针/数组操作越界 第5页 【案例1.2.1】 第5页 【案例1.2.2】 第6页 【案例1.2.3】 第7页 【案例1.2.4】 第8页 3、避免指针的非法引用 第9页 【案例1.3.1】 第9页 4、变量类型定义错误 第10页 【案例1.4.1】 第10页 5、正确使用逻辑与&&、屏蔽&操作符 第17页 【案例1.5.1】 第17页 6、注意数据类型的匹配 第18页 【案例1.6.1】 第18页 【案例1.6.2】 第18页 7、用于控制条件转移的表达式及取值范围是否书写正确 第20页 【案例1.7.1】 第20页 【案例1.7.2】 第21页 【案例1.7.3】 第22页 8、条件分支处理是否有遗漏 第24页 【案例1.8.1】 第24页 9、引用已释放的资源 第26页 【案例1.9.1】 第26页 10、分配资源是否已正确释放 第28页 【案例1.10.1】 第28页 【案例1.10.2】 第29页 【案例1.10.3】 第30页 【案例1.10.4】 第32页 【案例1.10.5】 第33页 【案例1.10.6】 第35页 【案例1.10.7】 第38页 11、防止资源的重复释放 第39页 【案例1.11.1】 第39页 12、公共资源的互斥性和竞用性 第40页 【案例1.12.1】 第40页 【案例1.12.2】 第40页 二、接口类代码问题 第43页 1、对函数参数进行有效性检查 第43页 【案例2.1.1】 第43页 【案例2.1.2】 第43页 【案例2.1.3】 第44页 【案例2.1.4】 第46页 【案例2.1.5】 第47页 【案例2.1.6】 第48页 2、注意多出口函数的处理 第49页 【案例2.2.1】 第49页 三、维护类代码问题 第51页 1、 统一枚举类型的使用 第51页 【案例3.1.1】 第51页 2、 注释量至少占代码总量的20% 第51页 【案例3.2.1】对XXX产品BAM某版本部分代码注释量的统计 第51页 四、产品兼容性问题 第52页 1、系统配置、命令方式 第52页 【案例4.1.1】 第52页 【案例4.1.2】 第53页 2、设备对接 第54页 【案例4.2.1】 第54页 3、其他 第55页 【案例4.3.1】 第55页 五、版本控制问题 第58页 1、新老代码同一全局变量不一致 第58页 【案例5.1.1】 第58页 六、可测试性代码问题 第59页 1、调试信息/打印信息的正确性 第59页 【案例6.1.1】 第59页 一、逻辑类代码问题 1、变量/指针在使用前就必须初始化 【案例1.1.1】 C语言最大的特色就是指针。指针的使用具有很强的技巧性和灵活性,但同时也带来了很大的危险性。在XXX的代码有如下一端对指针的灵活使用: ... ... _UC *puc_card_config_tab; ... ... Get_Config_Table(

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧