T-SQL在SQL Server中的怪异行为

时间:2018-10-25 20:30:42

标签: sql-server-2008-r2

对于下面的数据集,我试图获取总计Emp_AVG <> 100的结果。基于Emp_AVG列中数据的顺序,我如何在SQL Server中获得怪异的结果。

请参见以下示例:

drop table #temp1

select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'P' as Emp_Catagory, '99.99' as Emp_AVG, 'JAN' as Emp_Month into #temp1 union all
select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'FEB' as Emp_Month  union all
select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'MAR' as Emp_Month  union all
select 'F_TEST1' as First_Name, 'L_TEST1' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'APR' as Emp_Month  union all

select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'P' as Emp_Catagory, '99.98' as Emp_AVG, 'JAN' as Emp_Month  union all
select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'FEB' as Emp_Month  union all
select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'MAR' as Emp_Month  union all
select 'F_TEST2' as First_Name, 'L_TEST2' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'APR' as Emp_Month  union all

select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'P' as Emp_Catagory, '99.97' as Emp_AVG, 'JAN' as Emp_Month  union all
select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.4' as Emp_AVG, 'FEB' as Emp_Month  union all
select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'MAR' as Emp_Month  union all
select 'F_TEST3' as First_Name, 'L_TEST3' as Last_Name, 'C' as Emp_Catagory, '33.3' as Emp_AVG, 'APR' as Emp_Month  

--select * from #temp1

select First_Name,Last_Name, Emp_Catagory, sum(cast(Emp_AVG as float)) as Total_AVG
from #temp1
Group by First_Name,Last_Name, Emp_Catagory
having Sum(cast(Emp_AVG as float)) <> 100
order by sum(cast(Emp_AVG as float)) desc

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

如果有人能为我提供解决方案,我将不胜感激。

关于, 吉加尔·B。

1 个答案:

答案 0 :(得分:1)

我的猜测是,您看到的“奇怪”结果是有些结果中Total_AVG等于100?像这样:

/---------------------------------------------------\
| First_Name | Last_Name | Emp_Catagory | Total_AVG |
|------------+-----------+--------------+-----------|
|  F_TEST2   |  L_TEST2  |      C       |   100     |
|  F_TEST3   |  L_TEST3  |      C       |   100     |
|  F_TEST1   |  L_TEST1  |      P       |    99.99  |
|  F_TEST2   |  L_TEST2  |      P       |    99.98  |
|  F_TEST3   |  L_TEST3  |      P       |    99.97  |
\---------------------------------------------------/

尝试将值强制转换为decimal

select 
    First_Name,
    Last_Name, 
    Emp_Catagory, 
    sum(cast(Emp_AVG as decimal(4,2))) as Total_AVG
from #temp1
group by 
    First_Name,
    Last_Name, 
    Emp_Catagory
having sum(cast(Emp_AVG as decimal(4,2))) <> 100
order by sum(cast(Emp_AVG as decimal(4,2))) desc

然后,您只会看到期望的结果:

/---------------------------------------------------\
| First_Name | Last_Name | Emp_Catagory | Total_AVG |
|------------+-----------+--------------+-----------|
|  F_TEST1   |  L_TEST1  |      P       |    99.99  |
|  F_TEST2   |  L_TEST2  |      P       |    99.98  |
|  F_TEST3   |  L_TEST3  |      P       |    99.97  |
\---------------------------------------------------/