日期:2014-05-18  浏览次数:20789 次

web应用中的sql查询问题求助
Id int
ItemId int
ItemName                   nvarchar
Cost float
SelledPrice float
SelledYear int
SelledMonth int
SelledDay int int
ItemType                   nvarchar

Id自增,现在需要查询某一年,每个月份的销售总额,销售利润,总的成本
比如说:
select   sum(selledprice)   总收入,sum(cost)总支出,sum(selledprice)-sum(cost)利润   from   ItemInfo   where   selledyear=2007   and   selledmonth=4
查询的是2007年4月的总收入,总支出和利润

Q1:我在bean操作中想查询到每个月的销售的总收入,总支出,和利润
Q2:并且穿给jsp页面,然后在用图形方式表示




------解决方案--------------------
你连接数据库是用JDBC还是用的HIBERNATE啊.比如说是JDBC的话,可以先写一个查询方法,然后用
//先查出数据
ResultSet rs = conn.find( "select salary from table where condition ");
像你上面用这种方式求和的话,可能在JSP中不怎么好操作啊,可以这样写啊
float salary = 0;
while(rs.next())
{
salary =salary+ rs.getString( "salary ");


}
通过循环的方式取得数据的总和
将salary取出来的话在JSP中就可以取了 <%=salary%>
------解决方案--------------------
Q1:
select selledYear, selledMonth, sum(selledprice) 总收入,sum(cost)总支出,sum(selledprice)-sum(cost)利润
from ItemInfo
group by selledYear, selledMonth
order by selledYear, selledMonth

Q2:取出list后,采用一些报表框架如jasperreport+ireport将列表转换为图形。
------解决方案--------------------
就是一个分组求和SQL语句。

A1:执行SQL语句,把结果集用集合类包装一下,供页面显示。
A2:图形方式的话,自己作比较麻烦,在现有的报表工具中挑一个用。