xuejiayue's BLOG

联系邮箱xuejiayue@qq.com

利用 MS-SQL TEMP 数据库 临时表 做 销售排行 的T-SQL复杂语句

利用 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
/***********************************************************/
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。如果有需要转载请联系薛佳岳 xuejiayue@qq.com https://blog.csdn.net/xuejiayue1105/article/details/6160335
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭