利用 MS-SQL TEMP 数据库 临时表 做 销售排行 的T-SQL复杂语句
2011-01-24 00:10
716 查看
利用 MS-SQL TEMP 数据库 临时表 做 销售排行 的T-SQL复杂语句 利用 MS-SQL TEMP 数据库 临时表 做 销售排行 的T-SQL复杂语句 Declare @p1BegDate char(10) Declare @p2EndDate char(10) Declare @P3OrgCode char(6)
select @p1BegDate=:p1BegDate select @p2EndDate=:p2EndDate select @P3OrgCode=:p3OrgCode Declare @i int Declare @sDate char(10) Declare @sSQL varchar(8000) /*创建临时销售表*/ If Exists(Select name from tempdb..sysobjects where name='##TmpSale' and XType='U') Drop table ##TmpSale Create Table ##TmpSale (OrgCode char(10),PluCode char(30),DepCode char(10),VendorCode char(30) NULL,Counts money,Cost money, NetCost money,Total money,Amount money,NetAmount money,NormalDsc money,VipDsc money,PriceDsc money, GrossProfit money,NetProfit money,AdjustProfit money,NetAdjustprofit money,AccDate char(10) ) /*识别出日期的区间的表 例如 20090501 到 20090530 那马选择 ITEM200905*/ Set @sDate=@p1BegDate While Left(@sDate,7)<=Left(@p2EndDate,7) begin If Exists(Select name from sysobjects where name='item'+Left(@sDate,4)+SubString(@sDate,6,2) and XType='U') begin set @sSQL = ' insert into ##TmpSale(OrgCode,PluCode,DepCode,VendorCode,Counts,Cost,NetCost,Total,Amount, NetAmount,NormalDsc,VipDsc,PriceDsc,GrossProfit,NetProfit,AdjustProfit, NetAdjustprofit,AccDate ) select OrgCode,PluCode,DepCode,VendorCode,Counts,Cost,NetCost,Total,Amount, NetAmount,NormalDsc,VipDsc,PriceDsc,GrossProfit,NetProfit,AdjustProfit, NetAdjustprofit,AccDate from goodssale'+Left(@sDate,4)+SubString(@sDate,6,2)+' where accdate >='''+@p1BegDate+''' and accdate <= '''+@p2enddate+''' ' /*识别组织代码*/
if @P3OrgCode <> '<全部>' begin set @sSQL = @sSQL + ' and OrgCode='''+@P3OrgCode+''' ' end Exec(@sSQl) end Set @sDate=Convert(Char(10),DateAdd(month,1,@sDate),120) end
/*开始查询表中的数据 并将取出的 数据 导入表TmpSaleNew*/ set @sSQL = ' select OrgCode as 组织编码,Dept.DepCode as 部门编码,DepName as 部门名称, sGoods.PluCode as 商品编码,PluName as 商品名称, sum(Counts) AS 销售数量,sum(Amount) AS 含税销售收入, sum(NetAmount) AS 无税销售收入, sum(GrossProfit-AdjustProfit) AS 毛利, sum(NetProfit-NetAdjustprofit) AS 净毛利, case Sum(Amount) when 0 then 100*(case when sum(GrossProfit-AdjustProfit)>=0 then 1 else -1 end) else sum(GrossProfit-AdjustProfit)/Sum(Amount)*100 end as 毛利率, sum(sGoods.Cost+AdjustProfit) AS 含税销售成本, sum(sGoods.NetCost+NetAdjustProfit) AS 无税销售成本, sum(NormalDsc+VipDsc+PriceDsc) AS 商品总优惠, sum(sGoods.Total) as 售价金额, sum(NormalDsc) as 普通顾客优惠,sum(VipDsc) as 会员优惠金额, sum(PriceDsc) as 调价变动金额 into ##TmpSaleNew from ##TmpSale as sGoods,Goods,Dept,xtRightDept x where Goods.pluCode=sGoods.pluCode and x.staffCode='''+@sysUserCode+''' and (Dept.DepCode=x.DepCode or Dept.UpperCode1=x.DepCode or Dept.UpperCode2=x.DepCode or Dept.UpperCode3=x.DepCode or Dept.UpperCode4=x.DepCode) and (Goods.DepCode=Dept.DepCode) group by OrgCode,Dept.DepCode,DepName,sGoods.PluCode,PluName'
--and x.staffCode='''+@sysUserCode+''' if @sysOrderSQL<>'' set @sSQL=@sSQL+' order by '+@sysOrderSQL else set @sSQL=@sSQL+' order by sum(GrossProfit-AdjustProfit) DESC'
Exec(@sSQL) Select Identity(int,1,1) as 销售排名,* into ##TmpSaleNew1 from ##TmpSaleNew select * from ##TmpSaleNew1 /*使用完毕以后删除创建的临时表*/ If Exists(Select name from tempdb..sysobjects where name='##TmpSale' and XType='U') Drop table ##TmpSale If Exists(Select name from tempdb..sysobjects where name='##TmpSaleNew' and XType='U') Drop table ##TmpSaleNew
If Exists(Select name from tempdb..sysobjects where name='##TmpSaleNew1' and XType='U') Drop table ##TmpSaleNew1 /***********************************************************/
相关文章推荐
- 利用临时数据表完成SQL语句输出并进行自动编号
- 利用SQL语句查询一个数据库中的所有表
- 利用SQL语句重置数据库中所有表的标识列(自增量)
- 利用T-SQL语句,实现数据库的备份与还原的功能
- (精)如何利用T_SQL实现数据库备份与还原处理之一--(如何利用sql语句,正确还原数据库)
- 利用T-SQL语句,实现数据库的备份与还原的功能
- 利用正则表达式类解析SQL语句,达到Worklist兼容各个RIS数据库的目的
- 利用T-SQL语句,实现数据库的备份与还原的功能
- (精)如何利用T_SQL实现数据库备份与还原处理之一--(如何利用sql语句,得到数据库文件目录)
- 利用SQL语句查找某数据库中所有存储过程包含的内容
- 数据库的创建,修改和删除----利用企业管理器和T-SQL语句
- (精)如何利用T_SQL实现数据库备份与还原处理之一--(如何利用sql语句,正确备份数据库)
- Qt数据库(四)利用QSqlQuery类执行SQL语句(二)
- 利用SQL语句创建数据库
- Qt数据库(三)利用QSqlQuery类执行SQL语句(一)
- 利用SQL语句查询数据库中所有表
- 利用SQL语句查询数据库中所有表
- 利用正则表达式类解析SQL语句,达到Worklist兼容各个RIS数据库的目的
- 数据库分页语句(ms sqlserver)
- 15.5数据库(5):SQL语句进阶1:单表内复杂查询