微软BI开拓者数据库专区SQL Server管理 嵌套查询使用Group By出现数据异常问题

1  /  1  页   1 跳转 查看:4356

嵌套查询使用Group By出现数据异常问题

嵌套查询使用Group By出现数据异常问题

SQL Server2000中Group By产生的怪异问题
先把产生错误数据的代码写出来如下:
select convert(char(10),getdate()-1,120)as reportDate,groupID,serverID,ItemID,price,sum(quantity)as quantity,sum(total)as total,type
from(select groupID, serverID,convert(int,dbo.split(convert(char(100),memo),',',1))as ItemID,
convert(int,dbo.split(convert(char(100),memo),',',2))as price,
convert(int,dbo.split(convert(char(100),memo),',',3)) as quantity,
convert(int,dbo.split(convert(char(100),memo),',',4)) as total,
-- convert(char(10),dbo.split(convert(char(100),memo),',',5))as type
--因结构发生变化所以进行了修改20070629
Case when convert(bit,dbo.split(convert(char(100),memo),',',5))=0 then '银币'
when convert(bit,dbo.split(convert(char(100),memo),',',5))=1 then '金币'
else '非法'
End as type
from Temp_CCXY_Event
where event_type=17
)as AA
group by groupID,serverID,ItemID,price,type
order by ItemID

产生的数据会导致:
2007-07-01 1 1 1 1 1 3 银币
2007-07-01 1 1 2 2 5 75 银币
2007-07-01 1 1 3 3 5 225 银币
2007-07-01 1 1 16 16 105 315 银币
2007-07-01 1 1 17 17 2 30 银币
2007-07-01 1 1 19 19 1 100 银币
2007-07-01 1 1 31 31 1 3 银币
问题:列ItemID 与price 所有值都一样,而实际上他们的值是不同的


正确的代码:

select convert(char(10),getdate()-1,120)as reportDate,groupID,serverID,ItemID,price,sum(quantity)as quantity,sum(total)as total,type
from(select groupID, serverID,convert(int,dbo.split(convert(char(100),memo),',',1))as ItemID,
convert(smallint,dbo.split(convert(char(100),memo),',',2))as price,
convert(int,dbo.split(convert(char(100),memo),',',3)) as quantity,
convert(int,dbo.split(convert(char(100),memo),',',4)) as total,
-- convert(char(10),dbo.split(convert(char(100),memo),',',5))as type
--因结构发生变化所以进行了修改20070629
Case when convert(bit,dbo.split(convert(char(100),memo),',',5))=0 then '银币'
when convert(bit,dbo.split(convert(char(100),memo),',',5))=1 then '金币'
else '非法'
End as type
from Temp_CCXY_Event
where event_type=17
)as AA
group by groupID,serverID,ItemID,price,type
order by ItemID
产生的数据:
2007-07-01 1 1 1 3 1 3 银币
2007-07-01 1 1 2 15 5 75 银币
2007-07-01 1 1 3 45 5 225 银币
2007-07-01 1 1 16 3 105 315 银币
2007-07-01 1 1 17 15 2 30 银币

这样的数据就是对...他们所读取的原始数据是相同的..
只是我把ItemID 与price 的类型改为不同了..
ItemID 转换成 int
而 price 转换成 int 就出现数据异常,转换出smallint就没问题了.本人多这个问题感到很奇怪,..虽然我早已经数据异常的问题,但是本人希望找我造成这样出错出现的真正原因.....请各位高人告诉下小弟....是我代码的问题..还是微软的BUG,谢谢
 

测试的环境

CREATE function [dbo].[split](@str varchar(2000),@split char(1),@I smallint)
returns varchar(20)
As
Begin
declare @str1 varchar(2000)
declare @str2 varchar(20)
declare @str3 varchar(2000)

declare @pos smallint
declare @num smallint

set @str1=@str
set @num=@I

while @num>0
Begin
set @pos=charindex(@split,@str1,1)
if (@pos=0)
Begin
set @str2=@str1
End
else
Begin
if (@num<>1)
Begin
set @str3=stuff(@str1,1,@pos,'')
set @str1=Ltrim(@str3)
End
else
Begin
set @str3=Left(@str1,@pos-1)
set @str2=Rtrim(@str3)
End

End
if (@pos=0)
Begin
Break
End
set @num=@num-1
End

return @str2
End





insert into Temp_CCXY_Event(groupid,serverid,event_type,event,memo,create_date)
values(1,1,17,'测试1','1001,5,1,5,0','2007-07-02');
Go
insert into Temp_CCXY_Event(groupid,serverid,event_type,event,memo,create_date)
values(1,1,17,'测试1','1001,5,2,6,1','2007-07-02');
Go
insert into Temp_CCXY_Event(groupid,serverid,event_type,event,memo,create_date)
values(1,1,17,'测试1','1001,5,2,6,0','2007-07-02');
Go
insert into Temp_CCXY_Event(groupid,serverid,event_type,event,memo,create_date)
values(1,1,17,'测试1','1001,6,3,6,0','2007-07-02');
Go
insert into Temp_CCXY_Event(groupid,serverid,event_type,event,memo,create_date)
values(1,1,17,'测试1','1002,5,2,6,0','2007-07-02');其他数据按这样的格式增加了..这几条数据就可以的测试出问题的








 

回复: 嵌套查询使用Group By出现数据异常问题

这是自定义函数结合Group By使用时产生的Bug,在SQL 2005中已得到纠正。
虽有智慧,不如乘势;虽有鎡基,不如待时。
君子学以聚之,问以辨之,宽以居之,仁以行之。
独学而无友,则孤陋而寡闻。
 

回复: 嵌套查询使用Group By出现数据异常问题

我在2000与2005企业版本上都测试过,也确实只有2000会出现这样的异常问题,我听他们说,你们微软的讲师,能否详细介绍下Group By嵌套查询时是如何执行的,好比对下我的理解是否正确,......................或者麻烦您上传份资料让我再学习下了....我油箱:jinguanding@sina.com.cn    先谢谢了!
 

回复: 嵌套查询使用Group By出现数据异常问题

Microsoft.Press.Inside.Microsoft.SQL.Server.2005.T-SQL.Querying.Apr.2006.chm
一书的第一章即有讲解.
 
1  /  1  页   1 跳转

版权所有 微软BI开拓者 

Powered by Discuz!NT 2.1.202    Copyright © 2001-2012 Comsenz Inc.
Processed in 0.0156256 second(s) , 4 queries.
返顶部