帮看一个SQL查询结果

funsuzhou 2010-05-31 09:01:29
/*
表里3条记录片段:
SORDER EDA BUN CUST NOUBA JITU0 APRICE
RR128254 0 1 CSZ003U CSZ004U 62.700 62.10000000
RR128254 0 2 CSZ003U CSZ004U 213.750 62.10000000
RR128254 0 3 CSZ003U CSZ004U 8.550 62.10000000
*/
select case when cust='chf001j'and nouba='chf001j' then ceiling(jitu0*aprice)
when right(cust,1)='j'and cust<>'chf001j'and nouba<>'chf001j'then round(jitu0*aprice,0)
else round(jitu0*aprice,2) end
from xract where sorder='RR128254'

--照理应出来 round(jitu0*aprice,2)的结果,但是却出来ceiling(jitu0*aprice)的结果,怎么回事?
...全文
123 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2010-05-31
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 rmljoe 的回复:]
不信你看

SQL code
print case when 1<>1 then round(100.00,2) else ceiling(200) end
print ceiling(200)
/*
200.00
200
*/


同样是打印ceiling(200),但第一种情况,它会根据CASE转成了非整型了
[/Quote]

嗯,以前也有讨论过这个级别转换的问题,不知道是哪个大虾整的啦
chuifengde 2010-05-31
  • 打赏
  • 举报
回复
你可以看看2005帮助中数据类型的优先级别
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
原因知道了,确实如rmljoe所说那样。我已强制转换成decimal(28,2)!
rmljoe 2010-05-31
  • 打赏
  • 举报
回复
不信你看
print case when 1<>1 then  round(100.00,2) else ceiling(200) end
print ceiling(200)
/*
200.00
200
*/


同样是打印ceiling(200),但第一种情况,它会根据CASE转成了非整型了
rmljoe 2010-05-31
  • 打赏
  • 举报
回复
你的结果要输出成一个字段,由于前两种结果都会使这个字段是个整型的,造成第三种结果保存成整型的了。
你试试:

select case
when cust='chf001j'and nouba='chf001j' then cast(ceiling(jitu0*aprice) as numeric(10,2))
when right(cust,1)='j'and cust<>'chf001j'and nouba<>'chf001j'then cast(round(jitu0*aprice,0) as numeric(10,2))
else cast(round(jitu0*aprice,2) as numeric(10,2)) end
from xract where sorder='RR128254'
chuifengde 2010-05-31
  • 打赏
  • 举报
回复
无论是2000,2008都没遇到这个问题
SQL77 2010-05-31
  • 打赏
  • 举报
回复
别用ROUND函数截取,用DEC 或 NUMERIC来,上次忘了有讨论过ROUND函数关于版本的不同,
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
各位老大帮忙啊,急,这是否是Microsoft 的BUG啊?
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) Feb 9 2007 22:47:07 Copyright (c) 1988-2005 Microsoft Corporation Standard Edition on Windows NT 6.0 (Build 6002: Service Pack 2)
sych888 2010-05-31
  • 打赏
  • 举报
回复
学习.........
notfoundme 2010-05-31
  • 打赏
  • 举报
回复
执行语句后面用()包起来试试
chuifengde 2010-05-31
  • 打赏
  • 举报
回复
看版本和补丁
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
怎么这么奇怪,真是晕死。
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
select
case when cust='chf001j'and nouba='chf001j' then 'a'
when right(cust,1)='j'and cust<>'chf001j'and nouba<>'chf001j'then 'b'
else 'c' end,
* from xract where sorder='RR128254'

出来是c

但用
select case when cust='chf001j'and nouba='chf001j' then ceiling(jitu0*aprice)
when right(cust,1)='j'and cust<>'chf001j'and nouba<>'chf001j'then round(jitu0*aprice,0)
else round(jitu0*aprice,2) end
from xract where sorder='RR128254'
出来就是
3894
13274
531
funsuzhou 2010-05-31
  • 打赏
  • 举报
回复
to 2楼,我试过了,用字符串得到的结果是对的,但一用正式的语句就不行了。见鬼。
Andy__Huang 2010-05-31
  • 打赏
  • 举报
回复
你可以这样测试,就知道它读取的是哪个值

select case when cust='chf001j'and nouba='chf001j' then 'a' when right(cust,1)='j'and cust<>'chf001j'and nouba<>'chf001j'then 'b'
else 'c' end
from xract where sorder='RR128254'


出来应该是c
chuifengde 2010-05-31
  • 打赏
  • 举报
回复
为了正确说明你得到的结果,将then 后都用字符串试试比如 then 'a' then 'b' else 'c'
幸运的意外 2010-05-31
  • 打赏
  • 举报
回复
有点蹊跷,不知道啥原因。我在我机器上没问题,出来正确结果而来。

34,590

社区成员

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

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