各位达人,先听小弟的问题描述:
项目中需要完成一个报表功能,所以我先在mssql中使用了查询分析器,完成大致的sql语句,其功能已经达到了要求,代码如下
[code="java"]
select orgname, index_year, index_month,
sum(case when type='d'then 1 else 0 end), //当类型为‘d’是加1
sum(case when type='s' then 1 else 0 end), //当类型为‘s’是加1
sum(case when type='e' then 1 else 0 end), //当类型为‘e’是加1
sum(case when type='z' then 1 else 0 end), //当类型为‘z’是加1
sum(case when type!='s' then pprice else 0 end) from
(select sr.recid, datepart(year, sr.saledate) index_year,
datepart(month, sr.saledate) index_month,
u.cname, u.type, e.empname, o.orgname, p.pname, p.pprice
from t_sellrecord as sr, t_cuser as u, t_employment as e, t_organized as o, t_product as p
where datediff(month, sr.saledate ,getdate()) < 36
and o.recid = sr.saleorgid and sr.salesid = e.recid
and u.recid = sr.buyerid and p.recid = sr.proid )
x group by orgname, index_year, index_month order by orgname asc, index_year asc, index_month asc[/code]
然后直接考到java web项目中的dao中,使用hql语句,发现不支持datepart,所以改为直接sql,就使用了createSQLQuery,来进行查询,以上代码均未更改的直接copy了,查询结果另小弟十分疑惑。。。
sql查询分析器中是
eeee 2008 7 0 0 2 0 1000.0
aaaa 2008 7 0 2 0 0 0.0
bbbb 2008 7 0 0 2 0 1000.0
cccc 2008 7 0 1 0 0 0.0
dddd 2008 6 0 2 10 0 5000.0
dddd 2008 7 0 5 4 0 2000.0
而实际结果为
eeee 2008 7 0 0 0 0 0.0
aaaa 2008 7 0 0 0 0 0.0
bbbb 2008 7 0 0 0 0 0.0
cccc 2008 7 0 0 0 0 0.0
dddd 2008 6 0 0 0 0 0.0
dddd 2008 7 0 0 0 0 0.0
似乎,第一个sum中case when获取的结果把后面的值都覆盖了,
于是我试着吧type='d'改成1=1
sql查询分析器中是
eeee 2008 7 2 0 2 0 1000.0
aaaa 2008 7 2 2 0 0 0.0
bbbb 2008 7 2 0 2 0 1000.0
cccc 2008 7 1 1 0 0 0.0
dddd 2008 6 12 2 10 0 5000.0
dddd 2008 7 9 5 4 0 2000.0
而实际结果为
eeee 2008 7 2 2 2 2 2.0
aaaa 2008 7 2 2 2 2 2.0
bbbb 2008 7 2 2 2 2 2.0
cccc 2008 7 1 1 1 1 1.0
dddd 2008 6 12 12 12 12 12.0
dddd 2008 7 9 9 9 9 9.0
各位达人哇,我疑惑不解,疑惑不解,但求赐教~~跪谢 :cry: :cry: