求存储过程高手帮忙!只会写sql的人伤不起.

fuxiuhua 2012-05-24 11:25:59
这个用sql写效率太慢 求高手转换成存储过程 谢谢。。。


string year = Convert.ToDateTime(DateTime.Now.ToString()).Year.ToString();
string monthe = Convert.ToDateTime(DateTime.Now.ToString()).Month.ToString();

string sql = "select distinct Region_Code as 区域,City_Name as 城市,Sales_Name as 销售代表,Account_Name as 客户名称,Shop_Name as 店铺名称,Shop_Code as 店铺Code, ";
sql += "'" + (monthe) + "月'=(select sum(View_Store.sale_Amount) from View_Store where convert(varchar(10),View_Store.Report_Date,23) between '" + (year + "-01" + "-1") + "' and '" + (year + "-1") + "' and View_Store.Shop_Code=vf.Shop_Code) ,";

for (int i = 1; i < Convert.ToInt32(monthe) + 1; i++)
{
sql += "'" + i + "月'=(select View_Store.sale_Amount from View_Store where year(View_Store.Report_Date)=" + year + " and month(View_Store.Report_Date)=" + i + " and View_Store.Shop_Code=vf.Shop_Code), ";
sql += "'" + i + "月同期对比'=cast(cast((isnull((select View_Store.sale_Amount from View_Store where year(View_Store.Report_Date)=" + Convert.ToInt32(Convert.ToInt32(year) - 1) + " and month(View_Store.Report_Date)=" + i + " and View_Store.Shop_Code=vf.Shop_Code) ,null)/ isnull((select View_Store.sale_Amount from View_Store where year(View_Store.Report_Date)=" + year + " and month(View_Store.Report_Date)=" + i + " and View_Store.Shop_Code=vf.Shop_Code) ,null)-1) as float) as varchar) +'%',";
}

for (int j = 1; j <= Convert.ToInt32(DateTime.Now.Day); j++)
{
sql += " '" + (j) + "'=(select sum(sale_Amount) from Sell_Salesout INNER JOIN dbo.MST_Employee_Info ON dbo.Sell_Salesout.Employee_Code = dbo.MST_Employee_Info.Staff_Code INNER JOIN dbo.MST_Shop ON dbo.MST_Employee_Info.Shop_Code = dbo.MST_Shop.Shop_Code where year(Sell_Salesout.Report_Date)=" + year + " and month(Sell_Salesout.Report_Date)=" + monthe + " and Day(Sell_Salesout.Report_Date)=" + j + " and vf.Shop_Code=dbo.MST_Shop.Shop_Code) , ";
}



sql = sql.Remove(sql.LastIndexOf(","));
sql += " from View_SMSStore as vf ";




...全文
216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-05-26
  • 打赏
  • 举报
回复
建议写出你的表 字段 测试数据 需求的结果
nipzj 2012-05-24
  • 打赏
  • 举报
回复
你都不写实现什么功能怎么写?
  • 打赏
  • 举报
回复
谁知道你那是什么意思啊???
夜予 2012-05-24
  • 打赏
  • 举报
回复
把表字段,测试数据及要达到的目的贴出来

34,587

社区成员

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

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